Using Image control in WPF to display System.Drawing.Bitmap
Asked Answered
C

4

76

How do I assign an in-memory Bitmap object to an Image control in WPF ?

Counterattack answered 13/7, 2009 at 9:36 Comment(2)
Exact duplicate of #94956 but my answer does not leak HBitmapBeatup
Does this answer your question? Load a WPF BitmapImage from a System.Drawing.BitmapDympha
W
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;
Watchband answered 13/7, 2009 at 9:55 Comment(1)
I have Bitmap object, actully it is generated from a scan device, so I cant refer to any locationCounterattack
B
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);
Beatup answered 13/7, 2009 at 9:51 Comment(7)
Thx Lars, but I did much simpler, BitmapImage bmpi = new BitmapImage(); bmpi.BeginInit(); bmpi.StreamSource = new MemoryStream(ByteArray); bmpi.EndInit(); image1.Source = bmpi;Counterattack
Great. You can add your sollution as an answer to your own question.Beatup
I do not see a BitmapImage.StreamSource method. Prashant, did you type something wrong?Bitstock
Or a property, for that matter.Bitstock
Patrick, see msdn.microsoft.com/en-us/library/…Beatup
When using unmanaged handles (e.g. HBITMAP) consider using SafeHandles, see #1546591Earn
@Lars Truijens Very nice, would never of thought of that.Intoxicated
W
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;
Watchband answered 13/7, 2009 at 9:55 Comment(1)
I have Bitmap object, actully it is generated from a scan device, so I cant refer to any locationCounterattack
H
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;

Stealed here

Hepatic answered 26/4, 2012 at 14:23 Comment(2)
Thx,but the code have not closed the ms.I think you will use https://mcmap.net/q/116728/-load-a-wpf-bitmapimage-from-a-system-drawing-bitmapAindrea
@Aindrea 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.Actuality
U
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;
Uppercut answered 12/12, 2012 at 22:6 Comment(1)
Good answer, but I would highly recommend wrapping the Sql objects in using statements so they're disposed when you're done using them.Pomade

© 2022 - 2024 — McMap. All rights reserved.