RAD Studio for Microsoft .NET |  |

Displaying a Bitmap Image in a VCL Forms Application
See Also
Collapse All

These procedures load a bitmap image from a file and displays it to a VCL form.

1. Create a VCL form with a button control.
2. Provide a bitmap image.
3. Code the button's onClick event handler to load and display a bitmap image.
4. Build and run the application.

To create a VCL form and button

1. Choose FileNewOtherDelphi for .NET ProjectsVCL Forms Application. The VCL Forms Designer displays.
2. From the Standard category in the Tool Palette, place a TButton component on the form.

To provide a bitmap image

1. Create a directory in which to store your project files.
2. Locate a bitmap image and copy it to your project directory.
3. Save all files in your project to your project directory.

To write the OnClick event handler

1. 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.
2. Enter the following event handling code, replacing MyFile.bmp with the name of the bitmap image in your project directory:

Copy Code

                    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.

1. In the var section of the code, add these variable declarations:

Copy Code

    Bitmap : TBitmap;
    Rect : TRect;

To run the program

1. Save all files in your project.
2. Choose RunRun.
3. Click the button to display the image bitmap in a 100 x 100-pixel rectangle in the upper left corner of the form.

See Also

VCL.NET Overview

Drawing Straight Lines In a VCL Application

Drawing Rectangles and Ellipses in a VCL Application

Drawing a Rounded Rectangle in a VCL Application

Placing A Bitmap Image in a Control in a VCL Forms Application

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!