Display a transparent bitmap in C + + Builder

Source: Internet
Author: User
Tags transparent color

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.

Related Article

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.