Can you convert System.Windows.Control.Image to System.Drawing.Icon?

The title of the question largely indicates a problem. Is it possible?

+3
source share
4 answers

I changed the example from here . It seems to work very well.

    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());
    }
+3
source

As an alternative, I 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());
}
+4
source

,

http://www.dreamincode.net/code/snippet1684.htm

I am very glad that we are inserting links in our comments to where we found something. I prefer to send this to you instead of my code, because it merges with several archived files that enhance what you really want to receive.

+2
source

I made an IValueConverter Class from your WPF XAML code that converts a byte () array with an image into 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
0
source

Source: https://habr.com/ru/post/1716143/


All Articles