1 Attachment(s)
BitBlt'ing one DC into another
OK here's the score, I've created two device contexts and loaded a bitmap using the LoadImage API. I'm now 'selecting' the bitmap into the first DC and the BitBlt'ing it into the second DC but when I try to BitBlt this DC to the form for testing I get nothing.
This is the troublesome code:
VB Code:
'Create a copy of the original bitmap so it can be masked
Dim hbmCopy As Long, hdcCopy As Long
hDC = CreateCompatibleDC(0)
hdcCopy = CreateCompatibleDC(0)
hbmCopy = CreateCompatibleBitmap(0, 16, 16)
'Select the original image
SelectObject hDC, m_hbmMain
'SelectObject hdcCopy, hbmCopy
'BitBlt the original into the copy DC
Debug.Print "copying from ", hDC, "to", hdcCopy
Debug.Print "result:", BitBlt(hdcCopy, 0, 0, 16, 16, hDC, 0, 0, vbSrcCopy)
'See if we can BitBlt our copied one onto the form
Debug.Print "bitblt onto form", BitBlt(Me.hDC, 16, 0, 16, 16, hdcCopy, 0, 0, vbSrcCopy)
'Refresh the form
Me.Refresh
DeleteObject hbmCopy
DeleteDC hdcCopy
DeleteDC hDC
I'll also attach the form incase anyone needs a closer inspection. When the command button is clicked 2 images of an apple should appear in the top-left corner.
Cheers for any help, adehh.
Re: BitBlt'ing one DC into another
Quote:
Originally Posted by adzzzz
OK here's the score, I've created two device contexts and loaded a bitmap using the LoadImage API. I'm now 'selecting' the bitmap into the first DC and the BitBlt'ing it into the second DC but when I try to BitBlt this DC to the form for testing I get nothing.
This is the troublesome code:
VB Code:
'Create a copy of the original bitmap so it can be masked
Dim hbmCopy As Long, hdcCopy As Long
hDC = CreateCompatibleDC(0)
hdcCopy = CreateCompatibleDC(0)
hbmCopy = CreateCompatibleBitmap(0, 16, 16)
'Select the original image
SelectObject hDC, m_hbmMain
'SelectObject hdcCopy, hbmCopy
'BitBlt the original into the copy DC
Debug.Print "copying from ", hDC, "to", hdcCopy
Debug.Print "result:", BitBlt(hdcCopy, 0, 0, 16, 16, hDC, 0, 0, vbSrcCopy)
'See if we can BitBlt our copied one onto the form
Debug.Print "bitblt onto form", BitBlt(Me.hDC, 16, 0, 16, 16, hdcCopy, 0, 0, vbSrcCopy)
'Refresh the form
Me.Refresh
DeleteObject hbmCopy
DeleteDC hdcCopy
DeleteDC hDC
I'll also attach the form incase anyone needs a closer inspection. When the command button is clicked 2 images of an apple should appear in the top-left corner.
Cheers for any help, adehh.
Take a look at my code in http://www.planetsourcecode.com/vb/s...57119&lngWId=1
Re: BitBlt'ing one DC into another
I can't find nothing in your code where you are bitblting one DC into another. This is proper badly doing my head in now because I don't understand why it wont work.
I've tried doing it into a picture box then bitblting this picture box into another and that works fine, so why won't it work fine if I create the DC's myself?
Re: BitBlt'ing one DC into another
Quote:
Originally Posted by adzzzz
I can't find nothing in your code where you are bitblting one DC into another. This is proper badly doing my head in now because I don't understand why it wont work.
I've tried doing it into a picture box then bitblting this picture box into another and that works fine, so why won't it work fine if I create the DC's myself?
Ok, i've been lookign at this for a while and foolishly i didnt read the last line of your comments, i cant seme to finda way at the moment i'll kepe trying though.
removed attactchment solved below
Re: BitBlt'ing one DC into another
Ok done it :) I've spent a while trying this out and stuff so i hope its what you need
VB Code:
Private Sub Command2_Click()
Dim hBitmap As Long
Dim hDC As Long
'create dc
hDC = CreateCompatibleDC(GetDC(0))
'load imahe
hBitmap = LoadImage(App.hInstance, App.Path & "\apple.bmp", 0, 16, 16, LR_LOADFROMFILE)
'select
SelectObject hDC, hBitmap
'Copy the picture on to the memory DC
BitBlt hDC, 0, 0, 16, 16, hBitmap, 0, 0, vbSrcCopy
'Now copy it back to the Form
BitBlt Me.hDC, 0, 0, 16, 16, hDC, 0, 0, vbSrcCopy
'refresh
Me.Refresh
'and delete
DeleteObject hBitmap
End Sub
Add that to your command click :)
Let me know if this doesnt or does help :)
edit - make sure autoredraw is set to true :)
Re: BitBlt'ing one DC into another
Pino that's practically exactly the same thing I'm trying to do apart from my bitmap handle resides in a global variable and it's never deleted, so why isn't it working?
I need the bitmap handle in a global variable btw because I first started this on a game so I don't want to be loading the image for each frame.
Also I nearly got majorly confused by your code but realised it isnt actually copying anything, if you see the code below I have commented out one line and it still functions OK. It works to a certain extent but I need to copy it to another memory DC after the select statement and then use this new copied DC to draw my objects on screen.
VB Code:
Private Sub Command2_Click()
Dim hDC As Long, hBitmap As Long
'create dc
hDC = CreateCompatibleDC(GetDC(0))
'load imahe
hBitmap = LoadImage(App.hInstance, App.Path & "\apple.bmp", 0, 16, 16, LR_LOADFROMFILE)
'select
'This line puts the bitmap into the DC
SelectObject hDC, hBitmap
'Copy the picture on to the memory DC
'BitBlt hDC, 0, 0, 16, 16, hBitmap, 0, 0, vbSrcCopy
'Now copy it back to the Form
BitBlt Me.hDC, 0, 0, 16, 16, hDC, 0, 0, vbSrcCopy
'refresh
Me.Refresh
'and delete
DeleteObject hBitmap
DeleteDC hDC
End Sub
Re: BitBlt'ing one DC into another
Well ! You don't saw anithing in my code in order to met your needs ! Only because I do a BitBlt inside a memory DC and then, BitBlt it back to a OCX.hDC ! They are always DC's ! Anyway this doesn't work for you ! Let's try another thing ! Have you tried to use CopyMemory ? This functions maybe can do what BitBlt - in this case - can't ! I don't know if it will work, but, maybe !
Re: BitBlt'ing one DC into another
Make sure that CreateCompatibleBitmap is successfull. For me, the function failed using the desktop dc (no idea why) but worked using the Form's dc
hbmCopy = CreateCompatibleBitmap(Me.hdc, 16, 16)
This line needs to be uncommented. The bitmap must be selected into the dc
SelectObject hdcCopy, hbmCopy
Re: BitBlt'ing one DC into another
Did you manage to fix this?
Re: BitBlt'ing one DC into another
With those two changes to the code everything worked fine for me.