Displaying a Full View Bitmap Image in a Windows VCL Application
(Redirected from Displaying a Full View Bitmap Image in a VCL Forms Application)
Go Up to How To Build a Windows VCL Application with Graphics
This procedure loads a bitmap image from a file and displays it in its entirety to a VCL form. The procedure uses the Height and Width properties of the Bitmap object to display a full view of the image.
- Create a VCL form with a button control.
- Provide a bitmap image.
- Code the button's onClick event handler to load and display a bitmap image.
- Build and run the application.
To create a VCL form and button
- Choose File > New > Windows VCL Application - Delphi or File > New > Windows VCL Application - C++Builder .
- From the Standard page of the Tool Palette, place a button component on the form.
- In the Object Inspector, enter Full View for the Caption property and FullView for the name property.
To provide a bitmap image
- Create a directory in which to store your project files.
- Locate a bitmap image on your local drive, and copy it to your project directory.
- Save all files in your project to your project directory.
To write the OnClick event handler
- In the Object Inspector, double-click the Button1 OnClick event on the Events tab. The Code Editor displays with the cursor in the TForm1.FullViewClick (Delphi) or TForm1::FullViewClick (C++) event handler block.
- Enter the following event handling code, replacing MyFile.bmp with the name of the bitmap image in your project directory:
Bitmap := TBitmap.Create; try Bitmap.LoadFromFile('MyFile.bmp'); Form1.Canvas.Brush.Bitmap := Bitmap; Form1.Canvas.FillRect(Rect(0,0,Bitmap.Width,Bitmap.Height)); finally Form1.Canvas.Brush.Bitmap := nil; Bitmap.Free; end;
Graphics::TBitmap Bitmap = new Graphics::TBitmap(); try { Bitmap->LoadFromFile( "..\\MyFile.bmp" ); Form1->Canvas->Brush->Bitmap = Bitmap; Form1->Canvas->FillRect( Rect( 0, 0, Bitmap->Width, Bitmap->Height ) ); } __finally { Form1->Canvas->Brush->Bitmap = NULL; Bitmap->Free(); }
Note: For C++ projects, the code assumes the target output directory is located in the project directory.
- For Delphi, add the following variable declaration in the var block:
Bitmap : TBitmap;
To run the program
- Choose Run > Run .
- Click the button to display the image bitmap in a rectangle in the upper left corner of the form.