Displaying Images

The TBitmap object itself does not contain any methods that deal directly with its display. This functionality lies in the domain of the TCanvas object. There are several methods by which a bitmap can be displayed, and they all have similar ancestors in the form of GDI function calls. The act of copying bitmap pixels to the screen for display is known as a bitblt, or blit.

bitblt: bit block transfer. Pronounced "blit," this is the act of copying the pixels of a bitmap image onto the screen or other graphical output surface for display.

The easiest TCanvas method available for displaying an image is Draw. It simply takes three parameters: a horizontal and vertical position of the upper left-hand corner of the image and the image object itself. This draws the entire image with its upper left-hand corner located at the specified coordinate. Other methods include CopyRect, which allows only a portion of an image to be drawn (scaling as necessary), and StretchDraw, which allows the image to be drawn larger or smaller than its original size. Listing 3-5 illustrates loading a bitmap and displaying it on the surface of a form.

Listing 3-5: Loading and displaying a bitmap image procedure TForm1.FormCreate(Sender: TObject); begin

  • create the bitmap object} Bitmap := TBitmap.Create;
  • load in a bitmap image}

Bitmap.LoadFromFile(ExtractFilePath(ParamStr(0))+'Athena.bmp'); end;

procedure TForm1.FormDestroy(Sender: TObject); begin

{free the bitmap object} Bitmap.Free; end;

procedure TForm1.FormPaint(Sender: TObject); begin

  • display the bitmap using the Draw method} Canvas.Draw(8, 24, Bitmap);
  • display the bitmap using the CopyRect method}

Canvas.CopyRect(Rect(192, 24, 344, 104),Bitmap.Canvas,Rect(62, 39, 111, 74)

{display the bitmap using the StretchDraw method} Canvas.StretchDraw(Rect(8, 184, 352, 264), Bitmap); end;

Figure 3-12: An image displayed using several different methods

0 0

Post a comment