BitBlt doesn't work fine. It copies from screen instead of (invisible) window

Posted on 2005-05-08
Last Modified: 2012-05-05
Hello experts!

I'm trying to make a game in, and therefore I have a big bmp file with all small pictures on them (game objects). Now I want to copy pieces of that bmp to use it as tiles in my game. The bmp is on a form in a picturebox and is not visible at the moment of starting up (but it is loaded correctely, I can display it if I want to).
I use the code from here:

Here is a snap of my code,the copy rect function comes from the site above and I've put it
in a module called DrawCommands.
AllGraphics is the form where the 'big' image is on and 'ObjectMaster' is the picturebox.
'myMainForm' is the destinationform , also with a picturebox on it.
       Dim rect As RectangleF
        Dim gr As AllGraphics
        gr = New AllGraphics

        rect = New RectangleF(0, 0, 50, 500)

        ' bm is of type Bitmap
        bm = DrawCommands.copyRect(gr.ObjectMaster, rect)

        myMainForm.thePictureBox.Image = CType(bm, Bitmap).Clone
    End Sub
The problem is that when copying, instead of copying from the image, stuff gets copied from my screen or what is visible at the moment.But not from my form. The form is loaded correctly because when I show it on screen with gr.showDialog I can see it fine.

The second problem is that if I don't put the myMainForm.showDialog after the draw commands,
it doesn't draw a thing in the box.

And to be complete, here is the code from' DrawCommands.copyRect(gr.ObjectMaster, rect)'

Module DrawCommands
    Declare Auto 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 Int32) As Boolean

    ' Constants for Raster Operations used by BitBlt function.
    Public Const SRCAND = &H8800C6 ' dest = source AND dest
    Public Const SRCCOPY = &HCC0020 ' dest = source
    Public Const SRCPAINT = &HEE0086 ' dest = source OR dest
    ' This function copies a rectangle from source and returns the bitmap
    Public Function copyRect(ByVal src As PictureBox, _
           ByVal rect As RectangleF) As Bitmap        

        'Get a Graphics Object from the form
        Dim srcPic As Graphics = src.CreateGraphics
        'Create a EMPTY bitmap from that graphics
        Dim srcBmp As New Bitmap(src.Width, src.Height, srcPic)
        'Create a Graphics object in memory from that bitmap
        Dim srcMem As Graphics = Graphics.FromImage(srcBmp)

        'get the IntPtr's of the graphics
        Dim HDC1 As IntPtr = srcPic.GetHdc
        'get the IntPtr's of the graphics
        Dim HDC2 As IntPtr = srcMem.GetHdc        

        'get the picture
        BitBlt(HDC2, 0, 0, rect.Width, _
          rect.Height, HDC1, rect.X, rect.Y, SRCCOPY)  '13369376)
        'Clone the bitmap so we can dispose this one
        copyRect = srcBmp.Clone()

        'Clean Up
    End Function
End Module

Does anybody hav a clue why the image isn't drawn correctely ?

thanks a lot!

Question by:themuppeteer
    LVL 2

    Author Comment

    guess this is the anwser, I'll try it tonight...


    Expert Comment

    for those who are interested:
    the above link solved my problem (check out the forum below the article)

    LVL 2

    Author Comment

    woops by autologin accident used my girlfriend's account..


    Accepted Solution

    PAQed with points refunded (115)

    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now