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

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

I'm trying to make a game in VB.net, 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: http://www.codeproject.com/vb/net/BitBlt.asp

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
  • 2

Author Comment

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



Expert Comment

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


Author Comment

ID: 14228801
woops by autologin accident used my girlfriend's account..


Accepted Solution

modulo earned 0 total points
ID: 14251235
PAQed with points refunded (115)

Community Support Moderator

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Integration Management Part 2
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

829 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