Jump to content
  • Advertisement
Sign in to follow this  
CulDeVu

[C++] Win32 Image Loading

This topic is 2528 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all!

I've seen two different ways to load bitmap files from a resource (and from a file) using the Win32 API. This one I got from a book, and I've seen on the web once or twice:


// Find the bitmap resource
HRSRC hResInfo = FindResource(hInstance, MAKEINTRESOURCE(uiResID), RT_BITMAP);
if (hResInfo == NULL)
return FALSE;

// Load the bitmap resource
HGLOBAL hMemBitmap = LoadResource(hInstance, hResInfo);
if (hMemBitmap == NULL)
return FALSE;

// Lock the resource and access the entire bitmap image
PBYTE pBitmapImage = (BYTE*)LockResource(hMemBitmap);
if (pBitmapImage == NULL)
{
FreeResource(hMemBitmap);
return FALSE;
}

// Store the width and height of the bitmap
BITMAPINFO* pBitmapInfo = (BITMAPINFO*)pBitmapImage;
m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth;
m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight;

// Get a handle to the bitmap and copy the image bits
PBYTE pBitmapBits;
m_hBitmap = CreateDIBSection(hDC, pBitmapInfo, DIB_RGB_COLORS,
(PVOID*)&pBitmapBits, NULL, 0);
if ((m_hBitmap != NULL) && (pBitmapBits != NULL))
{
const PBYTE pTempBits = pBitmapImage + pBitmapInfo->bmiHeader.biSize +
pBitmapInfo->bmiHeader.biClrUsed * sizeof(RGBQUAD);
CopyMemory(pBitmapBits, pTempBits, pBitmapInfo->bmiHeader.biSizeImage);

if (m_crTransparent != 0)
m_hBitmask = CreateBitmask(hDC, m_hBitmap, m_crTransparent);

// Unlock and free the bitmap graphics object
UnlockResource(hMemBitmap);
FreeResource(hMemBitmap);
return TRUE;
}

// Something went wrong, so cleanup everything
UnlockResource(hMemBitmap);
FreeResource(hMemBitmap);
Free();
MessageBox(NULL, NULL, NULL, NULL);
return FALSE;// Load the bitmap resource
HGLOBAL hMemBitmap = LoadResource(hInstance, hResInfo);
if (hMemBitmap == NULL)
return FALSE;

// Lock the resource and access the entire bitmap image
PBYTE pBitmapImage = (BYTE*)LockResource(hMemBitmap);
if (pBitmapImage == NULL)
{
FreeResource(hMemBitmap);
return FALSE;
}

// Store the width and height of the bitmap
BITMAPINFO* pBitmapInfo = (BITMAPINFO*)pBitmapImage;
m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth;
m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight;

// Get a handle to the bitmap and copy the image bits
PBYTE pBitmapBits;
m_hBitmap = CreateDIBSection(hDC, pBitmapInfo, DIB_RGB_COLORS,
(PVOID*)&pBitmapBits, NULL, 0);
if ((m_hBitmap != NULL) && (pBitmapBits != NULL))
{
const PBYTE pTempBits = pBitmapImage + pBitmapInfo->bmiHeader.biSize +
pBitmapInfo->bmiHeader.biClrUsed * sizeof(RGBQUAD);
CopyMemory(pBitmapBits, pTempBits, pBitmapInfo->bmiHeader.biSizeImage);

if (m_crTransparent != 0)
m_hBitmask = CreateBitmask(hDC, m_hBitmap, m_crTransparent);

// Unlock and free the bitmap graphics object
UnlockResource(hMemBitmap);
FreeResource(hMemBitmap);
return TRUE;
}

// Something went wrong, so cleanup everything
UnlockResource(hMemBitmap);
FreeResource(hMemBitmap);
Free();
MessageBox(NULL, NULL, NULL, NULL);
return FALSE;


When I look up more in-depth explanations, on most internet sites I've seen this instead:


m_hBitmap = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(uiResID), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
if (m_crTransparent != 0)
CreateBitmask(hDC, m_hBitmap, m_crTransparent);


I couldn't find an explanation anywhere for this major difference. So my question is: what's the difference (besides size) between the two loading functions? Thanks for the help!

Share this post


Link to post
Share on other sites
Advertisement
LoadImage is basically a helper function which does all the heavy lifting (as depicted in the "first way") for you, instead of you having to manually read the bitmap header and fill in the bitmap data yourself from disk.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!