Results 1 to 12 of 12

Thread: BitBlt again...

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Sweden
    Posts
    37

    Talking

    I´m goofing around with BitBlt right now and wondering how to get max optimization out of it.
    Right now I erase the "source"-pic with .cls, but is this really the fastest way? Would it be better to blit an empty square over the old source-pic instead?
    Is there any use in refreashing the source?

    This is the (basic) way I figured out:
    .cls source - draw source-pic - blit to dest-pic - refresh dest-pic - and back from the beginning.
    Iv'e seen lots of versions...what do u think?

    Someone said you could store bitmaps in an array for faster transfers with BitBlt, what does that mean?

    Hope you can give me a hint, I´m quite a newbie to this.

    Cheers
    Balder = Viking God
    VB6/VC++ Enterprise Editions

  2. #2
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    Try the FillRect API (this is the work around I am currently using).....

    Code:
    Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
    Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    
    Private Sub Command1_Click()
        Dim rct As RECT
        Dim lBrsh As Long
        
        With rct
            .Right = Picture1.Width / Screen.TwipsPerPixelX
            .Bottom = Picture1.Height / Screen.TwipsPerPixelY
        End With
        
        lBrsh = CreateSolidBrush(Picture1.BackColor)
        
        FillRect Picture1.hdc, rct, lBrsh
        
        DeleteObject lBrsh
    End Sub
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  3. #3
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    You can also use BitBlt with the vbWhiteness constant to set the background all to white. Wouldn't thta be faster, Youngbuck?

  4. #4
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    If you use the .cls way, make sure to have 2 picboxes and bitblt to picbox2.

    Code:
    do
    
    picbox2.cls
    
    '... code here for bitblt (blit to picbox2!!!!!)
    
    rc=bitblt(picbox1.hdc,0,0,picbox2.scalewidth,picbox2.scaleheight,picbox2.hdc,0,0,srccopy)
    
    doevents
    
    loop
    Believe me; don't do it in a loop unless doing DirectDraw!
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  5. #5
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    Using BitBlt with the vbWhiteness constant will definitely be faster but what if you want the background to be a different color than white, that is why I suggested the FillRect API.
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  6. #6
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    That's correct. But you can also simply set the .BackColor property to another value, not?

  7. #7

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Sweden
    Posts
    37
    Hm, I have seen what you have written, thanks for it. But consider I have a certain picture as background, can I still use the vbwhiteness MadCompie? How then?
    Balder = Viking God
    VB6/VC++ Enterprise Editions

  8. #8
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    To get the best performance try storing the picture in an Offscreen Device Context and BitBlting it to the screen when you want to repaint the background instead of using .Cls, here is a quick example ..

    Code:
    Option Explicit
    
    Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
    
    Private lngBkGdDC As Long
    Private iPic As IPictureDisp
    Private lngOrigBitmap As Long
    
    Private Sub Command1_Click()
        
        ClearPictureBox
        
    End Sub
    
    Private Sub Form_Load()
        
        Picture1.ScaleMode = vbPixels
        
        ' create a new DC in memory that is compatible with the picture box's DC
        lngBkGdDC = CreateCompatibleDC(Picture1.hdc)
        
        If Not lngBkGdDC = 0 Then
        
            ' load the picture into memory
            Set iPic = LoadPicture("C:\Windows\Forest.bmp")
        
            ' load the picture into the DC and retrieve the handle to the bitmap _
               created with the DC
            lngOrigBitmap = SelectObject(lngBkGdDC, iPic)
            
        Else
            
            'if offscreen DC could not be created end the app
            Unload Me
            
        End If
        
    End Sub
    
    Public Sub ClearPictureBox()
     
        ' paint the offscreen dc over whatever is currently drawn in _
          the picture box
        BitBlt Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, lngBkGdDC, 0, 0, vbSrcCopy
        
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        
        '**IMPORTANT** Free Memory when the Device is no longer needed
        
        ' put the bitmap orignally created with     DC back into the DC
        SelectObject lngBkGdDC, lngOrigBitmap
        
        ' Delete the Picture from Memory
        DeleteObject iPic
        Set iPic = Nothing
        
        ' Remove the DC from memory
        DeleteDC lngBkGdDC
        
    End Sub
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  9. #9
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    Well Balder, I think YoungBuck just gave the answer...

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Sweden
    Posts
    37
    Yeah, that´s pretty much what I was looking for, thx ya all
    Balder = Viking God
    VB6/VC++ Enterprise Editions

  11. #11
    Lively Member
    Join Date
    Feb 2001
    Posts
    99
    I am also looking for a faster way to .CLS, so this looked like an ideal thread.

    To check out the ideas, I wrote a simple test under a Commandbutton_Click

    XX = Now
    For X = 1 to 10000
    execute YoungBucks FillRest Code
    Next X
    ZZ = Now
    Debug.Print ZZ - XX

    XX = Now
    For X = 1 to 10000
    Picture1.cls
    Next X
    ZZ = Now
    Debug.Print ZZ - XX

    Interestingly both loops took almost identical time. Looks to me like FillRect API (and its setup/teardown) is no better than .CLS

  12. #12
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    ...using my vB tool your code would look wayyy better Download now!

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