We know that GIF-type image formats support transparent display, while BMP-type image formats do not support transparent display. If the bitmap is stored in a transparent manner in the Microsoft Photo Editor, an error is prompted. However, in c++builder, transparent bitmaps can be easily displayed. Transparent bitmaps have a great advantage over transparent GIF images: The GIF image format supports up to 256 colors, while bitmaps can support true color.
----display a transparent bitmap in C++builder
----1. Use Timage components.
----below, we make a simple animation of a plane flying over the blue sky. Because the moving plane is displayed with a transparent bitmap, it looks very realistic.
The----(1) Activates the menu "File/new application" under C++builder to create a new project, the form cell is filed with the file Planemain.cpp, and the project file is filed plane.cpp.
----(2) Add a TButton component Button1 on the Form1 form.
----Add a new TTimer component Timer1, add three timage components Image1, Image2, and Image3, add a tlabel.
----(3) Double-click the Image1 on the form, bluesky.bmp the bitmap of the Blue sky, set its Transpartant property to false, that is opaque, as the background; double-click Image2, and transfer into the plane's bitmap plane.bmp, Sets its transparent property to true, which is transparent, AutoSize property is true, and Image2 is placed above Image1. At this point, we can see that the plane has been integrated into the blue sky. In contrast, double-click Image3, transfer the bitmap plane.bmp to the plane, and set its transparent property to false, which is opaque, and the AutoSize property is true. Set the Label1 Caption property to "source bitmap." Set the Button1 Caption property to start. The Enabled property of the set Timer1 is False,interval 100 (determines how fast and adjustable the aircraft is flying).
----(4) Timer1 OnTimer events control the movement of the aircraft, the code is as follows:
Void__fastcalltform1::timer1timer
(tobject*sender)
{image2->left=image2->left+1;}
----The Button1 onclick event to start the aircraft, the code is as follows:
Void__fastcalltform1::button1click
(tobject*sender)
{timer1-> Enabled=true;}
------2. Directly from the file into the bitmap, and then draw on the screen. This method is flexible and consumes less memory. Continue the above program design:
----(1) Add a TButton component Button2 on the form and set its Caption property to "show Transparent bitmap." The onclick event for the
----(2) Button2 displays a transparent bitmap with the following code:
Void__fastcalltform1::button2click (Tobject*sender)
//click Show Transparent Bitmap button to enter the
{
Graphics::tbitmap*pbitmap=newgraphics::tbitmap ();
Try
{pbitmap->loadfromfile ("plane.bmp");
//Transfer bitmap
Pbitmap->transparent=true
//Set transparency Properties
Pbitm Ap->transparentcolor=
Pbitmap->canvas->pixels[2][2];
Get the
Transparent color you want to set from the bitmap, or directly give
Form1->canvas->draw (10,20,pbitmap),
//AA
//transparent bitmap on the form,
not set tra Nsparentmode
//The color of pixels at bitmap [2][2] is a transparent color
Pbitmap->transparentmode=tmauto
Set Transparent mode, Tmauto:
TransparentColor property return bit
///graph leftmost pixel color, as throughBright color; The
//tmfixed:transparentcolor property returns the color of a pixel on the bitmap
as a transparent color.
Form1->canvas->draw (10,100,PBITMAP);
//bb
//displays a transparent bitmap on a form,
differs from AA row
//The color of the pixel in the lower-left corner of the bitmap is a transparent color
Image1->canvas->draw (30,20,pbitmap);
A transparent bitmap
}
catch (...) is displayed on the background bitmap.
{
ShowMessage ("Cannot dial or display a bitmap!") ");
//Display error messages
}
deletepbitmap;//Delete to image
}
----The application of this method for image synthesis, the background image requirements are bitmaps, and transparent bitmap and background bitmap color number to be as consistent as possible, so that the color of the bitmap will not change.
----In addition, we can also display transparent images using the Timagelist component, using the Brushcopy method of the Tcanvas class to realize the partial transparency of the copied image.
----The above procedures are debugged and passed under WIN98 and C++builder3.