![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
Q: Paint on the contents of a TImage control.Answer:TImage 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. const TPoint Arrow1[] = { {80,105} , {106,66}, {80,87},{94,98}}; Image1->Canvas->Pen->Color = clBlue; Image1->Canvas->Pen->Width = 3; Image1->Canvas->MoveTo(Arrow1[0].x,Arrow1[0].y); Image1->Canvas->LineTo(Arrow1[1].x,Arrow1[1].y); Image1->Canvas->MoveTo(Arrow1[0].x,Arrow1[0].y); Image1->Canvas->LineTo(Arrow1[2].x,Arrow1[2].y); Image1->Canvas->MoveTo(Arrow1[0].x,Arrow1[0].y); Image1->Canvas->LineTo(Arrow1[3].x,Arrow1[3].y); 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. // function converted to C++ TCanvas * TImage::GetCanvas() { Graphics::TBitmap *Bitmap; if (Picture->Graphic == NULL) { Bitmap = new Graphics::TBitmap Bitmap->Width = Width; Bitmap->Height = Height; // assign new bitmap to the Graphic property. This calls the // SetGraphic write method of TPicture. SetGraphic calls Assign, // equivalent to calling Picture->Graphic->Assign(Bitmap); Picture->Graphic = Bitmap; // Delete the temp bitmap pointer. Because Assign was called, // the contents of the HBITMAP is retained in Picture->Graphic delete Bitmap; } if (/* Picture->Graphic is a TBitmap object */ ) return Picture->Bitmap->Canvas; else throw EInvalidOperation( /* some args here maybe */); } 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. Note: As a result of how TImage::GetCanvas works, there is no difference beteen Image->Canvas and Image->Picture->Bitmap->Canvas. 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->Picture->Width will be the width of CHEMICAL.BMP (240). 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. | ||||||
All rights reserved. |