How do I assign an in-memory Bitmap object to an Image control in WPF ?
            Asked
            
        
        
            Active
            
        
            Viewed 1.3e+01k times
        
    76
            
            
         
    
    
        TLama
        
- 75,147
- 17
- 214
- 392
 
    
    
        Prashant Cholachagudda
        
- 13,012
- 23
- 97
- 162
- 
                    Exact duplicate of http://stackoverflow.com/questions/94456/load-a-wpf-bitmapimage-from-a-system-drawing-bitmap but my answer does not leak HBitmap – Lars Truijens Jul 13 '09 at 11:55
- 
                    Does this answer your question? [Load a WPF BitmapImage from a System.Drawing.Bitmap](https://stackoverflow.com/questions/94456/load-a-wpf-bitmapimage-from-a-system-drawing-bitmap) – StayOnTarget Feb 04 '20 at 17:25
4 Answers
86
            
            
        According to 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;
   }
It gets System.Drawing.Bitmap (from WindowsBased) and converts it into BitmapSource, which can be actually used as image source for your Image control in WPF.
image1.Source = YourUtilClass.loadBitmap(SomeBitmap);
 
    
    
        Community
        
- 1
- 1
 
    
    
        Lars Truijens
        
- 42,837
- 6
- 126
- 143
- 
                    7Thx Lars, but I did much simpler, BitmapImage bmpi = new BitmapImage(); bmpi.BeginInit(); bmpi.StreamSource = new MemoryStream(ByteArray); bmpi.EndInit(); image1.Source = bmpi; – Prashant Cholachagudda Jul 13 '09 at 10:13
- 
                    4Great. You can add your sollution as an answer to your own question. – Lars Truijens Jul 13 '09 at 11:58
- 
                    I do not see a BitmapImage.StreamSource method. Prashant, did you type something wrong? – Patrick Szalapski Oct 13 '09 at 23:22
- 
                    
- 
                    Patrick, see http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.streamsource.aspx – Lars Truijens Oct 14 '09 at 06:50
- 
                    4When using unmanaged handles (e.g. HBITMAP) consider using SafeHandles, see http://stackoverflow.com/questions/1546091/wpf-createbitmapsourcefromhbitmap-memory-leak/7035036#7035036 – Jack Ukleja Aug 12 '11 at 02:34
- 
                    
17
            
            
        It's easy for disk file, but harder for Bitmap in memory.
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;
- 
                    Thx,but the code have not closed the ms.I think you will use http://stackoverflow.com/a/1069509/6116637 – lindexi May 08 '17 at 03:17
- 
                    @lindexi Even though `MemoryStream` implements `IDisposable`, it does not require to be disposed explicitly since it does not wrap any unmanaged resource. It is like a byte array and will eventually get collected by GC. – kennyzx May 15 '17 at 02:33
17
            You can use the Source property of the image. Try this code...
ImageSource imageSource = new BitmapImage(new Uri("C:\\FileName.gif"));
image1.Source = imageSource;
- 
                    2I have Bitmap object, actully it is generated from a scan device, so I cant refer to any location – Prashant Cholachagudda Jul 13 '09 at 10:11
2
            
            
        I wrote a program with wpf and used Database for showing images and this is my code:
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;
 
    
    
        senia
        
- 37,745
- 4
- 88
- 129
- 
                    3Good answer, but I would highly recommend wrapping the Sql objects in using statements so they're disposed when you're done using them. – Maurice Reeves Dec 12 '12 at 22:23
