Question:
How do I display a bitmap image in Builder?
Answer:
Basics: Using TBitmap with TImage
Display a bitmap image at runtime is easy. The following code
will display
a TBitmap on a TImage using the Canvas of the TBitmap:
To compile the example, follow these steps:
1. File | Close All
2. File | New Application
3. Place a TImage on the Form
4. Go to the Object Inspector and change its Align Property
to alClient
5. Drop a TOpenDialog on the form
6. Place a TButton on the form and double-click on it.
7. Now place the following code in the Button1Click event
function:
8. Compile and run to see the TBitmap displayed!
//--------------------------------------------------------
// It is necessary to specify Graphics::TBitmap found
in <Graphics.hpp>
Graphics::TBitmap* bm = new Graphics::TBitmap;
try
{
// Opens a dialog and stores the selected
filename
OpenDialog1->Execute();
// Loads the user selected file
bm->LoadFromFile(OpenDialog1->FileName);
// Turns on image stretching
Image1->Stretch = true;
// Assigns the TBitmap to the TImage
Image1->Picture->Graphic = bm;
// Sent the TBitmap to the cavas to display
Image1->Canvas->Draw(0, 0, bm);
}
__finally
{
// Cleanup
delete bm;
}
}
//--------------------------------------------------------
Last Modified: 06-JUN-00