Capture Screen memory to a bitmap (visual c++ 6.0) - c++

I am using visual c++ 6.0 and need to capture the image on the screen to memory. How do I do this? thx

Get the DC for the screen with GetDC(NULL)
Create a memory DC with CreateCompatibleDC.
Create a bitmap compatible with the screen DC and the same size as the screen.
Select the bitmap into the memory DC.
BitBlt from the screen DC to the memory DC.
Deselect the bitmap from the memory DC.
Use GetBitmapBits to copy the bitmap into memory.

Related

How to create Gdiplus::Bitmap from CDC

I am trying to create a Gdiplus::Bitmap from a device context. The function I use is:
Bitmap bitmap((HBITMAP)myDC.GetCurrentBitmap(), (HPALETTE)myDC.GetCurrentPalette());
...but when I draw the bitmap on the screen, all I see is a black rectangle. I think I'm using the Bitmap constructor wrong, because in docs it's written:
Do not pass to the GDI+ Bitmap::Bitmap constructor a GDI bitmap or a
GDI palette that is currently (or was previously) selected into a
device context.
But I have no idea how to go around this. Another approach I tried is using:
To capture the preexisting image from a window, a Windows Graphics Device Interface (GDI) function such as BitBlt() or StretchBlt() would have to be used to copy the image from the screen to a memory bitmap. This memory bitmap could then be used in the overloaded Bitmap constructor, which takes an HBITMAP as a parameter.
But I couldn't achieve this either.

Convert HDC to 32bit bitmap [duplicate]

How could you copy the contents of an HDC to a bitmap?
Off the top of my head I think you need to:
Create a new DC compatible with the source DC. Call this the memory DC.
Create a new bitmap of the correct size.
Select the bitmap into the memory DC.
BitBlt the source DC into the memory DC.
The bitmap should now contain a copy of the source DC.
I'm at home so can't give you any code, so I hope this is enough to get you started. There is a good GDI section on Code Project.
http://www.codeproject.com/KB/graphics/
There is a good piece of sample code here that does just that (amongst other things).
I've used a similar technique before (many moons ago), but do not have the code to hand.

Convert single font character into bitmap pixel array in c++

I have a problem converting a character into a bitmap pixel array.
I work with Windows CE 6 and I have written an application in C++ to manage an OLED by serial interface.
To display the image on OLED I use the bitmap format, then if I want to display a text on OLED I have to convert the character to a bitmap array first, but I don't know how take a single character from a Windows CE font and convert it into a bitmap pixel array (bytes) to send it out via the serial interface.
Have you got an idea (standard functions, custom functions etc) how to solve this problem?
CreateDC to create a device context.
CreateCompatibleBitmap to create a bitmap of the right size.
SelectObject to select the bitmap into the DC.
CreateFontIndirect to create font.
SelectObject to select the font into the DC.
DrawText to draw the character on the DC.
SelectObject to select the previously selected bitmap back into the DC.
SelectObject to select the previously selected font back into the DC.
DeleteObject to delete the created font.
DeleteDC to delete the DC.
You now have a bitmap with the letter.

CreateCompatibleBitmap Vs SelectObject

I'm wondering , if I want to create different bitmaps,
I use the CreateCompatibleBitmap function again and again, to associate it to the same memory CDC.
is it the same meaning that I CreateBitmap and SelectObject again and again??
I ask this question because I want to do something to the newly created bitmap by another CDC.
Without seeing the specific code it's hard to know the exact problem but CreateCompatibleBitmap is commonly used in double-buffering situations to avoid flickering. Rather than drawing directly to the Device Context (DC) you first draw to an off-screen, or memory, DC which is basically drawing to a bitmap. The bitmap is then copied directly to the screen DC using BitBlt, so it appears like all the drawing happens at the same time.
The usual steps are this (and will probably happen on every WM_PAINT):
Use the screen DC to create a bitmap, which is 'compatible' with it, using CreateCompatibleBitmap.
Create a memory DC
Select the bitmap into the memory DC (this is what you'll draw to)
When drawing is finished BitBlt the memory DC's bitmap onto the screen DC.
More information available here: Guide to Win32 Memory DC (Code Project)
Yes, CreateCompatibleBitmap() creates a new bitmap, a new memory allocation, a new handler each time you call it in a loop;

GDI+ only draws monochrome on memory DC

I'm trying to do some double buffering in an MFC application and trying to draw on the memory DC with GDI+. However, although I called CreateCompatibleDC(), I'm only getting a monochrome image. Here is the code:
CDC bufferDC;
CBitmap bufferBitmap;
bufferDC.CreateCompatibleDC(&dc);
bufferBitmap.CreateCompatibleBitmap(&bufferDC, 300, 300);
bufferDC.SelectObject(bufferBitmap);
Graphics g(bufferDC);
g.Clear(Color::Green);
dc.BitBlt(0, 0, 300, 300, &bufferDC, 0, 0, SRCCOPY);
Instead of a green patch, I see a rectangle of dithered black and white dots. I even tried to save the bitmap to disk after the g.Clear() call. It is indeed a 1-bit depth file.
Any ideas what went wrong? Thanks.
A common mistake. A memory DC takes on the properties of the bitmap selected into it, no matter what compatibility it was created with. The default bitmap selected into a DC is monochrome. If you create a bitmap compatible with that DC, it will be monochrome too.
Create the bitmap to be compatible with the original DC, not the memory DC.
Both the bitnmap and the bufferDC should be compatible with dc (whatever device it refers to), not the bitmap compatible ... with its own DC.
Try to give &dc to CreateCopmpatibleBitmap.
Your code snippet does not show where the dc variable comes from. ThIs guy probably contains a monochrome bitmap, the default. You dont need it anyway. Instead, pass NULL to CreateCompatibleDC and it will be the same format as your display, which is probably color.