{"id":2705,"date":"2022-08-30T15:27:05","date_gmt":"2022-08-30T15:27:05","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/08\/place-an-icon-object-in-wpf-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:27:05","modified_gmt":"2022-08-30T15:27:05","slug":"place-an-icon-object-in-wpf-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/place-an-icon-object-in-wpf-collection-of-common-programming-errors\/","title":{"rendered":"Place an Icon Object in WPF-Collection of common programming errors"},"content":{"rendered":"<p>I have instantiated several System.Drawing.Icon Objects. Note that these are created at runtime and are not stored and loaded from the file system. I would like to place these images in my WPF application.<\/p>\n<p>However as I have discovered over the last few hours, it is not possible to simply add an object such as an system.drawing.image or icon directly to the canvas\/stack panel, nor is it possible to set the source of a System.Windows.Controls.Image to an image or icon not stored within the file system (or so it seems to me).<\/p>\n<p>Any ideas?<\/p>\n<ol>\n<li>\n<p>This has worked for me dynamically setting a WPF Image with bytes loaded from a bitmap that was either dynamically generated or loaded from disk:<\/p>\n<pre><code>using System;\nusing System.Collections.Generic;\nusing System.Text;\nusing System.Windows;\nusing System.Windows.Controls;\nusing System.Windows.Data;\nusing System.Windows.Documents;\nusing System.Windows.Input;\nusing System.Windows.Media;\nusing System.Windows.Media.Imaging;\nusing System.Windows.Navigation;\nusing System.Windows.Shapes;\n\nusing System.IO;\nusing System.Drawing.Imaging;\n\nnamespace Examples\n{\n    public class Util\n    {\n        private static void SetBitmap(Image imgDest, Bitmap bmpSource)\n        {\n            byte[] imageBytes;\n            using (MemoryStream stream = new MemoryStream())\n            {\n                bmpSource.Save(stream, ImageFormat.Png);\n\n                imageBytes = stream.ToArray();\n            }\n\n            BitmapImage bitmapImage = new BitmapImage();\n            bitmapImage.BeginInit();\n            bitmapImage.StreamSource = new MemoryStream(imageBytes);\n            bitmapImage.EndInit();\n\n            imgDest.Source = bitmapImage;\n        }\n    }\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-02-08 02:19:48. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have instantiated several System.Drawing.Icon Objects. Note that these are created at runtime and are not stored and loaded from the file system. I would like to place these images in my WPF application. However as I have discovered over the last few hours, it is not possible to simply add an object such as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2705","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2705"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2705\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}