Alpha blending bitmaps using GDI+

24 Jan

In a 2D image element which stores a color for each pixel(RGB values ),
an additional value is stored.Thats called the alpha channel.
It contains a value ranging from 0 to 1.value 0 means image is fully transparent.
value 1 means fully opeque

Here our focus is to change the value of the alpha channel.
In GDI+ it is much easier to change the alpha channel of an image (compared to GDI ).

We have to use GDI+ header file. #include Gdiplusimaging.h

Lets assum that our image is in the hard disk.
so first we have to load the image.

//define an Image pointer

Image* m_PImage;

Load(const TCHAR* pszPathName)
m_pImage = Image::FromFile(T2CW(pszPathName),m_bUseEmbeddedColorManagement);



if (m_pImage)
delete m_pImage;
m_pImage = NULL;

//have to pass the associater device context as a parameter.

AlphaBlend(CDC* pDC)
Graphics g(pDC->m_hDC);

float fBlend=m_nAlpha; //set the alpha value

ColorMatrix BitmapMatrix = {
1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, fBlend, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 1.0f

ImageAttributes ImgAttr;


//Image coordinates and size
RectF destination(m_nX,m_nY,m_nWidth,m_nHeight);

g.DrawImage(m_pImage,destination, 0,0,m_pImage->GetWidth(),m_pImage->GetHeight(),UnitPixel,&ImgAttr);


1 Comment

Posted by on January 24, 2008 in technology


Tags: , , ,

One response to “Alpha blending bitmaps using GDI+

  1. mos

    January 29, 2008 at 3:49 am

    Awesome, thanks. Was going crazy trying to find this in the docs, should have just used Google to start with. 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: