The title of the question pretty much states the problem. Is it possible?
Asked
Active
Viewed 8,017 times
4 Answers
4
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());
}

Daniel Rose
- 17,233
- 9
- 65
- 88
-
This leaks the Hicon handle. – Quolonel Questions Nov 14 '16 at 12:25
3
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());
}

Mark Schroering
- 71
- 1
- 6
-
Thanks! This answer helped me a lot. But I found a problem where my image when I assign it to a picturebox and then resize the box. My solution was to draw the image back into a new bitmap so I no longer need the unsafe array. http://stackoverflow.com/questions/5635968/open-and-display-a-hd-photo-in-winforms-application/5646307#5646307 – Kempeth Apr 13 '11 at 08:23
-
2
We had this problem a couple of months back and we found this solution
http://www.dreamincode.net/code/snippet1684.htm
I'm sooo glad that we insert references in our comments to where we found something. I prefer sending this to you instead of my code because it's merge with a get multiple zipped file which complexify what you really want to get at.

Philippe Asselin
- 363
- 2
- 6
-
This converts a System.Drawing.Image to a System.Drawing.Icon. I'm trying to convert a System.Windows.Control.Image to an System.Drawing.Icon. – Mark Schroering Aug 27 '09 at 11:46
0
I made from your code a WPF XAML IValueConverter Class that converts a byte() array with an image to an Icon , here is the code :
Public Class ByteArrayToIconConverter
Implements IValueConverter
' Define the Convert method to change a byte[] to icon.
Public Function Convert(ByVal value As Object, _
ByVal targetType As Type, ByVal parameter As Object, _
ByVal culture As System.Globalization.CultureInfo) As Object _
Implements System.Windows.Data.IValueConverter.Convert
If Not value Is Nothing Then
' value is the data from the source object.
Dim data() As Byte = CType(value, Byte())
Dim ms1 As MemoryStream = New MemoryStream(data)
Dim ms2 As MemoryStream = New MemoryStream()
Dim img As New BitmapImage()
img.BeginInit()
img.StreamSource = ms1
img.EndInit()
Dim encoder As New PngBitmapEncoder()
encoder.Frames.Add(BitmapFrame.Create(img))
encoder.Save(ms2)
Dim bmp As New Bitmap(ms2)
Dim newIcon As Icon = Icon.FromHandle(bmp.GetHicon())
Return newIcon
End If
End Function
' ConvertBack is not implemented for a OneWay binding.
Public Function ConvertBack(ByVal value As Object, _
ByVal targetType As Type, ByVal parameter As Object, _
ByVal culture As System.Globalization.CultureInfo) As Object _
Implements System.Windows.Data.IValueConverter.ConvertBack
Throw New NotImplementedException
End Function
End Class

Cédric Bellec
- 31
- 3