Load a WPF BitmapImage from a System.Drawing.Bitmap?

MemoryStream ms = new MemoryStream(); bitmap. Save(ms, ImageFormat. Png); ms.

Position = 0; BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi. StreamSource = ms; bi.EndInit().

5 You could add this code as an extension method on System.Drawing. Bitmap, something like ToBitmapImage() – Luke Puplett Feb 15 '10 at 13:46 6 Using ImageFormat. Bmp is an order of magnitude faster.

– RandomEngy Mar 7 '10 at 20:35 10 In case others are having problems with this code: I had to add ms. Seek(0, SeekOrigin. Begin); before setting bi.StreamSource.

I'm using . NET 4.0. – mlsteeves May 7 '10 at 13:57 4 @mls that would be true of any version of .net.

I'm gonna sneak in there and fix that code; nobody tell Pawel. – Will? Sep 27 '10 at 18:00 I guess everybody understood we have to close the MemoryStream after its use ;) – JiBéDoublevé Sep 21 at 15:50.

I know this has been answered, but here are a couple of extension methods (for . NET 3.0+) that do the conversion. :) /// /// Converts a into a WPF .

/// /// The source image. /// A BitmapSource public static BitmapSource ToBitmapSource(this System.Drawing. Image source) { System.Drawing.

Bitmap bitmap = new System.Drawing. Bitmap(source); var bitSrc = bitmap.ToBitmapSource(); bitmap.Dispose(); bitmap = null; return bitSrc; } /// /// Converts a into a WPF . /// /// Uses GDI to do the conversion.

Hence the call to the marshalled DeleteObject. /// /// The source bitmap. /// A BitmapSource public static BitmapSource ToBitmapSource(this 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; } finally { NativeMethods. DeleteObject(hBitmap); } return bitSrc; } and the NativeMethods class (to appease FxCop) /// /// FxCop requires all Marshalled functions to be in a class called NativeMethods.

/// internal static class NativeMethods { DllImport("gdi32. Dll") return: MarshalAs(UnmanagedType. Bool) internal static extern bool DeleteObject(IntPtr hObject); }.

When using unmanaged handles (e.g. HBITMAP) consider using SafeHandles, see stackoverflow. Com/questions/1546091/… – Schneider Aug 12 at 2:38.

Thanks to Hallgrim, here is the code I ended up with: ScreenCapture = System.Windows.Interop.Imaging. CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr. Zero, System.Windows.

Int32Rect. Empty, BitmapSizeOptions. FromWidthAndHeight(width, height)); I also ended up binding to a BitmapSource instead of a BitmapImage as in my original question.

1 Great! Why don't you select your own answer as the answer to the question? Your's is much better now.

– Hallgrim Sep 18 '08 at 21:28 Since yours is the accepted answer already, you could edit your answer to make it more complete. – Alan Jackson May 28 '09 at 5:35 11 Do mind that this code leaks a HBitmap. See stackoverflow.Com/questions/1118496/… for a fix – Lars Truijens Jul 13 '09 at 11:54 Please also note that this code will produce aמ InteropBitmap.

I had a problem with this when using XamlWriter and then XamlReader - InteropBitmap is not supported. See this link for more details. – Avi Bueno Sep 15 at 10:11.

It took me some time to get the conversion working both ways, so here are the two extension methods I came up with: using System. Drawing; using System.Drawing. Imaging; using System.IO; using System.Windows.Media.

Imaging; public static class BitmapConversion { public static Bitmap ToWinFormsBitmap(this BitmapSource bitmapsource) { using (MemoryStream stream = new MemoryStream()) { BitmapEncoder enc = new BmpBitmapEncoder(); enc.Frames. Add(BitmapFrame. Create(bitmapsource)); enc.

Save(stream); using (var tempBitmap = new Bitmap(stream)) { // According to MSDN, one "must keep the stream open for the lifetime of the Bitmap. " // So we return a copy of the new bitmap, allowing us to dispose both the bitmap and the stream. Return new Bitmap(tempBitmap); } } } public static BitmapSource ToWpfBitmap(this Bitmap bitmap) { using (MemoryStream stream = new MemoryStream()) { bitmap.

Save(stream, ImageFormat. Bmp); stream. Position = 0; BitmapImage result = new BitmapImage(); result.BeginInit(); // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed.

" // Force the bitmap to load right now so we can dispose the stream. Result. CacheOption = BitmapCacheOption.

OnLoad; result. StreamSource = stream; result.EndInit(); result.Freeze(); return result; } } }.

The easiest thing is if you can make the WPF bitmap from a file directly. Otherwise you will have to use System.Windows.Interop.Imaging. CreateBitmapSourceFromHBitmap.

I work at an imaging vendor and wrote an adapter for WPF to our image format which is similar to a System.Drawing.Bitmap. I wrote this KB to explain it to our customers: atalasoft.com/kb/article.aspx?id=10156 And there is code there that does it. You need to replace AtalaImage with Bitmap and do the equivalent thing that we are doing -- it should be pretty straightforward.

Thanks Lou - was able to do what I needed with one line of code – Kevin Sep 18 '08 at 20:21.

I know this is old, but the solution offered in 5 will lead to memory leaks as is. Check out: stackoverflow.com/questions/1546091/wpf-....

I came to this question because I was trying to do the same, but in my case the Bitmap is from a resource/file. I found the best solution is as described in the following link: msdn.microsoft.com/en-us/library/system.... // Create the image element. Image simpleImage = new Image(); simpleImage.

Width = 200; simpleImage. Margin = new Thickness(5); // Create source. BitmapImage bi = new BitmapImage(); // BitmapImage.

UriSource must be in a BeginInit/EndInit block. Bi.BeginInit(); bi. UriSource = new Uri(@"/sampleImages/cherries_larger.

Jpg",UriKind. RelativeOrAbsolute); bi.EndInit(); // Set the image source. SimpleImage.

Source = bi.

Converts a into a WPF . /// /// Uses GDI to do the conversion. Hence the call to the marshalled DeleteObject.

/// /// The source bitmap. /// A BitmapSource public static System.Windows.Media.Imaging. BitmapSource ToBitmapSource(this System.Drawing.

Bitmap source) { var hBitmap = source.GetHbitmap(); var result = System.Windows.Interop.Imaging. CreateBitmapSourceFromHBitmap(hBitmap, IntPtr. Zero, System.Windows.

Int32Rect. Empty, System.Windows.Media.Imaging. BitmapSizeOptions.

FromEmptyOptions()); DeleteObject(hBitmap); return result; }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions