Image Manipulation
Similarly, image manipulation in
Delphi is designed around the concept of a picture object such
as TBitmap. This object too has a canvas which can be manipulated
the same way that of a picture box or form can be.
Suppose you wanted to mimic the functionality
of the PICCLIP.OCX control which ships with Visual Basic Professional.
This control allows you to extract portions of a matrix of pictures
and assign the "cell picture" to another picture control.
To accomplish this, you might create
a subroutine which extracted a portion of a picture like this
and assigned it to another picture property:
procedure
GetCell (Index:Integer; Dest, Source:TPicture);
var
BWidth, BHeight:Integer;
SrcR, DestR:TRect;
begin
BWidth := Source.Picture.Width
div FCols;
BHeight := Source.Picture.Height
div FRows;
DestR.Left := 0;
DestR.Top := 0;
DestR.Right := BWidth;
DestR.Bottom := BHeight;
SrcR.Left := (Index mod
Cols) * BWidth;
SrcR.Top := (Index div
Cols) * BHeight;
SrcR.Right := SrcR.Left
+ BWidth;
SrcR.Bottom := SrcR.Top
+ BHeight;
Dest.Bitmap.Width := BWidth;
Dest.Bitmap.Height := BHeight;
Dest.Bitmap.Canvas.CopyRect
(DestR, Source.Canvas, SrcR);
end;
This is a pretty clean routine which
doesn't use any API calls. Although it might be "showing
off," you can take this a step further. What if you actually
wanted to create a PICCLIP component for Delphi? Where would you
begin? You want a control that is prepared to take a picture property,
so why not begin with a TImage component and work from
there. That's exactly what was done in the following unit, which
represents an implementation of PicClip for Delphi. You need only
install PRCCLIP.PAS into the IDE and you'll have a PicClip component!
Here is the complete source code for the PicClip component for
Delphi:
unit
PicClip;
interface
uses
Classes, Controls, WinTypes, Graphics, StdCtrls;
type
TPicClip = class(TImage)
private
FRows:Integer;
FCols:Integer;
FPicture:TPicture;
function GetCell (Index:Integer):TPicture;
public
constructor Create(AOwner:
TComponent); override;
destructor Destroy;override;
property GraphicCell[Index:Integer]:TPicture
read GetCell;
published
property Rows:Integer read
FRows write FRows;
property Cols:Integer read
FCols write FCols;
end;
procedure
Register;
implementation
constructor TPicClip.Create(AOwner:
TComponent);
begin
inherited Create(AOwner);
FPicture := TPicture.Create;
Visible := False;
end;
destructor TPicClip.Destroy;
begin
FPicture.Destroy;
inherited Destroy;
end;
function
TPicClip.GetCell (Index:Integer):TPicture;
var
BWidth, BHeight:Integer;
SrcR, DestR:TRect;
begin
BWidth := Picture.Width
div FCols;
BHeight := Picture.Height
div FRows;
DestR.Left := 0;
DestR.Top := 0;
DestR.Right := BWidth;
DestR.Bottom := BHeight;
SrcR.Left := (Index mod
Cols) * BWidth;
SrcR.Top := (Index div
Cols) * BHeight;
SrcR.Right := SrcR.Left
+ BWidth;
SrcR.Bottom := SrcR.Top
+ BHeight;
FPicture.Bitmap.Width :=
BWidth;
FPicture.Bitmap.Height
:= BHeight;
FPicture.Bitmap.Canvas.CopyRect
(DestR, Canvas, SrcR);
GetCell := FPicture;
end;
procedure
Register;
begin
RegisterComponents('Foxhall',
[TPicClip] );
end;
end.
It is intended that the examples
provided in this document have helped highlight some of the key
differences between programming with the interpreter-based Visual
Basic Pro and Borland's next-generation Delphi, built around a
state of that art optimising native code compiler. Furthermore,
the sample code also makes clear the differences between the script-language
style of Visual Basic versus the more structured, object-oriented
approach made possible by Delphi's Object Pascal language. You
should now be able to begin applying your programming skills productively
in Delphi. You can also use a variety of third party tools and
components to facilitate creation of Delphi or Delphi Client/Server
applications including tools that help convert existing code into
Delphi code.
As you become familiar with Delphi
programming you will find that Delphi and Delphi Client/Server
are able to take you further in your application development
with greater performance, reuse and scalability than Visual Basic
Pro.
Connect with Us