|
|
|
date: Thu, 15 May 2008 15:09:00 -0700,
group: microsoft.public.win32.programmer.gdi
back
(.NET/GDI/BitBlt) Bitmaps will not display correctly.
This code works fine, and displays an image on the PictureBox
Private bmpConv As Bitmap = Nothing
Private bmpBtl As Bitmap = Nothing
Private bmpBtlMask As Bitmap = Nothing
bmpConv = Image.FromFile("..\Images\Conveyor.bmp")
pbConv.BackgroundImage = bmpConv
bmpBtl = Image.FromFile("..\Images\Mist50White.bmp")
pbBtl.BackgroundImage = bmpBtl
bmpBtlMask = Image.FromFile("..Images\Mist50Mask.bmp")
pbBtlMask.BackgroundImage = bmpBtlMask
MyBitBlt(pbConv, pbBtl, pbBtlMask, 0, 0)
-----------------------------------------
Public Sub MyBitBlt(ByVal Dst As PictureBox, ByVal Src As PictureBox,
ByVal Mask As PictureBox, ByVal X As Long, ByRef Y As Long)
' Get device context of source and destination picture boxes
Dim srcHDC As IntPtr = Src.CreateGraphics.GetHdc
Dim dstHDC As IntPtr = Dst.CreateGraphics.GetHdc
Dim maskHDC As IntPtr = Mask.CreateGraphics.GetHdc
BitBlt(dstHDC.ToInt32, X, Y, 50, 50, maskHDC.ToInt32, 0, 0,
TernaryRasterOperations.SRCPAINT)
'put the picture onto the main screen
BitBlt(dstHDC.ToInt32, X, Y, 50, 50, srcHDC.ToInt32, 0, 0,
TernaryRasterOperations.SRCAND)
End Sub
however, the following code doesn't and its using the same images
Private bmpConv As Bitmap = Nothing
Private bmpBtl As Bitmap = Nothing
Private bmpBtlMask As Bitmap = Nothing
bmpConv = Image.FromFile("..\Images\Conveyor.bmp")
pbConv.BackgroundImage = bmpConv
bmpBtl = Image.FromFile("..\Images\Mist50White.bmp")
pbBtl.BackgroundImage = bmpBtl
bmpBtlMask = Image.FromFile("..Images\Mist50Mask.bmp")
pbBtlMask.BackgroundImage = bmpBtlMask
MyBitBlt(pbConv, bmpBtl, bmpBtlMask, 0, 0)
--------------------------------
Public Sub MyBitBlt(ByVal Dst As PictureBox, ByVal Src As Bitmap, ByVal
Mask As Bitmap, ByVal X As Long, ByRef Y As Long)
Try
Dim srcMem As Graphics = Graphics.FromImage(Src)
Dim srcHDC As IntPtr = srcMem.GetHdc
Dim maskMem As Graphics = Graphics.FromImage(Mask)
Dim maskHDC As IntPtr = maskMem.GetHdc
Dim dstHDC As IntPtr = Dst.CreateGraphics.GetHdc
BitBlt(dstHDC, X, Y, 50, 50, maskHDC, 0, 0,
TernaryRasterOperations.SRCPAINT)
'put the picture onto the main screen
BitBlt(dstHDC, X, Y, 50, 50, srcHDC, 0, 0,
TernaryRasterOperations.SRCAND)
srcMem.ReleaseHdc(srcHDC)
maskMem.ReleaseHdc(maskHDC)
srcMem.Dispose()
maskMem.Dispose()
Catch ex As Exception
Dim err As String = ex.Message
End Try
End Sub
It draws nothing on the first bitblt, and a black rectangle on the second
bitblt.
Wondered if anyone had any ideas or insights.
Thanks.
date: Thu, 15 May 2008 15:09:00 -0700
author: Tony (Goody Goody Liquor,Dallas)
And a solution seems to be....
Public Sub MyBitBlt(ByVal Dst As PictureBox, ByVal Back As Bitmap, ByVal Src
As Bitmap, ByVal Mask As Bitmap, ByVal X As Long, ByRef Y As Long)
Try
Dim dstHDC As IntPtr = Dst.CreateGraphics.GetHdc
Dim gdiHandleBack As IntPtr = CreateCompatibleDC(dstHDC.ToInt32)
Dim bmHandleBack As IntPtr = Back.GetHbitmap()
SelectObject(gdiHandleBack, bmHandleBack)
Dim backMem As Graphics = Graphics.FromHdc(gdiHandleBack)
Dim backHDC As IntPtr = backMem.GetHdc
Dim gdiHandleMask As IntPtr = CreateCompatibleDC(dstHDC.ToInt32)
Dim bmHandleMask As IntPtr = Mask.GetHbitmap()
SelectObject(gdiHandleMask, bmHandleMask)
Dim maskMem As Graphics = Graphics.FromHdc(gdiHandleMask)
Dim maskHDC As IntPtr = maskMem.GetHdc
BitBlt(backHDC, X, Y, 50, 50, maskHDC, 0, 0,
TernaryRasterOperations.SRCPAINT)
Dim gdiHandleSrc As IntPtr = CreateCompatibleDC(dstHDC.ToInt32)
Dim bmHandleSrc As IntPtr = Src.GetHbitmap()
SelectObject(gdiHandleSrc, bmHandleSrc)
Dim srcMem As Graphics = Graphics.FromHdc(gdiHandleSrc)
Dim srcHDC As IntPtr = srcMem.GetHdc
BitBlt(backHDC, X, Y, 50, 50, srcHDC, 0, 0,
TernaryRasterOperations.SRCAND)
BitBlt(dstHDC, 0, 0, Back.Width, Back.Height, backHDC, 0, 0,
TernaryRasterOperations.SRCCOPY)
srcMem.ReleaseHdc(srcHDC)
maskMem.ReleaseHdc(maskHDC)
backMem.ReleaseHdc(backHDC)
srcMem.Dispose()
maskMem.Dispose()
backMem.Dispose()
Catch ex As Exception
Dim err As String = ex.Message
End Try
End Sub
...didn't take the time to check for memory leaks.
There may be other more efficient solutions.
date: Fri, 16 May 2008 08:33:00 -0700
author: Tony (Goody Goody Liquor,Dallas)
|
|