2017-04-15 5 views
0

Ich habe ein picturebox genannt picture ich das Bild dieses picturebox erhalten möchten und speichern Sie es als BitmapGet Bitmap von einem picturebox in C#

BitmapSource myPic; 
    myPic = picture.Image; 

Aber ich bekomme diese Fehlermeldung:

Severity-Code Beschreibung Projekt Datei Zeilenunterdrückungsstatus Fehler CS0029 Implizit kann der Typ 'System.Drawing.Image' nicht in 'System.Windows.Media.Imaging.BitmapSource' konvertiert werden

Antwort

1

Sie müssen das Syste konvertieren m.Drawing.Image in eine System.Drawing.Bitmap und wandle sie als nächstes in eine BitmapSource um.

können Sie eine dieser Lösungen wählen: fast converting Bitmap to BitmapSource wpf

1

Verwendung dieser Methode:

public BitmapSource ImageToBitmapSource(System.Drawing.Image image) 
{ 
     var bitmap = new System.Drawing.Bitmap(image); 

     var bitSrc =BitmapToBitmapSource(bitmap); 

     bitmap.Dispose(); 
     bitmap = null; 

     return bitSrc; 
} 

public BitmapSource BitmapToBitmapSource(System.Drawing.Bitmap source) 
{ 
     BitmapSource bitSrc = null; 

     var hBitmap = source.GetHbitmap(); 

     try 
     { 
      bitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
       hBitmap, 
       IntPtr.Zero, 
       Int32Rect.Empty, 
       BitmapSizeOptions.FromEmptyOptions()); 
     } 
     catch (Win32Exception) 
     { 
      bitSrc = null; 
     } 

     return bitSrc; 
}