QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2487|回复: 0
打印 上一主题 下一主题

Paint on the contents of a TImage control.

[复制链接]
字体大小: 正常 放大
韩冰        

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2005-1-26 12:46 |只看该作者 |倒序浏览
|招呼Ta 关注Ta

Paint on the contents of a TImage control.

8 j0 N$ c1 {" S! t6 ?/ i" ] & \% ^$ @ B, M

Paint on the contents of a TImage control.

: Q, Q; ~/ e3 x1 l

6 o" @! T1 h' O" \. z+ N) O Answer: % Q" W: K. f; b* wTImage provides a Canvas property that allows you to draw on the contents of the image. Changes made to the image become a permanent part of the image. If you copy the resulting image to the clipboard or save the image to a file, your changes will appear right with the image. Here is a code snippet that draws an arrow onto an image.

6 w+ O% k( s7 a: l

const TPoint Arrow1[] = { {80,105} , {106,66}, {80,87},{94,98}}; 2 Y" ?3 M6 N+ @, I/ K Image1->Canvas->en->Color = clBlue; 5 Z- r' |5 C5 F( X" `) k Image1->Canvas->en->Width = 3;% E+ d" d7 q7 M7 q- I% h' _. a' S, g& X Image1->Canvas->MoveTo(Arrow1[0].x,Arrow1[0].y);' U! R6 n: A4 z Image1->Canvas->LineTo(Arrow1[1].x,Arrow1[1].y); ) j5 b Q3 T T4 L# T# p5 w Image1->Canvas->MoveTo(Arrow1[0].x,Arrow1[0].y); 9 D) F. z( P' C6 |1 @ Image1->Canvas->LineTo(Arrow1[2].x,Arrow1[2].y); L7 I5 l! c6 F; o% x! a Image1->Canvas->MoveTo(Arrow1[0].x,Arrow1[0].y); * Y4 o' W3 ~( P Image1->Canvas->LineTo(Arrow1[3].x,Arrow1[3].y);

0 n2 K( a0 e3 z+ k+ j& c9 j

Note: You can only draw on the Canvas property of TImage if the Picture is empty or contains a bitmap as its graphic. You cannot draw on the Canvas of TImage if the image contains an icon or a metafile as its graphic. The GetCanvas read method of TImage demonstrates why.

% g. x$ a2 [2 K7 j$ X

// function converted to C++) V$ \% b$ ]2 f1 a; d- p TCanvas * TImage::GetCanvas() ) I6 T" [, e! c) _2 ]; U2 _9 A{' O: { q4 v3 d, N/ }9 l0 N Graphics::TBitmap *Bitmap;

# Z _0 F# L, |6 r7 z3 E, R

if (Picture->Graphic == NULL) - Y- ?% a2 F7 `* `1 I' L- q {# Q' `! k0 [0 a" c: ^ Bitmap = new Graphics::TBitmap! e1 T( ^, q8 @ Bitmap->Width = Width;" P" Z# }+ c7 W0 P( Z; U6 \ Bitmap->Height = Height;

! m1 [0 t4 c- d# H5 ?

// assign new bitmap to the Graphic property. This calls the# @/ e- s0 Q7 M$ w ? // SetGraphic write method of TPicture. SetGraphic calls Assign, 1 u" T" t5 N# d( \% d/ r // equivalent to calling Picture->Graphic->Assign(Bitmap); & D7 T$ m' ?6 [! U0 j$ S- x7 i) | Picture->Graphic = Bitmap;

* i1 C/ ?1 E$ ]

// Delete the temp bitmap pointer. Because Assign was called," Y: D. z: F% q* \ // the contents of the HBITMAP is retained in Picture->Graphic . B [' R2 N$ V, c. H delete Bitmap; / S* C# y4 c8 ?) _2 u1 c# g+ h }

4 i. z( h% i6 F8 \

if (/* Picture->Graphic is a TBitmap object */ )/ k1 d+ _. j4 U) _' X5 @, R return Picture->Bitmap->Canvas; 8 ?- Y& X5 U1 P, { else, |% p+ P2 w: K" X5 j `' ]- w; ?0 g throw EInvalidOperation( /* some args here maybe */); 2 G* B+ `- F: H+ ^* Q {}

/ j0 A% R$ w2 s S

Notice that if the Picture property is empty, a new bitmap object is created for you to draw in. If a bitmap is loaded, then TImage::GetCanvas simply returns the Canvas of the Bitmap. The code generates an exception if the Picture is an icon or a metafile.

+ Y$ E& j; n1 q5 V9 x: a4 _ e

Note: As a result of how TImage::GetCanvas works, there is no difference beteen Image->Canvas and Image->icture->Bitmap->Canvas.

4 y% ^3 D7 \1 ~; A' R8 u8 C

Note: The width and the height of an Image control's drawing canvas are identical to the original bitmap size. This may differ from the Width and Height of the image if the bitmap has been stretched. For example, place an Image control onto a new form. Size the form so it is 500 x 400 pixels. Set the Align property of the Image control to alClient, and load in the CHEMICAL.BMP splash screen that ships with BCB. Finally, change the Stretch property to true to make the picture expand to fit the entire form. At this point, Image1->Width will equal Form1->ClientWidth, but Image1->icture->Width will be the width of CHEMICAL.BMP (240).

6 W4 k- M( ]8 ^

Note: When you draw on an image, try to use only the standard Windows colors or a color that is already conained in the palette of the Image. 2 |# U3 Y2 [% t# X/ F; J

zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-4-18 10:43 , Processed in 0.272929 second(s), 52 queries .

回顶部