a121496
asked on
Display a bitmap in a dialog
Okay, this has to be an easy easy easy problem. How do I load a bitmap from a file and display it in a dialog based application? There is no need for scaling, rotating, or any of that. All I want is to load a bitmap given a filename and display it in a dialog.
Thank you.
Thank you.
ASKER
I am going to have to reject your answer since I wanted one that loads a BMP from a FILE not from the resource. Points are at 50 now.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is a example for RGB color. If you load indexed color image, the GetPaletteSize will be different. You can compute the size yourself.
first read the bitmap file into a buffer. let's say pBuffer is a pointer pointing to the head of this buffer
void Display(pBuffer)
{
LPBITMAPINFO pBitmapInfo = pBuffer + sizeof(BITMAPFILEHEADER);
LPBITMAPINFOHEADER pBitmapInfoHeader = pBitmapInfo.bmiHeader;
PCHAR pData = pBitmapInfo + GetPaletteSize(pBitmapInfo );
SetDibitsToDevice(p1,p2,p3 ,p4,p5,p6, p7,p8,p1,p Data,pBitm apInfoHead er,.....);
}
DWORD GetPaletteSize(LPBITMAPINF O)
{
LPBITMAPINFOHEADER pHeader = pBitmapInfo.bmiHeader;
if (pHeader.biClrUsed ==0)
return 256*sizeof(RGBQUAD);
else
return pHeader.biClrUsed*sizeof(R GBQUAD);
}
first read the bitmap file into a buffer. let's say pBuffer is a pointer pointing to the head of this buffer
void Display(pBuffer)
{
LPBITMAPINFO pBitmapInfo = pBuffer + sizeof(BITMAPFILEHEADER);
LPBITMAPINFOHEADER pBitmapInfoHeader = pBitmapInfo.bmiHeader;
PCHAR pData = pBitmapInfo + GetPaletteSize(pBitmapInfo
SetDibitsToDevice(p1,p2,p3
}
DWORD GetPaletteSize(LPBITMAPINF
{
LPBITMAPINFOHEADER pHeader = pBitmapInfo.bmiHeader;
if (pHeader.biClrUsed ==0)
return 256*sizeof(RGBQUAD);
else
return pHeader.biClrUsed*sizeof(R
}
ASKER
It is clear that Answers knows what he is talking about. I have this code:
void CTestDlg::OnButtonLoad ()
{ HBITMAP hBitmap = (HBITMAP) ::LoadImage (0, m_Filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (hBitmap == NULL)
{ MessageBox ("ERROR!"); }
m_BMP.SetBitmap (hBitmap);
}
It does not give me an "ERROR!" But nothing is displayed on the screen at the location of the static resource either. What am I doing wrong?
void CTestDlg::OnButtonLoad ()
{ HBITMAP hBitmap = (HBITMAP) ::LoadImage (0, m_Filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (hBitmap == NULL)
{ MessageBox ("ERROR!"); }
m_BMP.SetBitmap (hBitmap);
}
It does not give me an "ERROR!" But nothing is displayed on the screen at the location of the static resource either. What am I doing wrong?
ASKER
Perhaps I should replace the first parameter of the LoadImage function with something other than 0? What though?
ASKER
I was making stupid mistakes (several). Suffice it to say you answered my question well. Thanks.
2.Bind a CStatic member variable to the static control (member variables tab in class wiz), say m_staticPict.
3. Use LoadImage API call to load the file, this gives you back a HBITMAP. As the return type is an HANDLE, you need to cast it
HBITMAP hBitmap = (HBITMAP)::LoadImage( ...etc...)
4. Use SetBitmap member of m_staticPict to set the bitmap
e.g.
m_staticPict.SetBitmap( hBitmap ) ;
That's it.