The title of the question pretty much states the problem. Is it possible?
As an alternative, I've used the hints found here:
public static Icon Convert(BitmapImage bitmapImage)
{
var ms = new MemoryStream();
var encoder = new PngBitmapEncoder(); // With this we also respect transparency.
encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
encoder.Save(ms);
var bmp = new Bitmap(ms);
return Icon.FromHandle(bmp.GetHicon());
}
I modified an example from here. This seems to work pretty good.
public static Icon Convert(BitmapImage bitmapImage)
{
System.Drawing.Bitmap bitmap = null;
var width = bitmapImage.PixelWidth;
var height = bitmapImage.PixelHeight;
var stride = width * ((bitmapImage.Format.BitsPerPixel + 7) / 8);
var bits = new byte[height * stride];
bitmapImage.CopyPixels(bits, stride, 0);
unsafe
{
fixed (byte* pB = bits)
{
var ptr = new IntPtr(pB);
bitmap = new System.Drawing.Bitmap(width, height, stride,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb,
ptr);
}
}
return Icon.FromHandle(bitmap.GetHicon());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With