Invictus
04-09-2004, 07:03 AM
Maybe I'm just missing something, but it seems unnecessarily difficult to move data from a Graphics object to A) a Bitmap, B) another Graphics object, C) a Picturebox or D) a file. This is extremely frustrating if you need to manipulate images in memory before displaying them.
After much experimentation, I at least figured out how to copy the contents of a Graphics object with the BitBlt API call. If anyone has a cleaner GDI+ solution, I'd love to see it.
Public Declare Function BitBlt Lib "gdi32.dll" Alias "BitBlt" (ByVal hDestDC As IntPtr, ByVal x As Int32, ByVal y As Int32, ByVal nWidth As Int32, ByVal nHeight As Int32, ByVal hSrcDC As IntPtr, ByVal xSrc As Int32, ByVal ySrc As Int32, ByVal dwRop As Int32) As Int32
Public Enum RasterOps
' Copies the source bitmap to destination bitmap
SRCCOPY = &HCC0020 '
' Combines pixels of the destination with source bitmap using the Boolean AND operator.
SRCAND = &H8800C6
' Combines pixels of the destination with source bitmap using the Boolean XOR operator.
SRCINVERT = &H660046 '
' Combines pixels of the destination with source bitmap using the Boolean OR operator.
SRCPAINT = &HEE0086 '
' Inverts the destination bitmap and then combines the results with the source bitmap
' using the Boolean AND operator.
SRCERASE = &H4400328 '
' Turns all output white.
WHITENESS = &HFF0062 '
' Turn output black.
BLACKNESS = &H42
End Enum
Public Sub CopyGraphicToPicBox(ByVal grSource As Graphics, ByVal picDest As PictureBox)
Dim hSource As IntPtr = grSource.GetHdc
Dim grPicDest As Graphics = picDest.CreateGraphics
Dim hDest As IntPtr = grPicDest.GetHdc
BitBlt(hDest, 0, 0, picDest.Width, picDest.Height, hSource, 0, 0, RasterOps.SRCCOPY)
grPicDest.ReleaseHdc(hDest)
grSource.ReleaseHdc(hSource)
End Sub
After much experimentation, I at least figured out how to copy the contents of a Graphics object with the BitBlt API call. If anyone has a cleaner GDI+ solution, I'd love to see it.
Public Declare Function BitBlt Lib "gdi32.dll" Alias "BitBlt" (ByVal hDestDC As IntPtr, ByVal x As Int32, ByVal y As Int32, ByVal nWidth As Int32, ByVal nHeight As Int32, ByVal hSrcDC As IntPtr, ByVal xSrc As Int32, ByVal ySrc As Int32, ByVal dwRop As Int32) As Int32
Public Enum RasterOps
' Copies the source bitmap to destination bitmap
SRCCOPY = &HCC0020 '
' Combines pixels of the destination with source bitmap using the Boolean AND operator.
SRCAND = &H8800C6
' Combines pixels of the destination with source bitmap using the Boolean XOR operator.
SRCINVERT = &H660046 '
' Combines pixels of the destination with source bitmap using the Boolean OR operator.
SRCPAINT = &HEE0086 '
' Inverts the destination bitmap and then combines the results with the source bitmap
' using the Boolean AND operator.
SRCERASE = &H4400328 '
' Turns all output white.
WHITENESS = &HFF0062 '
' Turn output black.
BLACKNESS = &H42
End Enum
Public Sub CopyGraphicToPicBox(ByVal grSource As Graphics, ByVal picDest As PictureBox)
Dim hSource As IntPtr = grSource.GetHdc
Dim grPicDest As Graphics = picDest.CreateGraphics
Dim hDest As IntPtr = grPicDest.GetHdc
BitBlt(hDest, 0, 0, picDest.Width, picDest.Height, hSource, 0, 0, RasterOps.SRCCOPY)
grPicDest.ReleaseHdc(hDest)
grSource.ReleaseHdc(hSource)
End Sub