BITBLT not working?

I have 2 bitmaps in memory; one is an original copy of a picture and the second I intend to block out parts of, and display it.

my bitmaps are named b1 and b2.

b1 is loaded from a file (and converted to a 32bpp bitmap along the way)

b2 is created with the same height, width, pixelformat, horizontal and vertical resolutions. (but empty)

I then attempt to copy part of b1 into b2 using BITBLT. (I don't want to use Graphics.DrawImageUnscaled - the reason being that I intend to copy parts of the image in the future)

I assign b2 to a picturebox, but it displays a blank image.

Here is the code which doesn't seem to work:

pbox2.Image = Nothing

Dim g1 As Graphics = Graphics.FromImage(b1)

Dim hdc1 As IntPtr = g1.GetHdc()

g1.ReleaseHdc(hdc1)

Dim g2 As Graphics = Graphics.FromImage(b2)

Dim hdc2 As IntPtr = g2.GetHdc()

g2.ReleaseHdc(hdc2)

BitBlt(hdc2, 0, 0, b1.Width, b1.Height, hdc1, 0, 0, SRCCOPY)

g1.Dispose()

g2.Dispose()

pbox2.Image = b2

I tried some simple debugging:

changing the last line to read "pbox2.image = b1" displays the original picture.

If I stick g2.Clear(color.Red) before the BITBLT, I get the solid color image. (So G2 is apparently correct)

If I stick g1.Clear(color.Red) before the BITBLT, and change the last line to read "pbox2.image = b1" I get a solid color image. (So G1 is apparently correct)

Moving the ReleaseHDC commands below the BITBLT has no affect. (Other than sometimes causing a "Object is in use elsewhere" error)

substituting known values for b1.width and b1.height has no effect.

Other related code snippets:

Private Declare Function BitBlt& Lib "gdi32.dll" _

(ByVal hdcDest As IntPtr, _

ByVal nXDest As Integer, ByVal nYDest As Integer, _

ByVal nWidth As Integer, ByVal nHeight As Integer, _

ByVal hdcSrc As IntPtr, _

ByVal nXSrc As Integer, ByVal nYSrc As Integer, _

ByVal dwRop As Integer)

Private Const SRCCOPY As Integer = &HCC0020

the handy little "load" routine:

Private Sub loadb1(ByVal f As String)

Dim b3 As Bitmap

b3 = Image.FromFile(f)

If Not (b1 Is Nothing) Then b1.Dispose()

If Not (b2 Is Nothing) Then b2.Dispose()

b1 = New Bitmap(b3.Width, b3.Height, Imaging.PixelFormat.Format32bppArgb)

b1.SetResolution(b3.HorizontalResolution, b3.VerticalResolution)

Dim g As Graphics = Graphics.FromImage(b1)

g.DrawImageUnscaled(b3, 0, 0)

g.Dispose()

b2 = New Bitmap(b3.Width, b3.Height, Imaging.PixelFormat.Format32bppArgb)

b2.SetResolution(b3.HorizontalResolution, b3.VerticalResolution)

b3.Dispose()

End Sub



Answer this question

BITBLT not working?

  • chris441962

    You'd definitely need to move the ReleaseHdc() calls after the BitBlt() call. Other than that, I see no problem. Why do you bother with this The Graphics.DrawImage(Image, Rectangle, Rectangle, GraphicsUnit) overload does what you need...


  • Samer Selo

    The position of the release doesn't seem to make a difference; at least in the short term the HDC stays the same.

    I've always used BITBLT in the past. (Not sure why it didn't work this time; the arguments all seemed okay.)

    Actually, I didn't realize that DrawImage had so many options!

    Yes, that is definitely what I'll use here; I'm always worried I'll have a memory leak if I forget to dispose of something explicitly.

    Sticking with a single graphics handle makes it much easier.

    I tested DrawImage, and it worked like a champ!

    Thanks for the help...


  • rja07

    Any suggestions for using BITBLT to copy from one bitmap in memory to another in memory

    Or perhaps it has to do with the two handles


  • BITBLT not working?