Click to See Complete Forum and Search --> : Loading and showing a JPEG file


dit6a9
February 26th, 2005, 03:52 AM
I'm trying to load and show a JPEG file this way:

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HDC hdcMem;

HRSRC hRsrc;
HGLOBAL hGlobal;
byte* lpBits;

BITMAPINFO bmi;
HDC hdc;
int iRet;
PAINTSTRUCT ps;

switch (message)
{
case WM_CREATE:
{
hRsrc = FindResource(NULL, "RCDATA_0", RT_RCDATA);
if (hRsrc==NULL) {
MessageBox(NULL, "hRsrc==NULL", "Error", MB_OK | MB_ICONERROR);
return 0;
}

hGlobal = LoadResource(NULL, hRsrc);
if (hGlobal==NULL) {
MessageBox(NULL, "hGlobal==NULL", "Error", MB_OK | MB_ICONERROR);
return 0;
}

lpBits = LockResource(hGlobal);
if (lpBits==NULL) {
MessageBox(NULL, "lpBits==NULL", "Error", MB_OK | MB_ICONERROR);
return 0;
}

memset(&bmi, 0, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = 400;
bmi.bmiHeader.biHeight = -300;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 0;
bmi.bmiHeader.biCompression = BI_JPEG;
bmi.bmiHeader.biSizeImage = SizeofResource(NULL, hRsrc);

hdcMem = CreateCompatibleDC(NULL);

iRet = StretchDIBits
(
hdcMem,
0, 0, 400, 300,
0, 0, 400, 300,
lpBits,
&bmi,
DIB_RGB_COLORS,
SRCCOPY
);

FreeResource(hGlobal);

if (iRet==GDI_ERROR) {
MessageBox(NULL, "iRet==GDI_ERROR", "Error", MB_OK | MB_ICONERROR);
}
break;

}
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
iRet = StretchBlt
(
hdc,
0, 0, 400, 300,
hdcMem,
0, 0, 400, 300,
SRCCOPY
);
EndPaint(hwnd, &ps);
break;
}
case WM_DESTROY:
{
DeleteDC(hdcMem);
PostQuitMessage (0);
break;
}
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}


If StretchDIBits fails, the return value is GDI_ERROR, but I never get it. If StretchDIBits succeeds, the return value is the number of scan lines copied, and I always get 0.

Can anybody help me?

NoHero
February 27th, 2005, 06:34 AM
My tipp: Use GDI+: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/gdiplus/gdiplus.asp

You will find a lot of examples showing how to use the Bitmap class.
If you are facing problems just come back, and we will help you.