Difference between revisions of "BGRABitmap tutorial 2"

From Lazarus wiki
Jump to navigationJump to search
(created)
 
Line 83: Line 83:
  
 
You should see a form with an image drawn in it at the upper-left corner.
 
You should see a form with an image drawn in it at the upper-left corner.
 +
 +
[[BGRABitmap tutorial|Previous tutorial]] | [[BGRABitmap tutorial 3|Next tutorial]]

Revision as of 14:38, 12 March 2011

This tutorial shows you how to load an image and draw it on a form.

Create a new project

Create a new project and add a reference to BGRABitmap, the same way as in the first tutorial.

Load the bitmap

Copy an image into your project directory. Let's suppose it's name is image.png.

Add a private variable to the main form to store the image : <delphi> TForm1 = class(TForm)

 private
   { private declarations }
   image: TBGRABitmap;
 public
   { public declarations }
 end; </delphi>

Load the image when the form is created. To do this, double-click on the form, a procedure should appear in the code editor. Add the loading instruction : <delphi>procedure TForm1.FormCreate(Sender: TObject); begin

 image := TBGRABitmap.Create('image.png');

end; </delphi>

Draw the bitmap

Add an OnPaint handler. To do this, select the main form, then go to the object inspector, in the event tab, and double-click on the OnPaint line. Then, add the drawing code : <delphi>procedure TForm1.FormPaint(Sender: TObject); begin

 image.Draw(Canvas,0,0,True);

end; </delphi>

Code

Finally you should have something like : <delphi>unit UMain;

{$mode objfpc}{$H+}

interface

uses

 Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
 BGRABitmap, BGRABitmapTypes;

type

 { TForm1 }
 TForm1 = class(TForm)
   procedure FormCreate(Sender: TObject);
   procedure FormPaint(Sender: TObject);
 private
   { private declarations }
   image: TBGRABitmap;
 public
   { public declarations }
 end; 

var

 Form1: TForm1; 

implementation

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject); begin

 image := TBGRABitmap.Create('image.png');

end;

procedure TForm1.FormPaint(Sender: TObject); begin

 image.Draw(Canvas,0,0,True);

end;

initialization

 {$I UMain.lrs}

end.</delphi>

Run the program

You should see a form with an image drawn in it at the upper-left corner.

Previous tutorial | Next tutorial