Want to do some testing with BitBlt but cannot get it to work.

aewarnick

Well-known member
Joined
Jan 29, 2003
Messages
1,031
Could someone post an example of how to draw on my form using BitBlt. Here is what I have so far:
C#:
IntPtr offscreenDC= a.Api.CreateCompatibleDC(e.Graphics.GetHdc());
a.Api.SelectObject(offscreenDC, B.GetHbitmap());
a.Api.BitBlt(e.Graphics.GetHdc(), this.posRec.X, this.posRec.Y, this.posRec.Width, this.posRec.Height, offscreenDC, 0, 0, SrcCopy);
e.Graphics.ReleaseHdc(e.Graphics.GetHdc());
a.Api.DeleteDC(offscreenDC);
that is done in the overriden OnPaint event. B is my Image.
I always get the error:
The object is currently in use elsewhere.
 
Last edited by a moderator:
Try removing the ReleaseHdc line; it is possible that it knows the DC is in use and will not let you release it yet.
 
I got it. Here was the problem:
e.Graphics.ReleaseHdc(e.Graphics.GetHdc());
I had to put the hDC into an IntPtr variable when I called GetHdc().
IntPtr hDC= e.Graphics.GetHdc();
e.Graphics.ReleaseHdc(hDC);
Thanks VF!
 
Yeah, sorry for being unclear; the problem is that when you call GetHdc(), it extracts the hDC, locking it, and allows you to use it, it doesnt simply return the handle. When you try to call ReleaseHdc(GetHdc()) you are trying to retrieve yet another Hdc and then release that one.
 
Either my testing was done wrong or DrawImageUnscaled is alot faster. Here is my method:
C#:
public static void DrawBitBlt(Graphics g, IntPtr hObject, Rectangle destRec)
{
	IntPtr hDC= g.GetHdc();
	IntPtr offscreenDC= a.Api.CreateCompatibleDC(hDC);
	a.Api.SelectObject(offscreenDC, hObject);
	a.Api.BitBlt(hDC, destRec.X, destRec.Y, destRec.Width, destRec.Height, offscreenDC, 0, 0, SrcCopy);
	a.Api.DeleteDC(offscreenDC);
	a.Api.DeleteObject(hObject);
	g.ReleaseHdc(hDC);
}
I am using the System.Timers class timer, setting its Interval to 1 and Incrementing a static int variable by one each time the Elapsed event is called.

Am I testing right?
 
In my tests I used the timeGetTime() API;

I did it like this:

C#:
int start, end;

start = timeGetTime();
for (int i = 0; i < 2000; i++) {
  // BitBlt here
}
end = timeGetTime()

Label1.Caption = "The BitBlt process took: " + Convert.ToString(end - start);
 
I took this line out:
a.Api.DeleteObject(hObject);
and now I have BLAZING speed!!
An approximate 3 seconds for BitBlt and 7 for DrawImageUnscaled!

Was it ok to take that line out?
 
Back
Top