【问题标题】:Can you convert a System.Windows.Control.Image to a System.Drawing.Icon?您可以将 System.Windows.Control.Image 转换为 System.Drawing.Icon 吗?
【发布时间】:2009-08-26 20:17:11
【问题描述】:

问题的标题几乎说明了问题。有可能吗?

【问题讨论】:

    标签: c# wpf image


    【解决方案1】:

    作为替代方案,我使用了 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());
    }
    

    【讨论】:

    • 这会泄露 Hicon 句柄。
    【解决方案2】:

    我从here 修改了一个示例。这似乎工作得很好。

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

    【讨论】:

    • 谢谢!这个答案对我帮助很大。但是当我将图像分配给图片框然后调整框大小时,我发现了一个问题。我的解决方案是将图像重新绘制到一个新的位图中,这样我就不再需要不安全的数组了。 stackoverflow.com/questions/5635968/…
    • 这会泄露 Hicon 句柄。
    【解决方案3】:

    几个月前我们遇到了这个问题,我们找到了这个解决方案

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

    我很高兴我们在 cmets 中插入了我们发现某些东西的引用。我更喜欢将此而不是我的代码发送给您,因为它与一个获取多个压缩文件合并,这使您真正想要获得的内容变得复杂。

    【讨论】:

    • 这会将 System.Drawing.Image 转换为 System.Drawing.Icon。我正在尝试将 System.Windows.Control.Image 转换为 System.Drawing.Icon。
    【解决方案4】:

    我从您的代码中创建了一个 WPF XAML IValueConverter 类,该类将带有图像的 byte() 数组转换为 Icon ,代码如下:

    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
    

    【讨论】:

      猜你喜欢
      • 2010-11-10
      • 2021-02-04
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 2012-02-14
      • 2016-08-20
      相关资源
      最近更新 更多