The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: cman on October 10, 2010, 07:53:41 PM

Title: Loading A Bitmap
Post by: cman on October 10, 2010, 07:53:41 PM
Whats the difference between using the resource compiler to load a bitmap and loading it this way ( this is code from a C++ programming book ):



  // Open the bitmap file
  HANDLE hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  if (hFile == INVALID_HANDLE_VALUE)
    return FALSE;

  // Read the bitmap file header
  BITMAPFILEHEADER  bmfHeader;
  DWORD             dwBytesRead;
  BOOL bOK = ReadFile(hFile, &bmfHeader, sizeof(BITMAPFILEHEADER),
    &dwBytesRead, NULL);
  if ((!bOK) || (dwBytesRead != sizeof(BITMAPFILEHEADER)) ||
    (bmfHeader.bfType != 0x4D42))
  {
    CloseHandle(hFile);
    return FALSE;
  }

  BITMAPINFO* pBitmapInfo = (new BITMAPINFO);
  if (pBitmapInfo != NULL)
  {
    // Read the bitmap info header
    bOK = ReadFile(hFile, pBitmapInfo, sizeof(BITMAPINFOHEADER),
      &dwBytesRead, NULL);
    if ((!bOK) || (dwBytesRead != sizeof(BITMAPINFOHEADER)))
    {
      CloseHandle(hFile);
      Free();
      return FALSE;
    }

    // Store the width and height of the bitmap
    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))
    {
      SetFilePointer(hFile, bmfHeader.bfOffBits, NULL, FILE_BEGIN);
      bOK = ReadFile(hFile, pBitmapBits, pBitmapInfo->bmiHeader.biSizeImage,
        &dwBytesRead, NULL);
      if (bOK)
        return TRUE;
    }
  }



I'm not sure which way to do this ( I think the resource compiler combines the bitmap with the executable file , which I don't think would be a good idea ). Thanks for any information!
Title: Re: Loading A Bitmap
Post by: raymond on October 11, 2010, 02:52:43 AM
Quote( I think the resource compiler combines the bitmap with the executable file , which I don't think would be a good idea ).

What would be a logical reason for not being a good idea?

If you need that bitmap for your app and it is not included in it, you would have to provide it as a separate file, requiring the same amount of space. Then, you have to provide two files for your app to function. If only the executable gets provided (or the bmp gets lost or corrupted), your app won't work.
Title: Re: Loading A Bitmap
Post by: Farabi on October 11, 2010, 06:44:41 AM
MASM lib already provided it, why take the hard way? It could load a jpg too.
Title: Re: Loading A Bitmap
Post by: ToutEnMasm on October 11, 2010, 07:21:10 AM

The difference is in the number of disk access and speed.
The resources are loaded with the executable and you need no search path and none access disk.
Createfile need the path and made an access disk for each file.
Title: Re: Loading A Bitmap
Post by: sinsi on October 11, 2010, 08:43:51 AM
Just use LoadImage, this can load a resource or a file.
As Raymond said, if it isn't going to change then include it in a resource. If the file isn't there, what happens next?
ToutEnMasm is right, the whole exe file is loaded into memory by windows, so your bmp is there.

Lots of image functions can work with a handle or a resource ID without having to load an external file.
Title: Re: Loading A Bitmap
Post by: oex on October 11, 2010, 01:42:57 PM
So I guess one reason why not to include it would be to not have it clogging up memory? Some images I deal with are 120Mb+
Title: Re: Loading A Bitmap
Post by: raymond on October 11, 2010, 05:45:17 PM
QuoteSome images I deal with are 120Mb+

The basic question will always be: Is it a necessary constant part of the app or is it only a part which may or may not even be required when running it. If the former, it will always clog memory, regardless of how you load it.
Title: Re: Loading A Bitmap
Post by: cman on October 11, 2010, 08:10:03 PM
OK , thanks for the information! The bitmap I am loading is definitely going to be used in the application so I will use the resource compiler to load it. I was just confused as why one would use one method or the other to load such a resource. Thank you for answering my questions! :U
Title: Re: Loading A Bitmap
Post by: dedndave on October 12, 2010, 12:02:11 AM
these are huge images - lol
Ketil O was playing with something similar a while back (i think it was Ketil)
you might find some help if you search the forum for "navigation chart" or similar phrases (posts by KetilO)

i downloaded a program from NASA or NOAA called "reproject" and played with it
it seems to do a good job of handling the mega-huge image files
Title: Re: Loading A Bitmap
Post by: oex on October 12, 2010, 12:10:33 AM
Yeah these are NASA style images.... Though these days Normal camera images are getting quite large 4kx3k+pixels which is over 45Mb in RGBA format.... Many compression formats require you to decompress the lot into memory in one go so you do need the free RAM....

The reason for my sub question was that I dont use resources and I wondered.... can you free the memory they use, does windows self manage it or does it just load straight into memory on application execution and then stay there until application end?
Title: Re: Loading A Bitmap
Post by: cman on October 14, 2010, 11:00:41 PM
What sort of controls are appropriate for painting a bitmap animation ( I haven't programmed GUI for quite a while ) ? I'm trying to put a bitmap animation on a control placed on a dialog generated by Visual Studio's resource editor but I'm not sure which control to use to do this ( a static control?? ). Does anyone have any example code to do this? Thanks for any information....
Title: Re: Loading A Bitmap
Post by: jj2007 on October 15, 2010, 01:18:33 AM
Quote from: cman on October 14, 2010, 11:00:41 PM
What sort of controls are appropriate for painting a bitmap animation

Anything goes. Choose a button if you want to click on the animation, otherwise static is fine.