Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Alpha Blending a BitmapGo to Serhiy Perevoznyk's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
Win API
Skill Level:
Scoring:
Last Update:
03/18/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet Delphi API AlphiBlend
Times Scored:
3
Visits:
2568
Uploader: Serhiy Perevoznyk
Company: http://users.chello.be/ws36637
Reference: N/A
 
Question/Problem/Abstract:
Using AlphaBlend function in Delphi
Answer:



Alpha blending is used to display an alpha bitmap, which is a bitmap that has transparent or semi-transparent pixels.
In addition to a red, green, and blue color channel, each pixel in an alpha bitmap has a transparency component known as its alpha channel. The alpha channel typically contains as many bits as a color channel. For example, an 8-bit alpha channel can represent 256 levels of transparency, from 0 (the entire bitmap is transparent) to 255 (the entire bitmap is opaque). Alpha blending mechanisms are invoked by calling AlphaBlend function. The AlphaBlend function displays bitmaps that have transparent or semitransparent pixels. It is not supported on Microsoft Windows 95 or on versions of Microsoft Windows NT prior to Microsoft Windows 2000.

The following code sample divides a window into three horizontal areas. Then it draws an alpha-blended bitmap in each of the window areas.

const
AC_SRC_ALPHA = $1;

procedure DrawAlphaBlend (hWnd : HWND;  hdcwnd : HDC);
var
    Ahdc : HDC;              // handle of the DC we will create
    bf : BLENDFUNCTION;      // structure for alpha blending
    Ahbitmap : HBITMAP;      // bitmap handle
    bmi : BITMAPINFO;        // bitmap header
    pvBits : pointer;        // pointer to DIB section
    ulWindowWidth,
    ulWindowHeight : ULONG;  // window width/height
    ulBitmapWidth,
    ulBitmapHeight : ULONG; // bitmap width/height
    rt : TRect;             // used for getting window dimensions
begin
    // get window dimensions
    GetClientRect(hWnd, rt);

    // calculate window width/height
    ulWindowWidth := rt.right - rt.left;
    ulWindowHeight := rt.bottom - rt.top;

    // make sure we have at least some window size
    if ((ulWindowWidth = 0 ) and  (ulWindowHeight=0)) then
        exit;

    // divide the window into 3 horizontal areas
    ulWindowHeight := trunc(ulWindowHeight / 3);

    // create a DC for our bitmap -- the source DC for AlphaBlend
    Ahdc := CreateCompatibleDC(hdcwnd);

    // zero the memory for the bitmap info
    ZeroMemory(@bmi, sizeof(BITMAPINFO));

    // setup bitmap info
    bmi.bmiHeader.biSize := sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth := trunc(ulWindowWidth - (ulWindowWidth/5)*2);
    ulBitmapWidth := trunc(ulWindowWidth - (ulWindowWidth/5)*2);
    bmi.bmiHeader.biHeight := trunc(ulWindowHeight - (ulWindowHeight/5)*2);
    ulBitmapHeight := trunc(ulWindowHeight - (ulWindowHeight/5)*2);
    bmi.bmiHeader.biPlanes := 1;
    bmi.bmiHeader.biBitCount := 32;         // four 8-bit components
    bmi.bmiHeader.biCompression := BI_RGB;
    bmi.bmiHeader.biSizeImage := ulBitmapWidth * ulBitmapHeight * 4;

    // create our DIB section and select the bitmap into the dc
    Ahbitmap := CreateDIBSection(Ahdc, bmi, DIB_RGB_COLORS, pvBits, 0, 0);
    SelectObject(Ahdc, Ahbitmap);

    bf.BlendOp := AC_SRC_OVER;
    bf.BlendFlags := 0;
    bf.SourceConstantAlpha := $7f;  // half of 0xff = 50% transparency
    bf.AlphaFormat := 0;             // ignore source alpha channel

    AlphaBlend(hdcwnd, trunc(ulWindowWidth/5), trunc(ulWindowHeight/5),
                    ulBitmapWidth, ulBitmapHeight,
                    Ahdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf);


    bf.BlendOp := AC_SRC_OVER;
    bf.BlendFlags := 0;
    bf.AlphaFormat := AC_SRC_ALPHA;  // use source alpha
    bf.SourceConstantAlpha := $ff;  // opaque (disable constant alpha)

    AlphaBlend(hdcwnd, trunc(ulWindowWidth/5),
     trunc(ulWindowHeight/5+ulWindowHeight), ulBitmapWidth, ulBitmapHeight,
      Ahdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf);

    bf.BlendOp := AC_SRC_OVER;
    bf.BlendFlags := 0;
    bf.AlphaFormat := 0;
    bf.SourceConstantAlpha := $3A;

    AlphaBlend(hdcwnd, trunc(ulWindowWidth/5),
               trunc(ulWindowHeight/5+2*ulWindowHeight), ulBitmapWidth,
               ulBitmapHeight, Ahdc, 0, 0, ulBitmapWidth,
               ulBitmapHeight, bf);

    // do cleanup
    DeleteObject(Ahbitmap);
    DeleteDC(Ahdc);

end;





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
D. Souchard
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)