2009-07-13 13 views

risposta

16

È possibile utilizzare la proprietà Origine dell'immagine. Prova questo codice ...

ImageSource imageSource = new BitmapImage(new Uri("C:\\FileName.gif")); 

image1.Source = imageSource; 
+38

Ha già la bitmap in memoria, quindi non può usare un uri –

+0

Ho un oggetto Bitmap, actully è generato da un dispositivo di scansione, quindi non posso riferire a nessuna posizione –

+0

ho un metodo che restituisce l'immagine bitmap .. come impostarlo come sorgente –

83

Secondo http://khason.net/blog/how-to-use-systemdrawingbitmap-hbitmap-in-wpf/

[DllImport("gdi32")] 
    static extern int DeleteObject(IntPtr o); 

    public static BitmapSource loadBitmap(System.Drawing.Bitmap source) 
    { 
     IntPtr ip = source.GetHbitmap(); 
     BitmapSource bs = null; 
     try 
     { 
      bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, 
       IntPtr.Zero, Int32Rect.Empty, 
       System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); 
     } 
     finally 
     { 
      DeleteObject(ip); 
     } 

     return bs; 
    } 

Ottiene System.Drawing.Bitmap (da WindowsBased) e lo converte in BitmapSource, che può essere effettivamente utilizzata come fonte di immagine per il controllo Image in WPF.

image1.Source = YourUtilClass.loadBitmap(SomeBitmap); 
+7

Thx Lars, ma l'ho fatto molto più semplice, BitmapImage bmpi = new BitmapImage(); bmpi.BeginInit(); bmpi.StreamSource = new MemoryStream (ByteArray); bmpi.EndInit(); image1.Source = bmpi; –

+3

Grande. Puoi aggiungere la tua sollution come risposta alla tua stessa domanda. –

+0

Non vedo un metodo BitmapImage.StreamSource. Prashant, hai scritto qualcosa di sbagliato? –

16

È facile per il file su disco, ma più difficile per Bitmap in memoria.

System.Drawing.Bitmap bmp; 
Image image; 
... 
MemoryStream ms = new MemoryStream(); 
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png); 
ms.Position = 0; 
BitmapImage bi = new BitmapImage(); 
bi.BeginInit(); 
bi.StreamSource = ms; 
bi.EndInit(); 

image.Source = bi; 

Stealed here

+0

Thx, ma il codice non ha chiuso il ms.I penso che userete http://stackoverflow.com/a/1069509/6116637 – lindexi

+0

@lindexi Anche se 'MemoryStream' implementa' IDisposable', non richiede di essere eliminato in modo esplicito in quanto non avvolge alcuna risorsa non gestita. È come un array di byte e alla fine verrà raccolto da GC. – kennyzx

2

ho scritto un programma con wpf e utilizzati database per mostrare le immagini e questo è il mio codice:

SqlConnection con = new SqlConnection(@"Data Source=HITMAN-PC\MYSQL; 
             Initial Catalog=Payam; 
             Integrated Security=True"); 

SqlDataAdapter da = new SqlDataAdapter("select * from news", con); 

DataTable dt = new DataTable(); 
da.Fill(dt); 

string adress = dt.Rows[i]["ImgLink"].ToString(); 
ImageSource imgsr = new BitmapImage(new Uri(adress)); 
PnlImg.Source = imgsr; 
+2

Buona risposta, ma consiglio vivamente di includere gli oggetti Sql nell'utilizzo delle istruzioni, in modo che siano disposte quando hai finito di usarle. –

Problemi correlati