Given this structure:
Code:
typedef struct TAGIMAGE
{
int m_width;
int m_height;
UINT *m_buffer;
} IMAGE, *LPIMAGE;
These two functions should give me the same result, correct?
Code:
bool CreateImage(LPIMAGE image, HINSTANCE hInst, char *res)
{
HBITMAP hbmp;
BITMAP bmp;
hbmp = LoadBitmap(hInst, res);
if (!hbmp)
return false;
GetObject(hbmp, sizeof(BITMAP), &bmp);
image->m_width = bmp.bmWidth;
image->m_height = bmp.bmHeight;
image->m_buffer = (UINT*)GetBitmapPixels(hbmp, GetDC(0));
if (image->m_buffer == NULL)
return false;
return true;
}
Code:
bool CreateImage(LPIMAGE image, HINSTANCE hInst, char *res)
{
HBITMAP hbmp;
BITMAP bmp;
HDC main_dc, bitmap_dc;
hbmp = LoadBitmap(hInst, res);
if (!hbmp)
return false;
GetObject(hbmp, sizeof(BITMAP), &bmp);
image->m_width = bmp.bmWidth;
image->m_height = bmp.bmHeight;
main_dc = GetDC(0);
bitmap_dc = CreateCompatibleDC(main_dc);
HBITMAP old_bmp = SelectBitmap(bitmap_dc, hbmp);
image->m_buffer = new UINT[image->m_width * image->m_height];
for (int y = 0; y < image->m_height; y++)
for (int x = 0; x < image->m_width; x++)
image->m_buffer[y * image->m_width + x] = GetPixel(bitmap_dc, x, y);
SelectBitmap(bitmap_dc, old_bmp);
DeleteDC(bitmap_dc);
DeleteObject(hbmp);
return true;
}
The second works but the first doesn't.