Relationship and difference between timage, tpaintbox, tpicture, tbitmap, TCanvas, and tgraphic

Source: Internet
Author: User
Tgraphic is the parent class of tbitmap. Many drawing functions have parameters of tgraphic, but we often give the function tbitmap;

Many functions of tbitmap are inherited from tgraphic, for example:
Width, height, loadfromfile, savetofile, loadfromstream, and savetostream;
There are also palette (color palette), transparent (whether transparent) and so on.

Practical example of tgraphic:

 
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 circle, painting side, paint brush, painting brush and so on;

The canvas attribute of the control is a TCanvas, for example:

 
VaR CVS: TCanvas; begin CVS: = self. canvas; CVs. brush. color: = clyellow; CVs. font. color: = clred; CVs. font. name: = ' '; CVs. font. style: = [fsbold]; CVs. font. size: = 24; CVs. textout (10, 10, 'in case of Delphi blog'); end;

  

Some controls do not directly give the canvas attribute. We can also use TCanvas to obtain its drawing surface;
All visible controls should have a drawing surface, or how is the system painted?

For example, tpanel and tbutton do not have the canvas attribute because they are not commonly used. If necessary, you can:

 
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 inherits from tgraphic and also includes TCanvas as its own attribute;
Therefore, it has the dual function of processing images and plotting!

Here is an example for tbitmap:

 
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 to process images in more formats (such as ICO and WMF;
However, tgraphic, TCanvas, and tbitmap functions are borrowed from each other, making them more powerful.

Tpicture and tgraphic duplicate functions (for example, loadfromfile) are internally called tgraphic;
To plot the image, call tpicture. bitmap. Canvas.

The following example uses tpicture to draw a cross icon (displayed and saved ):

 
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 images. It mainly includes tpicture. With tpicture, everything above is available;
However, because it is inherited from tcontrol-> tgraphiccontrol, it has the basic capabilities of controls (events, messages, and so on ).

Tpaintbox is mainly used for plotting and does not have the ability to process images. Therefore, it only contains TCanvas;
It also inherits from tcontrol-> tgraphiccontrol and is a control that can interact with each other.

Obviously, timage is more powerful than tpaintbox, but tpaintbox is lighter for plotting purposes only.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.