Bitmap Images


If you only have to display an image on the form, you can use the TImage component and load the bitmap into its Picture property. But if you want to do anything more complex with bitmap images, you should use the TBitmap class. The TBitmap class is a great class that enables you to load, save, and process bitmap images.

The TCanvas class has several methods that enable you to draw bitmap images. The three most often used methods are Draw, StretchDraw, and CopyRect. The Draw and StretchDraw methods enable you to draw entire images, while the CopyRect method enables you to draw portions of the bitmap, as shown in Figure 22-16.

image from book
Figure 22-16: Drawing bitmaps on the canvas

Carefully look at the comments (and the code, of course) in Listing 22-16 to see how to work with and draw bitmap images.

Listing 22-16: Drawing bitmaps

image from book
unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs; type   TMainForm = class(TForm)     procedure FormPaint(Sender: TObject);     procedure FormDestroy(Sender: TObject);     procedure FormCreate(Sender: TObject);   private     { Private declarations }     FImage: TBitmap;   public     { Public declarations }   end; var   MainForm: TMainForm; implementation {$R *.dfm} procedure TMainForm.FormCreate(Sender: TObject); var   imagePath: string; begin   { create and load the image here, actually - always do as     much as you can outside of the OnPaint event handler, write     only what's necessary in OnPaint }   FImage := TBitmap.Create;   imagePath := ExtractFilePath(Application.ExeName) + 'image.bmp';   FImage.LoadFromFile(imagePath); end; procedure TMainForm.FormDestroy(Sender: TObject); begin   { don't forget to release the bitmap from memory }   FImage.Free; end; procedure TMainForm.FormPaint(Sender: TObject); var   srcRect: TRect;   destRect: TRect;   txtHeight: Integer; begin   with Canvas do   begin     Font.Color := clYellow;     Font.Size := 16;     txtHeight := TextHeight('Wg');   end;       // with Canvas   { draw the entire image }   Canvas.TextOut(10, 0, 'Draw');   Canvas.Draw(10, txtHeight, FImage);   { draw the image stretched in a 400x100 rect }   Canvas.TextOut(10, FImage.Height + (txtHeight * 2), 'StretchDraw');   srcRect := Rect(10, FImage.Height + txtHeight * 3,     410, FImage.Height + (txtHeight * 3) + 100);   Canvas.StretchDraw(srcRect, FImage);   { draw the 100x100 top-left rect of the image }   Canvas.TextOut(FImage.Width + 20, 0, 'CopyRect');   srcRect := Rect(0, 0, 100, 100);   destRect := Rect(FImage.Width + 20,     txtHeight, FImage.Width + 120, txtHeight + 100);   Canvas.CopyRect(destRect, FImage.Canvas, srcRect); end; end.
image from book



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net