I need to convert a System.Drawing.Bitmap into System.Windows.Media.ImageSource class in order to bind it into a HeaderImage control of a WizardPage (Extended WPF toolkit). The bitmap is set as a resource of the assembly I write. It is being referenced like that:
public Bitmap GetBitmap
{
get
{
Bitmap bitmap = new Bitmap(Resources.my_banner);
return bitmap;
}
}
public ImageSource HeaderBitmap
{
get
{
ImageSourceConverter c = new ImageSourceConverter();
return (ImageSource) c.ConvertFrom(GetBitmap);
}
}
The converter was found by me here: http://www.codeproject.com/Questions/621920/How-to-convert-Bitmap-to-ImageSource I get a NullReferenceException at
return (ImageSource) c.ConvertFrom(Resources.my_banner);
How can i initialize ImageSource in order to avoid this exception? Or is there another way?
I want to use it afterwards like:
<xctk:WizardPage x:Name="StartPage" Height="500" Width="700"
HeaderImage="{Binding HeaderBitmap}" Enter="StartPage_OnEnter"
Thanks in advance for any answers.
For others, this works:
I do not believe that
ImageSourceConverter
will convert from aSystem.Drawing.Bitmap
. However, you can use the following:This solution requires the source image to be in Bgra32 format; if you are dealing with other formats, you may need to add a conversion.
The simpliest solution for me:
dethSwatch - Thank you for your answer above! It helped tremendously! After implementing it I was getting the desired behavior but I found I was getting a memory/handle issue in another section of my program. I changed the code as follows, making it a little more verbose and the issue went away. Thank you again!
For the benefit of searchers, I created a quick converter based on a this more detailed solution.
No problems so far.