Sign in to follow this  
lonewolff

Problems loading a Bitmap from memory

Recommended Posts

Hi Guys, hopefully you can help me out as I have hit a brick wall and 'Dr. Google' isn't being of great help. :unsure:

I have a Win32 application in C++, where I have manually loaded a bitmap into memory. I know you can use LoadImage() etc.. But, I need it to be done this way for the (future) purpose of my application.

I have gotten to the point where my Bitmap is in memory but am having problems drawing it to the screen.

Here is my WM_PAINT code

[code] case WM_PAINT:
{
// ***** THESE HAVE BEEN DECLARED GLOBALLY *****
// LPBITMAPINFO lpbi=NULL; // bitmap format info; used by GetDIBits
// LPVOID lpvBits=NULL; // pointer to bitmap bits array
// BITMAPFILEHEADER bmfh; // .bmp file header
// *********************************************

BITMAP hBitmap;
PAINTSTRUCT ps;

HDC hdc=BeginPaint(hWnd,&ps);

HDC hdcMem=CreateCompatibleDC(hdc);

hBitmap=CreateDIBSection(NULL,&lpbi,DIB_RGB_COLORS,(void**)&lpvBits,NULL,0);
SetDIBits(NULL,hBitmap,0,lpbi->bmiHeader.biHeight,lpbi->bmiHeader.biSizeImage,&lpbi,DIB_RGB_COLORS);

BitBlt(hdc,0,0,hBitmap.bmWidth,hBitmap.bmHeight,hdcMem,0,0,SRCCOPY);

SelectObject(hdcMem,hbmOld); // Not sure what to do here also...
DeleteDC(hdcMem);

EndPaint(hWnd,&ps);
}[/code]

But when I compile I get the following errors;

[b]error C2664: 'CreateDIBSection' : cannot convert parameter 2 from 'LPBITMAPINFO *' to 'const BITMAPINFO *'
error C2664: 'SetDIBits' : cannot convert parameter 2 from 'BITMAP' to 'HBITMAP'
[/b]
[size="1"][size="2"]Any help would be absolutly awesome! B) [/size]

[/size]

Share this post


Link to post
Share on other sites
The first error says that parameter 2 of CreateDIBSection should be a pointer, not a pointer-to-a-pointer. Change "&lpbi" to "lpbi".

Also, CreateDIBSection returns a HBITMAP, not a BITMAP, so change "BITMAP hBitmap;" to "HBITMAP hBitmap;"

Share this post


Link to post
Share on other sites
Thanks for your help.

Things are a little better but changing BITMAP to HBITMAP breaks the [color="#660066"]BitBlt[/color][color="#666600"]() [/color][color="#000000"]function.

[size="2"][b]error C2228: left of '.bmWidth' must have class/struct/union[/b]

[/size][/color]

Share this post


Link to post
Share on other sites
Try not to create all your stuff over and over every time you draw a frame, just do it once at creation and destroy it when you close.

The simplest way (and probably the fastest?) to draw a bitmap is to convert it to a 32 bit one after you load it and you can just do one memcpy per line.

Share this post


Link to post
Share on other sites
It looks like you may have another problem. Do you have lpbi = new BITMAPINFO somewhere? I.e., do you actually create the structure somewhere? GetDIBits needs a pointer to a structure to fill out, not just a pointer.

Normally one would declare [b]BITMAPINFO bmi[/b] and pass [b]&bmi[/b] for the required LPBITMAPINFO and BITMAPINFO* arguments in the various function calls.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this