I have tried htis and it is MUCH faster, exactly what I wanted however I
have encountered a problem with dynamic images where only a black square is
blitted accross but an image contained within a PictureBox placed onto the
form at design time works fine. The code that works is
Private Enum TernaryRasterOperations As Integer
SRCCOPY = &HCC0020 ' dest = source */
SRCPAINT = &HEE0086 ' dest = source OR dest */
SRCAND = &H8800C6 ' dest = source AND dest */
SRCINVERT = &H660046 ' dest = source XOR dest */
SRCERASE = &H440328 ' dest = source AND (NOT dest ) */
NOTSRCCOPY = &H330008 ' dest = (NOT source) */
NOTSRCERASE = &H1100A6 ' dest = (NOT src) AND (NOT dest) */
MERGECOPY = &HC000CA ' dest = (source AND pattern) */
MERGEPAINT = &HBB0226 ' dest = (NOT source) OR dest */
PATCOPY = &HF00021 ' dest = pattern */
PATPAINT = &HFB0A09 ' dest = DPSnoo */
PATINVERT = &H5A0049 ' dest = pattern XOR dest */
DSTINVERT = &H550009 ' dest = (NOT dest) */
BLACKNESS = &H42 ' dest = BLACK */
WHITENESS = &HFF0062 ' dest = WHITE */
End Enum
Private Declare Auto Function BitBlt Lib "GDI32.DLL" (ByVal hObject As
IntPtr, ByVal XDest As Integer, ByVal YDest As Integer, ByVal Width As
Integer, ByVal Height As Integer, ByVal ObjSource As IntPtr, ByVal XSrc As
Integer, ByVal YSrc As Integer, ByVal Operation As TernaryRasterOperations)
As Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
' Load image into picture box
PictureBox1.Image = Image.FromFile("Image.bmp")
System.Windows.Forms.Application.DoEvents()
' Obtain graphics objects
Dim FormGraphics As Graphics = CreateGraphics(), ImageGraphics As
Graphics = PictureBox1.CreateGraphics
' Obtain HDC's
Dim FormHDC As IntPtr = FormGraphics.GetHdc, ImgHDC As IntPtr =
ImageGraphics.GetHdc
' Draw image onto the form
BitBlt(FormHDC, 0, 0, 50, 50, ImgHDC, 0, 0,
TernaryRasterOperations.SRCCOPY)
' Release the HDC's
FormGraphics.ReleaseHdc(FormHDC)
ImageGraphics.ReleaseHdc(ImgHDC)
' Relase the graphics objects
FormGraphics.Dispose()
ImageGraphics.Dispose()
End Sub
That works very well and is fast enough to have 1000 images running around
the screen at 40 frames per second and taking up very little processing
power. However when i try to use a dynamically generated Image object or
PictureBox object all that gets drawn on the screen is a black box. This is
the code I used:
Private Enum TernaryRasterOperations As Integer
SRCCOPY = &HCC0020 ' dest = source */
SRCPAINT = &HEE0086 ' dest = source OR dest */
SRCAND = &H8800C6 ' dest = source AND dest */
SRCINVERT = &H660046 ' dest = source XOR dest */
SRCERASE = &H440328 ' dest = source AND (NOT dest ) */
NOTSRCCOPY = &H330008 ' dest = (NOT source) */
NOTSRCERASE = &H1100A6 ' dest = (NOT src) AND (NOT dest) */
MERGECOPY = &HC000CA ' dest = (source AND pattern) */
MERGEPAINT = &HBB0226 ' dest = (NOT source) OR dest */
PATCOPY = &HF00021 ' dest = pattern */
PATPAINT = &HFB0A09 ' dest = DPSnoo */
PATINVERT = &H5A0049 ' dest = pattern XOR dest */
DSTINVERT = &H550009 ' dest = (NOT dest) */
BLACKNESS = &H42 ' dest = BLACK */
WHITENESS = &HFF0062 ' dest = WHITE */
End Enum
Private Declare Auto Function BitBlt Lib "GDI32.DLL" (ByVal hObject As
IntPtr, ByVal XDest As Integer, ByVal YDest As Integer, ByVal Width As
Integer, ByVal Height As Integer, ByVal ObjSource As IntPtr, ByVal XSrc As
Integer, ByVal YSrc As Integer, ByVal Operation As TernaryRasterOperations)
As Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
' Load new image
Dim newImage As Image = Image.FromFile("Image.bmp")
' Obtain graphics objects
Dim FormGraphics As Graphics = CreateGraphics(), ImageGraphics As
Graphics = Graphics.FromImage(newImage)
' Obtain HDCs
Dim FormHDC As IntPtr = FormGraphics.GetHdc, ImgHDC As IntPtr =
ImageGraphics.GetHdc
' Draw image onto the form
BitBlt(FormHDC, 0, 0, 50, 50, ImgHDC, 0, 0,
TernaryRasterOperations.SRCCOPY)
' Release the HDC's
FormGraphics.ReleaseHdc(FormHDC)
ImageGraphics.ReleaseHdc(ImgHDC)
' Relase the graphics objects
FormGraphics.Dispose()
ImageGraphics.Dispose()
End Sub
The only difference between the two is the image object and the creation of
the graphics object. I have tried to draw the image on the graphics object
after it is created but that had no effect. If anyone can help me figure
this out I would appreciate it.