Relationships and differences between TImage, TPaintBox, TPICture, TBitmap, TCanvas, TGraphic in Delphi
var g: TGraphic; begin g: = TBitmap.Create; g.LoadFromFile ('c: \ temp \ test.bmp'); Self.Canvas.StretchDraw (ClientRect, g); g.Free; end ;
TCanvas is a drawing surface, such as drawing circles, painting squares, brushes, painting brushes, etc. are all its functions;
the Canvas property of the control is a TCanvas, such as:
var cvs: TCanvas; begin cvs: = Self.Canvas; cvs.Brush.Color: = clYellow; cvs.Font.Color: = clRed; cvs.Font.Name: = 'Songti'; cvs.Font.Style: = [fsBold]; cvs.Font.Size: = twenty four; cvs.TextOut (10, 10, 'In case of a Delphi blog'); end ;
Some controls do not directly give the Canvas property, we can also get its drawing surface through TCanvas;
all visible controls should have a drawing surface, otherwise how does the system draw?
For example, TPanel and TButton have no Canvas property, not because Infrequently, if needed:
var cvs: TCanvas; begin cvs: = TCanvas.Create; cvs.Handle: = GetDC (Panel1.Handle); cvs.Pen.Width: = 2; cvs.Pen.Color: = clRed; cvs.Brush.Color: = clYellow; cvs.Rectangle (5, 5, 50, 30); cvs.Free; end ;
Besides TBitmap, it inherits from TGraphic, and at the same time incorporates TCanvas as its own properties;
so it has the dual function of processing pictures and drawings!
Give TBitmap an example:
var bit: TBitmap; begin bit: = TBitmap.Create; bit.LoadFromFile ('c: \ temp \ test.bmp'); bit.Canvas.Brush.Style: = bsClear; bit.Canvas.Pen.Color: = clRed; bit.Canvas.Pen.Width: = 2; bit.Canvas.Ellipse (2, 2, 88, 66); Self.Canvas.Draw (0, 0, bit); bit.Free; end ;
TPicture exists in order to process more formats of pictures (such as: ico, wmf, etc.);
but it borrows the functions of TGraphic, TCanvas, TBitmap, so it is more powerful.
TPicture and TGraphic duplicated functions (such as: LoadFromFile) are internally called TGraphic;
but if you want to draw it, you need to call: TPicture.Bitmap.Canvas.
The following example draws a cross icon (displayed and saved) using TPICture:
var pic: TPicture; begin pic: = TPicture.Create; pic.Bitmap.SetSize (32, 32); pic.Bitmap.Canvas.Pen.Color: = clRed; pic.Bitmap.Canvas.Pen.Width: = 8; pic.Bitmap.Canvas.MoveTo (0, 16); pic.Bitmap.Canvas.LineTo (32, 16); pic.Bitmap.Canvas.MoveTo (16, 0); pic.Bitmap.Canvas.LineTo (16, 32); Self.Canvas.Draw (4, 4, pic.Graphic); pic.SaveToFile ('c: \ temp \ test.ico'); pic.Free; end ;
TImage is mainly used to display pictures.It mainly contains TPICture. With TPiture, it has everything above;
but because it is inherited from TControl-> TGraphicControl, it has the basic capabilities of the control (events, messages Etc.).
TPaintBox is mainly used for drawing, and does not have the ability to process pictures, so it only contains TCanvas;
it also inherits from TControl-> TGraphicControl and is an interactive control.
Obviously, TImage is more powerful than TPaintBox; but only As far as drawing is concerned, TPaintBox is more portable.