Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble saving JPGs using Intel Library

Status
Not open for further replies.

MacLeod72

Programmer
Jan 17, 2002
80
US
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


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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);
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
Test this code with 256 display colors - many converters do not work with more colors, and Your Functions use display settings.
 
Thanks for the idea! However, the image still doesn't come out.

--Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top