Setting WPF image source in code
        Posted  
        
            by Torbjørn
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Torbjørn
        
        
        
        Published on 2008-12-08T16:15:29Z
        Indexed on 
            2010/03/24
            20:33 UTC
        
        
        Read the original article
        Hit count: 412
        
I'm trying to set a WPF image's source in code. The image is embedded as a resource in the project. By looking at examples I've come up with the below code. For some reason it doesn't work - the image does not show up.
By debugging I can see that the stream contains the image data. So what's wrong?
Assembly asm = Assembly.GetExecutingAssembly();
Stream iconStream = asm.GetManifestResourceStream("SomeImage.png");
PngBitmapDecoder iconDecoder = new PngBitmapDecoder(iconStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
ImageSource iconSource = iconDecoder.Frames[0];
_icon.Source = iconSource;
The icon is defined something like this: <Image x:Name="_icon" Width="16" Height="16" />
© Stack Overflow or respective owner