MFC: Alpha Blending with bitmap

 

See the snippet for Alpha Blending with bitmap

void CAlphaBlendingBitmapDlg::DrawBitmap()
{
    CDC* pDC = GetDC();
    ASSERT(pDC);
    if (!pDC)
        return;

    CBitmap bitmap;
    // loading the bitmap
    bitmap.LoadBitmap(IDB_BITMAP_SCN);

    // get the bitmap size
    BITMAP bmp;
    bitmap.GetBitmap(&bmp);

    // create memory DC
    CDC memDc;
    memDc.CreateCompatibleDC(pDC);
    memDc.SelectObject(&bitmap);

    // giving the blend option
    BLENDFUNCTION blndFxn = {0};
    blndFxn.BlendOp = AC_SRC_OVER;
    blndFxn.SourceConstantAlpha = m_nAlpha;
    blndFxn.AlphaFormat = AC_SRC_ALPHA;

    // drawing into the dialog
    pDC->AlphaBlend(50,11,bmp.bmWidth,bmp.bmHeight,
    &memDc,0,0,bmp.bmWidth,bmp.bmHeight,blndFxn);
}
 
  • http://www.blogger.com/profile/15477351 Martijn

    Is that about bleding in flash?

  • http://sarathc.wordpress.com/ S a r a t h

    No…
    It’s concerned with a bitmap

  • thilani

    Can you please tell me what are the properties that change when Alpha blend an Bitmap.

    I want to alpha blend a image and want to save it as CBitmap objet, where other code can
    change the image properties like width ,height of the alphablend image.

    Appreciate a quick reply

    regards
    Thilani

  • thilani

    Hi folks..

    Alpha blendilng is much easier in GDI+ .
    We can use the GDI+ Image class

    Image * m_pImage ;

    m_pImage = Image::FromFile(T2CW(pszPathName),m_bUseEmbeddedColorManagement);

    Graphics g(pDC->m_hDC); //pDC is the pointer to CDC
    float fBlend=m_nAlpha;

    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;
    ImgAttr.SetColorMatrix(&BitmapMatrix,
    ColorMatrixFlagsDefault,
    ColorAdjustTypeBitmap);

    RectF destination(m_nX,m_nY,m_nWidth,m_nHeight);//Image dimentions

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