Results 1 to 10 of 10

Thread: BitBlt'ing one DC into another

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506

    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:
    1. 'Create a copy of the original bitmap so it can be masked
    2.   Dim hbmCopy As Long, hdcCopy As Long
    3.  
    4.   hDC = CreateCompatibleDC(0)
    5.   hdcCopy = CreateCompatibleDC(0)
    6.    
    7.   hbmCopy = CreateCompatibleBitmap(0, 16, 16)
    8.  
    9.   'Select the original image
    10.   SelectObject hDC, m_hbmMain
    11.   'SelectObject hdcCopy, hbmCopy
    12.  
    13.   'BitBlt the original into the copy DC
    14.   Debug.Print "copying from ", hDC, "to", hdcCopy
    15.   Debug.Print "result:", BitBlt(hdcCopy, 0, 0, 16, 16, hDC, 0, 0, vbSrcCopy)
    16.  
    17.   'See if we can BitBlt our copied one onto the form
    18.   Debug.Print "bitblt onto form", BitBlt(Me.hDC, 16, 0, 16, 16, hdcCopy, 0, 0, vbSrcCopy)
    19.  
    20.   'Refresh the form
    21.   Me.Refresh
    22.  
    23.   DeleteObject hbmCopy
    24.   DeleteDC hdcCopy
    25.   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.
    Attached Files Attached Files

  2. #2
    Addicted Member Abilio's Avatar
    Join Date
    May 2003
    Location
    Aveiro - Portugal
    Posts
    222

    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:
    1. 'Create a copy of the original bitmap so it can be masked
    2.   Dim hbmCopy As Long, hdcCopy As Long
    3.  
    4.   hDC = CreateCompatibleDC(0)
    5.   hdcCopy = CreateCompatibleDC(0)
    6.    
    7.   hbmCopy = CreateCompatibleBitmap(0, 16, 16)
    8.  
    9.   'Select the original image
    10.   SelectObject hDC, m_hbmMain
    11.   'SelectObject hdcCopy, hbmCopy
    12.  
    13.   'BitBlt the original into the copy DC
    14.   Debug.Print "copying from ", hDC, "to", hdcCopy
    15.   Debug.Print "result:", BitBlt(hdcCopy, 0, 0, 16, 16, hDC, 0, 0, vbSrcCopy)
    16.  
    17.   'See if we can BitBlt our copied one onto the form
    18.   Debug.Print "bitblt onto form", BitBlt(Me.hDC, 16, 0, 16, 16, hdcCopy, 0, 0, vbSrcCopy)
    19.  
    20.   'Refresh the form
    21.   Me.Refresh
    22.  
    23.   DeleteObject hbmCopy
    24.   DeleteDC hdcCopy
    25.   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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506

    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?

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    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
    Last edited by Pino; Feb 18th, 2005 at 05:12 PM.

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    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:
    1. Private Sub Command2_Click()
    2. Dim hBitmap As Long
    3. Dim hDC As Long
    4.    
    5.    'create dc
    6.    hDC = CreateCompatibleDC(GetDC(0))
    7.    'load imahe
    8.    hBitmap = LoadImage(App.hInstance, App.Path & "\apple.bmp", 0, 16, 16, LR_LOADFROMFILE)
    9.    'select
    10.    SelectObject hDC, hBitmap
    11.    
    12.         'Copy the picture on to the memory DC
    13.         BitBlt hDC, 0, 0, 16, 16, hBitmap, 0, 0, vbSrcCopy
    14.            
    15.         'Now copy it back to the Form
    16.         BitBlt Me.hDC, 0, 0, 16, 16, hDC, 0, 0, vbSrcCopy
    17.        
    18.         'refresh
    19.         Me.Refresh
    20.      'and delete
    21. DeleteObject hBitmap
    22. 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
    Last edited by Pino; Feb 18th, 2005 at 05:17 PM.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506

    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:
    1. Private Sub Command2_Click()
    2. Dim hDC As Long, hBitmap As Long
    3.    
    4.    'create dc
    5.    hDC = CreateCompatibleDC(GetDC(0))
    6.    'load imahe
    7.    hBitmap = LoadImage(App.hInstance, App.Path & "\apple.bmp", 0, 16, 16, LR_LOADFROMFILE)
    8.    'select
    9.    
    10.    'This line puts the bitmap into the DC
    11.    SelectObject hDC, hBitmap
    12.    
    13.         'Copy the picture on to the memory DC
    14.         'BitBlt hDC, 0, 0, 16, 16, hBitmap, 0, 0, vbSrcCopy
    15.            
    16.         'Now copy it back to the Form
    17.         BitBlt Me.hDC, 0, 0, 16, 16, hDC, 0, 0, vbSrcCopy
    18.        
    19.         'refresh
    20.         Me.Refresh
    21.      'and delete
    22.  
    23.   DeleteObject hBitmap
    24.   DeleteDC hDC
    25. End Sub

  7. #7
    Addicted Member Abilio's Avatar
    Join Date
    May 2003
    Location
    Aveiro - Portugal
    Posts
    222

    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 !

  8. #8
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    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

  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: BitBlt'ing one DC into another

    Did you manage to fix this?

  10. #10
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: BitBlt'ing one DC into another

    With those two changes to the code everything worked fine for me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width