BitBlt()
Can anyone shed some light as to how this function works? I''ve seen it a lot, but I''m still not sure how it''s used. I looked it up in the MSDN and it said you send it a handle to a source DC and a destination DC, and then it will copy over "bit-block transfer of the color data"
My question is this: how do I write to my source DC, so that I can transfer it over? I can draw on it with SetPixel and other GDI drawing functions, but I don''t think that''s how you transfer
Any ideas?
Peon
Peon
BitBlt is useful for drawing images. Drawing lines, pixels, etc can be done to the destination DC directly, i.e.
WM_PAINT handler: // no error checking
PAINTSTRUCT ps;
HDC dest_dc = BeginPaint (window_handle, &ps);
MoveToEx (dest_dc, x, y, NULL);
LineTo (dest_dc, x, y);
...etc...
EndPaint (window_handle, &ps);
To draw an image using BitBlt:
// insert in the ...etc... section
HDC source_dc = CreateCompatibleDC (dest_dc); // DC''s need to be similar to do a Blt
SelectObject (source_dc, your_bitmap);
BitBlt (......);
DeleteDC (source_dc);
To speed things up, you could create your source_dc and select the bitmap elsewhere but that will consume system resources.
Skizz
WM_PAINT handler: // no error checking
PAINTSTRUCT ps;
HDC dest_dc = BeginPaint (window_handle, &ps);
MoveToEx (dest_dc, x, y, NULL);
LineTo (dest_dc, x, y);
...etc...
EndPaint (window_handle, &ps);
To draw an image using BitBlt:
// insert in the ...etc... section
HDC source_dc = CreateCompatibleDC (dest_dc); // DC''s need to be similar to do a Blt
SelectObject (source_dc, your_bitmap);
BitBlt (......);
DeleteDC (source_dc);
To speed things up, you could create your source_dc and select the bitmap elsewhere but that will consume system resources.
Skizz
skizz: Okay, well that confirmed some of the stuff I had thought out bitBlt(), that it was to draw images in such. I looked over the example Skizz provided, but part of my original problem remains -- I''m still at a loss as to how I can actually GET anything on the source DC in the first place.
Peon
Peon
Peon
The source_dc in my example code behaves exactly like a screen DC - you can use all the GDI functions to put stuff on it. However, you will first need to create a bitmap that the DC can draw into. CreateCompatibleBitmap will do this.
source_dc = CreateCompatibleDC (screen_dc);
source_bitmap = CreateCompatibleBitmap (screen_dc, width, height);
SelectObject (source_dc, source_bitmap); // DC can now be written to
// for example
MoveToEx (source_dc, 10, 10, NULL);
LineTo (source_dc, 20, 10);
RECT draw_rect = {20, 20, 80, 40};
DrawText (source_dc, "some text", -1, draw_rect, DT_TOP | DT_LEFT);
// and copy to screen
BitBlt (screen_dc, 0, 0, width, height, source_dc, 0, 0, SRCCOPY);
Skizz
source_dc = CreateCompatibleDC (screen_dc);
source_bitmap = CreateCompatibleBitmap (screen_dc, width, height);
SelectObject (source_dc, source_bitmap); // DC can now be written to
// for example
MoveToEx (source_dc, 10, 10, NULL);
LineTo (source_dc, 20, 10);
RECT draw_rect = {20, 20, 80, 40};
DrawText (source_dc, "some text", -1, draw_rect, DT_TOP | DT_LEFT);
// and copy to screen
BitBlt (screen_dc, 0, 0, width, height, source_dc, 0, 0, SRCCOPY);
Skizz
Ohh.. I see. I can''t try this now as I''m sitting in history class right now but I assume that if I draw on this source DC, it won''t appear on the screen, correct? The reason I ask is that I''ve always understood (or thought, rather) that bitBlt() is used primarily for "page flipping" with GDI based graphics. Is this "understanding" correct?
Thanks for the help Skizz
Peon
Thanks for the help Skizz
Peon
Peon
Yes, drawing on a memory DC will not produce anything visible on the screen.
BitBlt is primarily used to copy the image, or pattern brush, in one DC to another DC (usually of a different size). The BitBlt function wasn''t designed with "page flipping" as the main goal - there''s too much flexibility in the blitting options (it can copy brushes, PATCOPY, as well as images, SRCCOPY, as well as perform blending operations). BitBlt can be used to implement a "page flipping" effect, although it may not be the most efficient way to do it since you may be copying data from system RAM to video RAM. If speed is an issue then look at the DirectDraw API, otherwise BitBlt will do the job well enough.
Skizz
BitBlt is primarily used to copy the image, or pattern brush, in one DC to another DC (usually of a different size). The BitBlt function wasn''t designed with "page flipping" as the main goal - there''s too much flexibility in the blitting options (it can copy brushes, PATCOPY, as well as images, SRCCOPY, as well as perform blending operations). BitBlt can be used to implement a "page flipping" effect, although it may not be the most efficient way to do it since you may be copying data from system RAM to video RAM. If speed is an issue then look at the DirectDraw API, otherwise BitBlt will do the job well enough.
Skizz
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement