Hello All,
I've been working on this for several days now and could use some direction
so thanks in advace for any help.
I have a COM method which needs "the handle to the device context for the
bitmap." I'm guessing that it's expecting a DIB but I'm not certain. If so,
how do I create a DIB from a GDI+ bitmap?
Here's the code I've written so far for creating the device context. But
the result is a reference to an empty bitmap (regardless of the DIB issue).
Public OSDBitmap As Bitmap
Public OSDGraphics As Graphics
Public Function GetHDCFromBitmap() As IntPtr
OSDBitmap = New Bitmap("c:\testing.dib")
Dim r As New Rectangle(0, 0, OSDBitmap.Width, OSDBitmap.Height)
OSDBitmap.LockBits(r, Imaging.ImageLockMode.ReadWrite,
OSDBitmap.PixelFormat)
OSDGraphics = Graphics.FromImage(OSDBitmap)
'just for testing
OSDGraphics.FillRectangle(New SolidBrush(Color.Red), 10, 10, 50, 50)
Me.pbOSD.Image = bm
'end testing
Dim h1 As GCHandle = GCHandle.Alloc(bm, GCHandleType.Normal)
Dim hBitmap As IntPtr = bm.GetHbitmap()
Dim hdcBmp, hbmOld As IntPtr
Dim h2 As GCHandle = GCHandle.Alloc(OSDGraphics,
GCHandleType.Normal)
Dim hdc As IntPtr = OSDGraphics.GetHdc
hdcBmp = CreateCompatibleDC(hdc)
hbmOld = SelectObject(hdcBmp, hBitmap) 'hbitmap or hdc or hdcbmp?
OSDGraphics.ReleaseHdc(hdc)
If hdcBmp.ToInt32 = 0 Then
Throw New Exception("no hdcBmp")
End If
DeleteObject(hdc)
h1.Free()
h2.Free()
Return hdcBmp
End Function
What am I doing wrong? and do you think I need a DIB?
Thanks,
Tom
James Westgate - 29 Aug 2004 18:23 GMT
Hi Thomas
Have you tried
Graphics g = Graphics.FromImage(image);
IntPtr ptr = g.GetHdc;
James

Signature
Create interactive diagrams and flowcharts with ERM Diagram at
http://www.crainiate.net
Take the ERM Tour at http://www.flowchartcontrol.com
> Hello All,
>
[quoted text clipped - 53 lines]
> Thanks,
> Tom