RAD Studio VCL Reference
|
Provides a drawing surface for embellishing bitmap images.
property Canvas: TCanvas;
__property TCanvas Canvas;
Canvas is the TCanvas object on which the image for the TImage component is drawn. The painting of the image specified in the Picture property is automatic and does not require manual intercession for the picture to appear.
You can use properties and methods of TCanvas to do such things as draw lines, draw images, and write text to the TImage control's canvas.
C++ Examples:
/* This example creates a region and selects this region as the clipping rectangle for the Image component's canvas. It then sets the canvas's brush color to red and calls FillRect using the ClipRect as the area to fill. Lastly, the ClipRect is reset to the original value that it contained by calling SelectClipRect with nil as the second parameter and deletes the region. Place a TImage object in the form. */ void __fastcall TForm1::Button1Click(TObject *Sender) { HRGN MyRgn; MyRgn = ::CreateRectRgn(100,100,200,200); ::SelectClipRgn(Image1->Canvas->Handle,MyRgn); Image1->Canvas->Brush->Color = clRed; Image1->Canvas->FillRect(Image1->Canvas->ClipRect); Image1->Invalidate(); ::SelectClipRgn(Image1->Canvas->Handle,NULL); ::DeleteObject(MyRgn); }
/* This example converts a specified icon to a bitmap. To run this example, add an image, a button, and an Open dialog to a form. Name the button ConvertIcon2Bitmap, and add the following code as its OnClick event handler. */ #include <memory> //for STL auto_ptr class void __fastcall TForm1::ConvertIcon2BitmapClick(TObject *Sender) { OpenDialog1->DefaultExt = ".ICO"; OpenDialog1->Filter = "icons (*.ico)|*.ICO"; OpenDialog1->Options << ofOverwritePrompt << ofFileMustExist << ofHideReadOnly; if (OpenDialog1->Execute()) { std::auto_ptr<TIcon> pi(new Graphics::TIcon); AnsiString as; pi->LoadFromFile(OpenDialog1->FileName); as = ChangeFileExt(OpenDialog1->FileName,".BMP"); Image1->Width = pi->Width; Image1->Height = pi->Height; Image1->Canvas->Draw(0, 0, pi.get()); Image1->Picture->SaveToFile(as); ShowMessage(OpenDialog1->FileName + " Saved to " + as); } }
/* The following code uses ClientRect to find and draw a line from the top left to the bottom right of the current control. Select a TControl in the ListBox list. This example assumes that the current object is a descendent of TControl and has a public Canvas property. */ { TPoint APoint = Point(X, Y); int Index = ListBox1->ItemAtPos(APoint, True); TImage *myImage; TClass ClassRef = ListBox1->Items->Objects[Index]->ClassType(); if (String(ClassRef->ClassName()) == "TImage") { myImage = dynamic_cast<TImage *>(ListBox1->Items->Objects[Index]); // make sure we are using the TControls canvas and not the from's! myImage->Canvas->MoveTo(myImage->ClientRect.Left, myImage->ClientRect.Top); myImage->Canvas->LineTo(myImage->ClientRect.Right, myImage->ClientRect.Bottom); myImage = NULL; } }
/* The following example paints a cross-hatched ellipse onto Image1 which is a TImage on the form when the user clicks on Button1. */ void __fastcall TForm1::Button1Click(TObject *Sender) { TCanvas *pCanvas = Image1->Canvas; pCanvas->Brush->Color = clRed; pCanvas->Brush->Style = bsDiagCross; pCanvas->Ellipse(0, 0, Image1->Width, Image1->Height); }
Delphi Examples:
{ This example creates a region and selects this region as the clipping rectangle for the Image component's canvas. It then sets the canvas's brush color to red and calls FillRect using the ClipRect as the area to fill. Lastly, the ClipRect is reset to the original value that it contained by calling SelectClipRect with nil as the second parameter and deletes the region. Place a TImage object in the form. } procedure TForm1.Button1Click(Sender: TObject); var MyRgn: HRGN; begin MyRgn := CreateRectRgn(100,100,200,200); SelectClipRgn(Image1.Canvas.Handle,MyRgn); Image1.Canvas.Brush.Color := clRed; Image1.Canvas.FillRect(Image1.Canvas.ClipRect); Image1.Invalidate; SelectClipRgn(Image1.Canvas.Handle, HRGN(nil)); DeleteObject(MyRgn); end;
{ This example converts a specified icon to a bitmap. To run this example, add an image, a button, and an Open dialog to a form. Name the button ConvertIcon2Bitmap, and add the following code as its OnClick event handler. } procedure TForm1.ConvertIcon2BitmapClick(Sender: TObject); var s : string; Icon: TIcon; begin OpenDialog1.DefaultExt := '.ICO'; OpenDialog1.Filter := 'icons (*.ico)|*.ICO'; OpenDialog1.Options := [ofOverwritePrompt, ofFileMustExist, ofHideReadOnly ]; if OpenDialog1.Execute then begin Icon := TIcon.Create; try Icon.Loadfromfile(OpenDialog1.FileName); s:= ChangeFileExt(OpenDialog1.FileName,'.BMP'); Image1.Width := Icon.Width; Image1.Height := Icon.Height; Image1.Canvas.Draw(0,0,Icon); Image1.Picture.SaveToFile(s); ShowMessage(OpenDialog1.FileName + ' Saved to ' + s); finally Icon.Free; end; end; end;
{ The following code uses ClientRect to find and draw a line from the top left to the bottom right of the current control. Select a TControl in the ListBox list. This example assumes that the current object is a descendent of TControl and has a public Canvas property. } if (ListBox1.Items.Objects[Index] is TImage) then begin myImage:= (ListBox1.Items.Objects[Index] as TImage); with myImage.ClientRect do begin // make sure we are using the TControl's canvas and not the form's! myImage.Canvas.MoveTo(Left, Top); myImage.Canvas.LineTo(Right, Bottom); end; myImage:= nil; end;
{ The following example paints a cross-hatched ellipse onto Image1 which is a TImage on the form when the user clicks on Button1. } procedure TForm1.Button1Click(Sender: TObject); begin with Image1 do begin Canvas.Brush.Color := clRed; Canvas.Brush.Style := bsDiagCross; Canvas.Ellipse(0, 0, Image1.Width, Image1.Height); end; end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|