BitBlt Graphics to PictureBox

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

MikeJ
04-09-2004, 09:57 AM
Use System.Drawing.Graphics.DrawImage:

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim g As Graphics = e.Graphics
g.DrawImage(New Bitmap("C:\Path\To\Image.ext"), New Point(0, 0))
End Sub

There are other parameters, but this is the basic way to do it. Check out MSDN for more examples.

reboot
04-09-2004, 10:59 AM
There's really no point in using bitblt in .net, it gains you nothing as far as I can tell.

Invictus
04-09-2004, 06:18 PM
Use System.Drawing.Graphics.DrawImage:

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim g As Graphics = e.Graphics
g.DrawImage(New Bitmap("C:\Path\To\Image.ext"), New Point(0, 0))
End Sub

There are other parameters, but this is the basic way to do it. Check out MSDN for more examples.

Correct me if I'm wrong, but that code only loads a bitmap into a Graphics object and displays it on the form. I understand how I get data into a Graphics object, but I don't know how to get data out of one in GDI+.

Let's say I want to load a bitmap onto a Graphics backbuffer, modify it there and copy it to another Graphics object that has been attached to a picture box. Or I want to modify the Graphics object and save the modified image as a bitmap file. The syntax does not allow something like:

Bitmap.DrawImage(Graphics)
'copies from the graphics object to a bitmap
'then
Bitmap.Save(File)
'to save what was on the graphics object to a file

'or

gDisplay.DrawImage(gBackbuffer,0,0)
'copies from gBackbuffer to gDisplay

Can you point me in the right direction?

Iceplug
04-10-2004, 08:50 AM
Let's say I want to load a bitmap onto a Graphics backbuffer, modify it there and copy it to another Graphics object that has been attached to a picture box. Or I want to modify the Graphics object and save the modified image as a bitmap file.
* Let's say I want to load a bitmap onto a Graphics backbuffer:
Dim Bmp As Bitmap = New Bitmap(32, 32)
Dim GFXBmp As Graphics = Graphics.FromBitmap(Bmp)
*modify it there
GFXBmp.DrawWhatever()
*and copy it to another Graphics object that has been attached to a picturebox.
GFXPB.DrawImage(Bmp)
*Or I wanted to modify the Graphics object
GFXBmp.DrawWhatever()
*and save the modified image as a bitmap
Bmp.Save()

The point is that the Graphics object modifies whatever it is aimed at.
To copy an image, copy the image, don't copy the thing that is drawing the image ;). Good luck.

Invictus
04-10-2004, 03:33 PM
Thanks a lot, Iceplug. The "Graphics.FromImage" syntax had me confused. I thought it was a one-way data transfer. :o

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum