Hello All-
I am trying to save an image of the screen as a JPEG. Through testing I know that my code will grab a device-dependent bitmap of the screen and convert it into a device-independent bitmap. However, when I attempt to save the DIB as a JPG using the Intel JPEG library, I get a scrambled image saved and the ijlWrite() command returns -1, which is documented as "Exception Encountered". From what I understand, the code should be generating an upside-down full size image of the desktop. I would be grateful for any help.
Thanks!
--Rob
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
I am trying to save an image of the screen as a JPEG. Through testing I know that my code will grab a device-dependent bitmap of the screen and convert it into a device-independent bitmap. However, when I attempt to save the DIB as a JPG using the Intel JPEG library, I get a scrambled image saved and the ijlWrite() command returns -1, which is documented as "Exception Encountered". From what I understand, the code should be generating an upside-down full size image of the desktop. I would be grateful for any help.
Thanks!
--Rob
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Code:
HWND hDesktop;
HDC hDesktopDC, hCompatDC;
HBITMAP hBmpScreen, hBmpA;
LPBITMAPINFO lpbmiDIBInfo = (LPBITMAPINFO)(new char[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)]);
int iScreenWidth, iScreenHeight;
LPVOID lpvDIBBytes;
JPEG_CORE_PROPERTIES *pjcpJPEG;
iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
iScreenHeight = GetSystemMetrics(SM_CYSCREEN);
hDesktop = ::GetDesktopWindow();
hDesktopDC = ::GetDC(hDesktop);
hCompatDC = CreateCompatibleDC(hDesktopDC);
hBmpScreen = CreateCompatibleBitmap(hDesktopDC, iScreenWidth, iScreenHeight);
hBmpA = (HBITMAP) SelectObject(hCompatDC, hBmpScreen);
BitBlt(hCompatDC, 0, 0, iScreenWidth, iScreenHeight, hDesktopDC, 0,0,SRCCOPY);
::ReleaseDC(hDesktop, hDesktopDC);
memset(lpbmiDIBInfo, 0, sizeof(*lpbmiDIBInfo));
pjcpJPEG = new JPEG_CORE_PROPERTIES;
memset(pjcpJPEG, 0, sizeof(JPEG_CORE_PROPERTIES));
lpbmiDIBInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
// Deselect Bitmap for DIb conversion
SelectObject(hCompatDC, hBmpA);
// Retrieve Header Info
GetDIBits(hCompatDC, hBmpScreen, 0, iScreenHeight, NULL, lpbmiDIBInfo, DIB_RGB_COLORS);
lpvDIBBytes = new BYTE[lpbmiDIBInfo->bmiHeader.biSizeImage];
// Create DIB in (void *)
GetDIBits(hCompatDC, hBmpScreen, 0, iScreenHeight, lpvDIBBytes, lpbmiDIBInfo, DIB_RGB_COLORS);
// Clear Bitmap
DeleteDC(hCompatDC);
DeleteObject(hBmpScreen);
// Save JPG from DIB
ijlInit(pjcpJPEG);
pjcpJPEG->DIBWidth = lpbmiDIBInfo->bmiHeader.biWidth;
pjcpJPEG->DIBHeight = lpbmiDIBInfo->bmiHeader.biHeight;
pjcpJPEG->DIBChannels = 3;
pjcpJPEG->DIBSubsampling = IJL_NONE;
pjcpJPEG->DIBColor = IJL_BGR;
pjcpJPEG->DIBBytes = (unsigned char *) lpvDIBBytes;
pjcpJPEG->DIBPadBytes = IJL_DIB_PAD_BYTES(pjcpJPEG->DIBWidth,pjcpJPEG->DIBChannels);
pjcpJPEG->JPGWidth = lpbmiDIBInfo->bmiHeader.biWidth;
pjcpJPEG->JPGHeight = lpbmiDIBInfo->bmiHeader.biHeight;
pjcpJPEG->JPGChannels = 3;
pjcpJPEG->JPGSubsampling = IJL_411;
pjcpJPEG->JPGColor = IJL_YCBCR;
pjcpJPEG->JPGFile = "c:\\test.jpg";
pjcpJPEG->jquality = 100;
ijlWrite(pjcpJPEG, IJL_JFILE_WRITEWHOLEIMAGE);
ijlFree(pjcpJPEG);