These procedures load a bitmap image from a file and displays it to a VCL form.
- 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 FileNewOtherDelphi for .NET ProjectsVCL Forms Application. The VCL Forms Designer displays.
- From the Standard category in the Tool Palette, place a TButton component on the form.
To provide a bitmap image
- Create a directory in which to store your project files.
- Locate a bitmap image 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 Input category of the Events tab, double-click the Button1OnClick event. The Code Editor displays with the cursor in the TForm1.Button1Click event handler block.
- Enter the following event handling code, replacing MyFile.bmp with the name of the bitmap image in your project directory:
Rect := TRect.Create(0,0,100,100);
Bitmap := TBitmap.Create;
try
Bitmap.LoadFromFile('MyFile.bmp');
Form1.Canvas.Brush.Bitmap := Bitmap;
Form1.Canvas.FillRect(Rect);
finally
Form1.Canvas.Brush.Bitmap := nil;
Bitmap.Free;
end;
Tip: You can change the size of the rectangle to be displayed by adjusting the Rect parameter values.
- In the var section of the code, add these variable declarations:
Bitmap : TBitmap;
Rect : TRect;
To run the program
- Save all files in your project.
- Choose RunRun.
- Click the button to display the image bitmap in a 100 x 100-pixel rectangle in the upper left corner of the form.