你能将System.Windows.Control.Image转换为System.Drawing.Icon吗?

4
问题的标题已经表达了问题。这种情况是否可能?
4个回答

4

作为替代方案,我使用了在这里找到的提示:

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

这会泄露 Hicon 句柄。 - Quolonel Questions

3

我修改了这里的示例。这似乎运作得非常好。

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

谢谢!这个答案帮了我很多。但是我发现一个问题,当我将图像分配给picturebox并调整框的大小时。我的解决方案是将图像重新绘制到新位图中,因此我不再需要不安全的数组。http://stackoverflow.com/questions/5635968/open-and-display-a-hd-photo-in-winforms-application/5646307#5646307 - Kempeth
这会泄露 Hicon 句柄。 - Quolonel Questions

2
我们几个月前遇到过这个问题,后来找到了这个解决方案:http://www.dreamincode.net/code/snippet1684.htm。我很高兴我们在注释中插入引用,以表明我们从哪里找到了某些东西。我更喜欢将此发送给您,而不是发送我的代码,因为它与获取多个压缩文件的代码合并在一起,这使得您真正想要看到的东西变得复杂了。

这将把一个 System.Drawing.Image 转换成一个 System.Drawing.Icon。我正在尝试将一个 System.Windows.Control.Image 转换成一个 System.Drawing.Icon。 - Mark Schroering

0
我从你的代码中创建了一个WPF XAML IValueConverter类,它将带有图像的byte()数组转换为图标,以下是代码:
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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接