How to use ImageSource (no handler) in WinForms as System.Drawing.Bitmap (hbitmap)
How to easily convert WinForms System.Drawing.Bitmap into WPF ImageSource you learned from this article. Today, I’ll explain how to do it contrary. Actually, all he have to do is to extract handler from BitmapSource, however, such approach is not supported, thus the only thing we can do is just copy pixels of BitmapSource (or BitmapFrame) into byte array and then copy them into the pointer of HBitmap. Here the code
public static System.Drawing.Bitmap BitmapSourceToBitmap(BitmapSource srs) { System.Drawing.Bitmap btm = null; int width = srs.PixelWidth; int height = srs.PixelHeight; int stride = width * ((srs.Format.BitsPerPixel + 7) / 8); byte[] bits = new byte[height * stride]; srs.CopyPixels(bits, stride, 0); unsafe { fixed (byte* pB = bits) { IntPtr ptr = new IntPtr(pB); btm = new System.Drawing.Bitmap( width, height, stride, System.Drawing.Imaging.PixelFormat.Format32bppPArgb, ptr); } } return btm; }
Not so straight forward, but it works.
You may also be interested with:
September 8th, 2007 · Comments (0)
No comments yet
Leave a Reply
Discover other tags
.NET 3.5
Accessibility
blogging general
blogging tools
C#
CodeProject
demos
DEV
DevAcademy2
DirectX
download
events
fun
Hardware
help
Interop
jobs
LINQ
Microsoft
Mobile
My tools
Performance
promo
Silverlight
soft
source
teched
TechedIsrael2008
thoughts
Tips and Tricks
tutorial
Vista
Vista Battery Saver
Visual Studio
VSTS
WCF
Web
Windows Gadgets
Windows Live
Work process
WPF
WPF/E
WPF crossbow
XNA
XPS
My tools
- .NET Framework Detector
- Duplicate images finder
- Exchange Security Policy for Windows Mobile Devices Fix
- Gas Price Windows Vista SideBar gadget
- Israel Traffic Information Windows Vista SideBar gadget
- Localization fix for SAP ES Explorer for Visual Studio
- LocTester
- RTL and LTR in Windows Live Writer
- Silverlight controls library
- Snipping tool integration plugin for WLW
- USB FM receiver library
- Vista Battery Saver
- WebCam control for WPF
- Windows Live SkyDrive attachment for Windows Live Writer
- Wireless Migrator
- WPF Virtual Keyboard



