Create image from Form

From Lazarus wiki
Jump to navigationJump to search

Deutsch (de) English (en)

The following units are required for the examples:

  ...
  
 uses
   Graphics, Controls, Clipbrd, Forms, ...

   ...

This function creates a screenshot from the submitted form:

  ...

 uses
   Forms, Graphics, Controls, ...

   ...

 Procedure subCreateFormImage(Form: TForm; outBitmap: TBitmap);
 var
   Width: Integer;
   Height: Integer;
   Rectangle: TRect;

 begin

   Width := Form.Width;
   Height:= Form.Height;
   Rectangle:= Rect(0, 0, width, height);

   Bitmap.Width := width;
   Bitmap.Height:= height;
   Bitmap.Canvas.CopyRect(rectangle, form.Canvas, rectangle);

 end;

Demonstration of how the above function can be used:

  ...

 uses
   Clipbrd, ...;

   ...

 procedure test;
 var
   Bitmap: TBitMap;

 begin

   ...

   // Requests the memory for the bitmap
   Bitmap := TBitmap.Create;

   // Calls the function to create the screen printout / screenshot
   subCreateFormImage(Form1, Bitmap);

   // Example 1: Assigns the bitmap to an image
   image1.Picture.Assign(bitmap);

   // Example 2: Assigns the bitmap to the clipboard
   Clipboard.Assign(bitmap);

   // Releases the memory for the bitmap
   FreeAndNil(bitmap)

   ...

 end;

   ...