Solved

Display a bitmap in a dialog

Posted on 1998-11-29
7
241 Views
Last Modified: 2013-12-03
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.
0
Comment
Question by:a121496
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 8

Expert Comment

by:Answers2000
ID: 1325473
1. On dialog resource create a static control, make sure it has SS_BITMAP style.  Change the resource id from IDC_STATIC to something else,  e.g. IDC_STATIC_PICT

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

Author Comment

by:a121496
ID: 1325474
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.
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1325475
Step 3 in my original answer can load from a FILE or a resource (NT4 or 95 or 98)

To make it load file a file, make the last parameter of LoadImage (fuLoad) include the LR_LOADFROMFILE flag, and lpszName point (LPCSTR) to a NUL terminated containing the file name.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Expert Comment

by:martin_yeh
ID: 1325476
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,pData,pBitmapInfoHeader,.....);
}

DWORD GetPaletteSize(LPBITMAPINFO)
{
LPBITMAPINFOHEADER pHeader = pBitmapInfo.bmiHeader;
if (pHeader.biClrUsed ==0)
return 256*sizeof(RGBQUAD);
else
return pHeader.biClrUsed*sizeof(RGBQUAD);
}
0
 

Author Comment

by:a121496
ID: 1325477
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?
0
 

Author Comment

by:a121496
ID: 1325478
Perhaps I should replace the first parameter of the LoadImage function with something other than 0?  What though?
0
 

Author Comment

by:a121496
ID: 1325479
I was making stupid mistakes (several).  Suffice it to say you answered my question well.  Thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question