C# 能否将System.Windows.Control.Image转换为System.Drawing.Icon?
问题的标题大致说明了问题所在。有可能吗?几个月前我们遇到了这个问题,我们找到了这个解决方案C# 能否将System.Windows.Control.Image转换为System.Drawing.Icon?,c#,wpf,image,C#,Wpf,Image,问题的标题大致说明了问题所在。有可能吗?几个月前我们遇到了这个问题,我们找到了这个解决方案 我很高兴我们在评论中提到了我们发现的东西。我更喜欢将此文件发送给您,而不是我的代码,因为它与get multiple zipped文件合并,这会使您真正想要的内容变得复杂。我修改了中的一个示例。这似乎很有效 public static Icon Convert(BitmapImage bitmapImage) { System.Drawing.Bitmap bitmap
我很高兴我们在评论中提到了我们发现的东西。我更喜欢将此文件发送给您,而不是我的代码,因为它与get multiple zipped文件合并,这会使您真正想要的内容变得复杂。我修改了中的一个示例。这似乎很有效
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());
}
作为替代方案,我使用了发现的提示:
我根据您的代码创建了一个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
这会将System.Drawing.Image转换为System.Drawing.Icon。我正在尝试将System.Windows.Control.Image转换为System.Drawing.Icon。谢谢!这个答案对我帮助很大。但当我将图像分配给picturebox,然后调整框的大小时,我发现了一个问题。我的解决方案是将图像重新绘制成一个新的位图,这样我就不再需要不安全的数组了。这泄露了Hicon把手这泄露了Hicon把手。
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