News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Loading A Bitmap

Started by cman, October 10, 2010, 07:53:41 PM

Previous topic - Next topic

cman

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!

raymond

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.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Farabi

MASM lib already provided it, why take the hard way? It could load a jpg too.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

ToutEnMasm


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.

sinsi

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.
Light travels faster than sound, that's why some people seem bright until you hear them.

oex

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+
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

raymond

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.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

cman

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

dedndave

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

oex

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?
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

cman

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....

jj2007

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.