Setting the Initial Bitmap Size
Go Up to Adding an Image Control
When you place an image control, it is simply a container. However, you can set the image control's Picture property at design time to contain a static graphic. The control can also load its picture from a file at run time, as described in Loading and Saving Graphics Files.
To create a blank bitmap when the application starts
- Attach a handler to the OnCreate event for the form that contains the image.
- Create a bitmap object, and assign it to the image control's Picture.Graphic property.
In this example, the image is in the application's main form, Form1, so the code attaches a handler to Form1's OnCreate event:
procedure TForm1.FormCreate(Sender: TObject);
var
Bitmap: TBitmap; { temporary variable to hold the bitmap }
begin
Bitmap := TBitmap.Create; { construct the bitmap object }
Bitmap.Width := 200; { assign the initial width... }
Bitmap.Height := 200; { ...and the initial height }
Image.Picture.Graphic := Bitmap; { assign the bitmap to the image control }
Bitmap.Free; { We are done with the bitmap, so free it }
end
void __fastcall TForm1::FormCreate(TObject *Sender) {
Graphics::TBitmap *Bitmap = new Graphics::TBitmap();
// create the bitmap object
Bitmap->Width = 200; // assign the initial width...
Bitmap->Height = 200; // ...and the initial height
Image->Picture->Graphic = Bitmap; // assign the bitmap to the image control
delete Bitmap; // free the bitmap object
}
Assigning the bitmap to the picture's Graphic property copies the bitmap to the picture object. However, the picture object does not take ownership of the bitmap, so after making the assignment, you must free it.
If you run the application now, you see that client area of the form has a white region, representing the bitmap. If you size the window so that the client area cannot display the entire image, you'll see that the scroll box automatically shows scroll bars to allow display of the rest of the image. But if you try to draw on the image, you don't get any graphics, because the application is still drawing on the form, which is now behind the image and the scroll box.