-
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
-
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
-
You can also use BitBlt with the vbWhiteness constant to set the background all to white. Wouldn't thta be faster, Youngbuck?
-
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!
-
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.
-
That's correct. But you can also simply set the .BackColor property to another value, not?
-
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?
-
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
-
Well Balder, I think YoungBuck just gave the answer...
-
Yeah, that´s pretty much what I was looking for, thx ya all :)
-
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
-
...using my vB tool your code would look wayyy better :) Download now!