How to make a translucent image on a background image.

Source: Internet
Author: User
I want to display translucent data information on a map, similar to the processing of an isothermal map in the weather forecast.
How can we map the background image to the top-level image to achieve a translucent effect?
---------------------------------------------------------------

You can use transparentimage,
To solve the problem of flickering, we should use dual buffering to implement it,
Search for double buffering here to get a lot of information
---------------------------------------------------------------

You can use an API of Windows
Alphablend
Its usage is similar to bitblt. It is very useful and we always use it.

Blendfunction BF;
BF. alphaformat = 0;
BF. blendflags = 0;
BF. blendop = ac_src_over;
BF. sourceconstantalpha = 100; // transparency 0-255

Alphablend (hbackdc, hmaskdc, BF );

It cannot be used in WindowsCE.
---------------------------------------------------------------

Http://www.vckbase.com/document/viewdoc.asp? Id = 532

It provides detailed introduction and Code
---------------------------------------------------------------

You can refer to the following code in idea.
This Code creates a translucent bitmap:
The main functions are:
Getbitmapbits ();
Setbitmapbits ()
Because the dots in the bitmap file are red, green, and blue, the storage format of the bitmap file in the memory is as follows:
GBR
..............
..............
We can see from the above that each vertex occupies 3X1 pixels, and the order is arranged in GBR order. Setting the image transparency is actually to set the transparency of each vertex. Take a look at the following code! I think it should be helpful to you!
Bitmap bm;
Int X, Y, S, T, M, I = 0;
Getclientrect (& rect );
MDC = new CDC;
Bitmap = new cbitmap;
Bgbmp = new cbitmap;
MDC-> createcompatibledc (DC );
Bgbmp-> m_hobject = (hbitmap): LoadImage (null, "bground.bmp", image_bitmap, rect. Right, rect. Bottom, lr_loadfromfile );
Bitmap-> m_hobject = (hbitmap): LoadImage (null, "girl.bmp", image_bitmap, 298,329, lr_loadfromfile );
Bitmap-> GetObject (sizeof (Bitmap), & BM );
Unsigned char * PX = new unsigned char [BM. bmheight * BM. bmwidthbytes];
Bitmap-> getbitmapbits (BM. bmheight * BM. bmwidthbytes, PX );
S = BM. bmheight;
T = BM. bmwidth;
M = BM. bmwidthbytes;
For (y = 0; y <BM. bmheight; y ++)
For (x = 0; x <BM. bmwidth; X ++)
{
// PX [x * 3 + y * BM. bmwidthbytes] * = 0.5;
Px [x * 3 + y * BM. bmwidthbytes] = (unsigned char) (PX [x * 3 + y * BM. bmwidthbytes] * 0.5 );
// PX [x * 3 + 1 + y * BM. bmwidthbytes] * = 0.5;
Px [x * 3 + 1 + y * BM. bmwidthbytes] = (unsigned char) (PX [x * 3 + 1 + y * BM. bmwidthbytes] * 0.5 );
// PX [x * 3 + 2 + y * BM. bmwidthbytes] * = 0.5;
Px [x * 3 + 2 + y * BM. bmwidthbytes] = (unsigned char) (PX [x * 3 + 2 + y * BM. bmwidthbytes] * 0.5 );

}
Bitmap-> setbitmapbits (BM. bmheight * BM. bmwidthbytes, PX );
Bgbmp-> GetObject (sizeof (Bitmap), & BM );
Unsigned char * py = new unsigned char [BM. bmheight * BM. bmwidthbytes];
Bgbmp-> getbitmapbits (BM. bmheight * BM. bmwidthbytes, Py );
For (y = 100; y <100 + S; y ++)
{
I = (y-100) * m;
For (x = 100; x <100 + T; X ++)
{
// Py [x * 3 + y * BM. bmwidthbytes] = py [x * 3 + y * BM. bmwidthbytes] * 0.5 + px [I];
PY [x * 3 + y * BM. bmwidthbytes] = (unsigned char) (PY [x * 3 + y * BM. bmwidthbytes] * 0.5 + px [I]); // sets the transparency to 50%.
// Py [x * 3 + 1 + y * BM. bmwidthbytes] = py [x * 3 + 1 + y * BM. bmwidthbytes] * 0.5 + px [I + 1];
PY [x * 3 + 1 + y * BM. bmwidthbytes] = (unsigned char) (PY [x * 3 + 1 + y * BM. bmwidthbytes] * 0.5 + px [I + 1]);
// Py [x * 3 + 2 + y * BM. bmwidthbytes] = py [x * 3 + 2 + y * BM. bmwidthbytes] * 0.5 + px [I + 2];
PY [x * 3 + 2 + y * BM. bmwidthbytes] = (unsigned char) (PY [x * 3 + 2 + y * BM. bmwidthbytes] * 0.5 + px [I + 2]);
I = I + 3;
}
}
Bgbmp-> setbitmapbits (BM. bmheight * BM. bmwidthbytes, Py );
MDC-> SelectObject (bgbmp );
Delete [] PX; // This form must be used to delete an array
Delete [] py;

Hope to help you!
---------------------------------------------------------------

Of course.
You only need to # include <wingdi. h>
Then msimg32.lib is linked.
No problem !!

Some documents indicate that there is a problem under 98. But it's okay if I use it now.
Feel free to use it !!
---------------------------------------------------------------

"Alpha-blending 』
The so-called alpha-blending, in fact, is to mix the source pixel and the target pixel according to the value of the "Alpha" hybrid vector, which is generally used to process the translucent effect.

Alpha-blending technology and a brief introduction to the specific alpha-Blending Technology Algorithm .

The so-called alpha-blending is actually mixing the source and target pixels according to the value of the "Alpha" hybrid vector. For ease of understanding, let's talk about the alpha-blending process between two pixels.

The first step is to separate the RGB color components of the source pixel from the target pixel, and then multiply the three color components of the source pixel by the Alpha value, then, multiply the three color components of the target pixel by the inverse value of Alpha, and then add the results according to the corresponding color components, divide the final result of each component by the maximum value of alpha (usually this step is done by shift, which is also the reason why the maximum value of Alpha is always a power of two ), finally, the three color components are combined into a pixel output.

In this process, we usually use the Mask Method to separate the RGB color components in pixels. As for the specific three mask values: rmask, gmask, and bmask, you can get the result from ddpixelformat in DirectDraw (of course, you can also perform various flexible processing as needed ).

In the descriptive routine given below, we assume that the rmask, gmask, and bmask have stored the bitmask values of RGB three color components. The maximum Alpha value is 256, source and DEST are pointers to source and target pixels.

Int ialpha = 256-* Alpha; // The Reverse value of alpha

* DEST = (rmask & (* Source & rmask) ** Alpha + (* DEST & rmask) * ialpha)> 8 )) merge (gmask & (* Source & gmask) ** Alpha + (* DEST & gmask) * ialpha)> 8 )) values (bmask & (* Source & bmask) ** Alpha + (* DEST & bmask) * ialpha)> 8 ));

---------------------------------------------------------------

GDI +

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.