2009-08-26 120 views

回答

3

我修改了一個例子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()); 
    } 
+0

謝謝!這個答案幫了我很多。但是當我將圖片分配給一個圖片框然後重新調整圖片框的大小時,我發現了一個問題。我的解決方案是將圖像繪製回新的位圖,所以我不再需要不安全的數組。 http://stackoverflow.com/questions/5635968/open-and-display-a-hd-photo-in-winforms-application/5646307#5646307 – Kempeth 2011-04-13 08:23:07

+0

這泄漏Hicon手柄。 – 2016-11-14 12:25:32

2

我們有這個問題幾個月回來,我們發現這個解決方案

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

我SOOO高興我們插入我們的地方,我們發現了一些評論引用。我更願意將它發送給你,而不是我的代碼,因爲它與一個獲取多個壓縮文件合併在一起,使你真正想要的東西變得複雜。

+0

這將System.Drawing.Image轉換爲System.Drawing.Icon。我試圖將System.Windows.Control.Image轉換爲System.Drawing.Icon。 – 2009-08-27 11:46:09

4

作爲替代方案,我使用了提示發現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()); 
} 
+0

這會泄漏Hicon手柄。 – 2016-11-14 12:25:11

0

我從代碼中的WPF XAML的IValueConverter類,一個字節()陣列與圖像轉換爲圖標製成,這裏是代碼:

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 
相關問題