..
| Bitmap |
Picture |
| Metafile |
Icon |
| Clipboard |
|
.
| DIB |
Device Independant Bitmaps |
| DDB |
Device Dependant Bitmaps |
| One main difference between DIB and DDB, is that DDB uses the palette
provided by the system while DIB provides its own palette.
The first versions of Delphi used DDB to store bitmaps in memory, but
from Delphi 3 onwards Delphi could use either DDB or DIB, you can convert
from DIB to DDB or visa versa by setting the Bitmap.HandleType property
to bmDDB or bmDIB, note that you may lose some color information in the
conversion. |
.
| Pen |
Used to set the type of pen the canvas will use for drawing
lines and outlining shapes. |
| Font |
Sets the font - color, style, face and size. |
| PenPos |
Current drawing position of the pen. |
| Pixels |
Sets the color of the area of the pixels within the current ClipRect. |
| Brush |
This is used to fill areas and this includes filling the interior of
shapes. |
| Handle |
Specifies the Windows GDI handle to the device context for this canvas. |
| TextFlags |
Specifies how the text is written to the canvas. |
| CanvasOrientation |
Used to determine the orientation of the canvas as left-to-right or
right-to-left. |
| CopyMode |
Specifies how a graphical image is copied onto the canvas. |
. |
|
| TImage:
One of the advantages of using a TImage control is that refreshing and
painting of the graphic image in the TImage is automatically handled by
the VCL.
|
| The Brush Property:
This is used to fill areas and this includes filling the interior of
shapes.
The 3 brush propeties you can use are:
Style
Color
Bitmap
Examples:
Style:
Canvas.Brush.Style := bsBDiagonal;
Brush Styles = bsSolid, bsClear, bsBDiagonal, bsFDiagonal, bsCross,
bsDiagCross, bsHorizontal, bsVertical.
Color:
Canvas.Brush.Color := clBlue;
Bitmap:
procedure TForm1.Button1Click(Sender: TObject);
var Bitmap : TBitmap;
begin
Bitmap := TBitmap.Create;
try
Bitmap.LoadFromFile('e:\Images\Athena2.bmp');
Canvas.Brush.Bitmap := Bitmap;
Canvas.FillRect(Rect(20, 20, 200, 200));
finally
Canvas.Brush.Bitmap := Nil; {Set to
nil}
Bitmap.Free; {Free the bitmap, Delphi
doesn't do this so we have to free this resource ourselves}
end;
end;
More examples are shown below. |
| The Pen Property:
The pen propeties you can use are:
.
| Mode - |
The Pen Mode property is used to specify various ways to
combine the pens color with the color on the canvas,
and is pmCopy by default.
. |
| Color - |
Used to set the color of the pen.
. |
| Width - |
The pen’s width property determines the thickness, in pixels, of the
lines it uses to draw.
. |
| Style - |
The Pen.Style property lets you set solid lines, dashed lines, dotted
lines, etc.
. |
Examples:
Pen Mode:
pmWhite,
pmBlack,
pmNop (Invisible),
pmNot,
pmCopy,
pmNotCopy (The inverse pen color),
pmMergePenNot,
pmMaskPenNot,
pmMaskNotPen,
pmMergeNotPen,
pmMerge,
pmNotMerge,
pmMask,
pmNotMask,
pmXOR,
pmNotXOR.
Use the Delphi help files to learn more about these, look for pen.mode.
An easy way to find TPen.Mode is to paste or type the text into the
Code Editor and then press the F1 key.
Make sure the caret is inside the text.
Pen Color:
Canvas.Pen.Color := clBlue;
Pen Width:
Canvas.Pen.Width := PenWidth.Position;
Pen Style:
Canvas.Pen.Style := psSolid;
Pen Styles:
| psClear |
Transparent Line |
| psDash |
Dashed Line |
| psDashDot |
Line of Dashes & Dots |
| psDashDotDot |
Line of 1 Dash & 2 Dots |
| psDot |
Dotted Line |
| psSolid |
Solid Line |
| psInsideFrame |
A solid line and it can use a dithered color with a with greater than
one pixel wide. |
Note: Windows 95 doesn't support dotted or dashed lines styles for
pens that are wider than one pixel and no matter what style you choose
makes all larger pens solid.
More examples are shown below. |
| RGB:
RGB = Red, Green, Blue.
Example:
RGB(0, 0, 0);
procedure TForm1.Button1Click(Sender: TObject);
begin
Form1.Color := RGB(100, 100, 100);
end;
Each number should be from 0 to 255.
By changing the values you can get different colors. |
| Pixel:
The upper left corner of the form is Canvas.Pixels(0, 0);
The bottom right corner is CanvasPixels(ClientWidth, ClientHeight);
Example:
This example will draw yellow stars (Dots) on
a Form(Canvas):
Start a new project.
Change the colour(Color) of Form1 to clBlack.
Add Button1.
Add this code to the button’s OnClick event handler:
procedure TForm1.Button1Click(Sender: TObject);
var I : Integer;
begin
I := 0;
begin
repeat
I := I
+ 1;
Canvas.Pixels[Random(ClientWidth),
Random(ClientHeight)] := clYellow;
until I = 10000;
end;
end;
Run the program and click on Button1. |
| The Font Property And Drawing Text:
The Canvas.Font property is used to change the appearance of the text
that is drawn on the canvas.
Font Styles
| fsItalic |
Italic |
| fsBold |
Bold |
| fsUnderLine |
Underlined |
| fsStrikeOut |
Strikeout - line through the text |
Using bold and underlined:
Canvas.Font.Style := [fsBold, fsUnderline];
Using a TFontDialog to change the forms font:
procedure TForm1.Button2Click(Sender: TObject);
begin
Canvas.TextOut(10, 10, 'Here!');{Current forms font}
if FontDialog1.Execute then
Canvas.Font.Assign(FontDialog1.Font);
Canvas.TextOut(100, 100, 'Here!');{New form
font}
end;
Examples:
Canvas.Font.Name:
Canvas.Font.Name := 'Arial';
Canvas.Font.Color:
Canvas.Font.Color := clBlue;
Large Fonts:
This example was created like this:
procedure TForm1.Button1Click(Sender: TObject);
begin
Canvas.Font.Height := Font.Size * Font.PixelsPerInch
div 10;
Canvas.Font.Name := 'Napa Heavy SF';
Canvas.TextOut(100, 200, 'Text is here!');
end;
You can also change the font size with Font.Size
Adding text to a form:
procedure TForm1.Button1Click(Sender: TObject);
begin
Canvas.TextOut(10, 30, 'Here!');
end;
Drawing a box with some text in it:
procedure TForm1.Button1Click(Sender: TObject);
var AreaBox : TRect;
begin
AreaBox := Rect(100, 100, 200, 200);
Canvas.TextRect(AreaBox,100, 100,'Inserted text');
end;
Drawing the same box as above, this time with
a blue frame:
procedure TForm1.Button1Click(Sender: TObject);
var AreaBox : TRect;
begin
AreaBox := Rect(100, 100, 200, 200);
Canvas.TextRect(AreaBox,100,100,'Inserted text');
Canvas.Brush.Color := clBlue;
Canvas.FrameRect(AreaBox);
end;
|
| To Copy One Bitmap To Another:
Bitmap.Assign(Bitmap2); |
| StretchDraw:
StretchDraw(ARect, Image1.Picture.Bitmap); |
| MoveTo:
You can use MoveTo to move the drawing position to somewhere else on
the form like this:
Canvas.MoveTo(0, 0);
Have a look at the drawing example to see MoveTo in action. |
| LineTo:
LineTo is used to draw a line from its current position to a new position.
Here is an example:
Canvas.LineTo(10, 100);
You could do this:
Canvas.MoveTo(0, 0); //Start in the top
left corner of the form.
Canvas.LineTo(10, 10); //Draw a line from 0, 0 to the new
position at 10, 10.
Have a look at the drawing example to see LineTo in action. |
GetNearestColor:
[GetNearestColor(HDC hdc, crColor);]
HDC // handle of device context
crColor // color to be matched
GetNearestColor is a Windows function that
can take a HDC and RGB color and return the nearest RGB color in the system
palette.
CLR_INVALID is the return value if the
function fails. Use GetLastError to get extended error information.
Example:
procedure TForm1.Button1Click(Sender: TObject);
var TheColor : TColorRef;
begin
TheColor := GetNearestColor(Form1.Canvas.Handle,
RGB(133, 53, 20));
Panel1.Color := TColor(TheColor);
end;
. |
| Getting The Color
Value Of A Pixel In A TImage:
To do this I added a TPanel (Panel1), and
a TImage (Image1) to a form.
Next I loaded an image into Image1.
Next I clicked on Image1 to make sure it
was selected and then I went to the Events page in the Object Inspector
and double-clicked on the OnMouseDown event.
Next I added this code:
procedure TForm2.Image1MouseDown(Sender:
TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Panel1.Color := Image1.Canvas.Pixels[X,Y];
end;
Run the example and click on an area in
Image1. You should see the color appear im Panel1 now.
Click on different areas on the image.
. |
ColorToRGB:
[function ColorToRGB(Color: TColor): Longint;]
Unit Graphics - Graphics Utilities
The ColorToRGB function is used to convert
a TColor into a RGB representation of that color.
Example:
procedure TForm1.Button1Click(Sender: TObject);
var L : LongInt;
begin
L := ColorToRGB(Form1.Color);
Panel1.Color := L;
Label1.Caption := ColorToString(L);
end;
. |
| Drawing an Ellipse:
procedure TForm1.Button1Click(Sender: TObject);
begin
Canvas.Ellipse(100, 100, 160, 160);
end; |
| Drawing a Rectangle:
procedure TForm1.Button1Click(Sender: TObject);
begin
Canvas.Rectangle(100, 100, 200, 160);
end; |
| Drawing a Polygon:
procedure TForm1.Button1Click(Sender: TObject);
begin
Canvas.Polygon([Point(0, 0), Point(50, 20),
Point(150, 50), Point(240, 120)]);
end; |
| Drawing a blue Polygon:
procedure TForm1.Button1Click(Sender: TObject);
begin
Canvas.Brush.Color := clBlue;
Canvas.Polygon([Point(0, 0), Point(50, 20),
Point(150, 50), Point(240, 120)]);
end; |
| Paint the form blue:
procedure TForm1.Button1Click(Sender: TObject);
begin
Canvas.BrushStyle := bsSolid;
Canvas.Brush.Color := clBlue;
Canvas.FillRect(ClientRect);
end;
This could be used to clear the form, in other words rub-out the current
content.
You can also use ClearCanvas. |
| Drawing a rounded Rectangle:
procedure TForm1.Button1Click(Sender: TObject);
begin
Canvas.RoundRect(0, 0, 60, 60, 40, 40);
end; |
| Using the mouse to place Rectangles on a Form:
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if ssLeft in Shift then
begin
Canvas.Rectangle(X, Y, X
+ 10, Y + 10);
end;
end;
Run the program and click the left mouse button while the mouse cursor
is on the Form. |
| Using the mouse to place rectangles on a Form
with an animated effect:
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if ssLeft in Shift then
begin
Canvas.Pen.Color :=
clBlue;
Canvas.Rectangle(X,
Y, X + 10, Y + 10);
Sleep(200);
Canvas.Rectangle(X,
Y, X + 20, Y + 20);
Sleep(200);
Canvas.Rectangle(X,
Y, X + 30, Y + 30);
Sleep(200);
Canvas.Rectangle(X,
Y, X + 40, Y + 40);
end;
end;
When you run this program click on the Form about 10 times as fast
as you can, and all over the Form. |
|
| Here is an example that puts a bitmap on the
form (canvas):
procedure TForm1.Button1Click(Sender: TObject);
var MyBitmap : TBitMap;
begin
MyBitmap := TBitmap.Create;
try
with MyBitmap do begin
LoadFromFile('D:\Delphi
3\Images\Splash\256color\handshak.bmp'); //Put your own .bmp file path
here
Form1.Canvas.Draw(10, 10,
MyBitMap);
end;
finally
MyBitmap.Free;
end;
end; |
| Different patterns in rectangles:
procedure TForm1.Button1Click(Sender: TObject);
begin
Form1.Canvas.Brush.Style := bsHorizontal;
Canvas.Rectangle(10, 10, 60, 60);
Form1.Canvas.Brush.Style := bsVertical;
Canvas.Rectangle(70, 10, 120, 60);
Form1.Canvas.Pen.Color := clBlue;
Form1.Canvas.Brush.Style := bsCross;
Canvas.Rectangle(130, 10, 180, 60);
Form1.Canvas.Brush.Style := bsBDiagonal;
Canvas.Rectangle(190, 10, 240, 60);
Form1.Canvas.Brush.Color := clYellow;
Form1.Canvas.Brush.Style := bsDiagCross;
Canvas.Rectangle(250, 10, 300, 60);
end; |
| Getting The Color
Value Of A Pixel In A TImage:
To do this I added a TPanel (Panel1), and
a TImage (Image1) to a form.
Next I loaded an image into Image1.
Next I clicked on Image1 to make sure it
was selected and then I went to the Events page in the Object Inspector
and double-clicked on the OnMouseDown event.
Next I added this code:
procedure TForm2.Image1MouseDown(Sender:
TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Panel1.Color := Image1.Canvas.Pixels[X,Y];
end;
Run the example and click on an area in
Image1. You should see the color appear im Panel1 now.
Click on different areas on the image.
. |