Discuss Taking desktop capture? in the C Programming forum on Dev Shed. Taking desktop capture? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Time spent in forums: 5 h 56 m 35 sec
Reputation Power: 11
Taking desktop capture?
What API calls would I look at for taking a capture of the desktop? Also for converting to JPEG, I'v heard about the intel jpeg library, but on their site it's no longer available (I think it is but they charge $?). I have the dll, ijl15.dll.No doc's or any info tho, anyone have any idea how I would use it?
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
Unfortunately, you need to know a bit about the GDI to even do something simple like this. No, I do not have any other links, sorry. And, this also doesn't touch the subject on saving it as a .JPG. Bitmaps are handled (usually) as .BMP files within memory, so it's not a big deal to save them as .BMP. .JPG is another problem. I have not coded anything to do with .JPG, but I do believe that there is support for them in the GDI somewhere... do a search on MSDN, that's the best bet.
Time spent in forums: 3 Days 18 h 33 m 41 sec
Reputation Power: 27
>>What is wrong with this code?
Apart from what JD said it has many GDI memory leaks. The HDC's are not returned to the default state before they are freed. The GDI objects created are not freed.
You will have to write the function CreateBitmapInfoStruct (or I will send it to you as soon as you pay my wages)
// Get bitmap header information
//Use GetObject() to find the bitmap color format, width, and height.
// Convert the color format to a count of bits.
// Allocate memory for the BITMAPINFO structure
// Initialize the fields in the BITMAPINFO structure
// Compute the number of bytes in the array of color indices and store the result in biSizeImage. This is OS dependent ie Win9x re-acts diff to Win NT (DWORD aligned) ect
// Set biClrImportant to 0 (all are iimportant)
Then write a function,CreateBMPFile() to save the bits and a use GDI+ or a third party lib (as I did) to convert to JPG and save
// Retrieve the color table (RGBQUAD array) and the bits (array of palette indices) from the DIB. GetDIBits()
// Compute the size of the entire file & alloc mem
//convert and resize based on the colour depth used on the screen ie display settings ie 24bit BMP has an array of BGR so just convert to RGB. Other colour depths will have to be converted to RGB based on how they stored the colours
//clean up and free mem
//Screen Capture function
hDC=GetDC(NULL);//get screen DC
hdcMem=CreateCompatibleDC(hDC); //create temp DC and find the size
iWidth = GetDeviceCaps(hdc,HORZRES);
iHeight = GetDeviceCaps(hdc,VERTRES);
hImage=CreateCompatibleBitmap(hDC,iWidth,iHeight); // create the BMP
SelectObject(hdcMem,hSysBMP); // capture the image off screen
BMPINFO=CreateBitmapInfoStruct(hWnd, hImage); // fill a BMP header
CreateBMPFile(hWnd, BMPINFO, hImage,hdcMem,pHeader->iCode); // convert and save (send)
// Clean up GDI's
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.
It is completely wrong.
- in the WM_CREATE you cannot use BeginPaint and EndPaint. To get the DC in WM_CREATE you use the GetDC function.
- Don't check for errors when painting in the WM_PAINT, it is completely unnecesary
- Use the CreateIC function instead of CreateDC for the Display. The way you call it you cannot delete the DC and there is a memory leak. It is very important that you clean all objects after use in the WM_PAINT handler BECAUSE GDI RESOURCES IN WINDOWS ARE VERY LIMITED AND AFTER A WHILE YOUR OS WILL BECOME STRANGE AND SLOW, WILL CHANGE ALL FONTS AND BACKGROUNDS, ETC. To see what i am talking about start your app, start task manager, resize multiple times your window and see what is going on in the "Gdi objects" header tab of the processes view in task manager. Your app will quickly create thousands of gdi objects and that is VERY BAD. So cleanup them immediately after finish with them. If you GetDC use ReleaseDC, if you Create a DC use DeleteDC, after finish with each object use DeleteObject
In WM_CREATE use GetDC and ReleaseDC, in WM_PAINT use BeginPaint and EndPaint. Don't check for errors. The functions are pretty simple and you always know if the DC or some object is valid or not