{"id":2894,"date":"2019-11-19T14:27:15","date_gmt":"2019-11-19T13:27:15","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=2894"},"modified":"2022-07-25T08:33:24","modified_gmt":"2022-07-25T07:33:24","slug":"c-wpf-load-image-from-embedded-resource","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-wpf-load-image-from-embedded-resource\/","title":{"rendered":"C# WPF Load image from embedded resource"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System.IO;\nusing System.Reflection;\nusing System.Windows.Media.Imaging;\n\nnamespace Plugin.Resources\n{\n    public static class Resource\n    {\n        public static BitmapFrame GreenImage { get; private set; }\n        public static BitmapFrame RedImage { get; private set; }\n\n        public static void LoadBitmapsInCurrentThread()\n        {\n            GreenImage = LoadBitmapFromResource(\"Plugin.Resources.green.png\");\n            RedImage = LoadBitmapFromResource(\"Plugin.Resources.red.png\");\n        }\n\n        private static BitmapFrame LoadBitmapFromResource(string resourceName)\n        {\n            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))\n            {\n                var bitmapFrame = BitmapFrame.Create(stream, BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnLoad);\n                bitmapFrame.Freeze();\n                return bitmapFrame;\n            }\n        }\n    }\n}\n<\/pre>\n\n\n\n<p>Using Compile type <strong>Resource<\/strong> iso <strong>Embedded Resource<\/strong>, just use Uri&#8217;s<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">this.image.Source = new Uri(\"\/Image.png\", UriKind.Relative);<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ using System.Windows;\n        private static BitmapFrame LoadBitmapFromResource(Uri resourceUri)\n        {\n            using (StreamResourceInfo info = Application.GetContentStream(resourceUri);)\n            {\n                var bitmapFrame = BitmapFrame.Create(info.stream, BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnLoad);\n                bitmapFrame.Freeze();\n                return bitmapFrame;\n            }\n        }\n<\/pre>\n\n\n\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/framework\/wpf\/app-development\/wpf-application-resource-content-and-data-files\">https:\/\/docs.microsoft.com\/en-us\/dotnet\/framework\/wpf\/app-development\/wpf-application-resource-content-and-data-files<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using Compile type Resource iso Embedded Resource, just use Uri&#8217;s https:\/\/docs.microsoft.com\/en-us\/dotnet\/framework\/wpf\/app-development\/wpf-application-resource-content-and-data-files<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-2894","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2894","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=2894"}],"version-history":[{"count":5,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2894\/revisions"}],"predecessor-version":[{"id":2910,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2894\/revisions\/2910"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=2894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=2894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=2894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}