22 Mart 2012 Perşembe

Draw Alpha Blend Bitmap




const
 AC_SRC_ALPHA = $1;


procedure DrawAlphaBlend (Bitmap:TBitmap; hdcwnd : HDC; Alpha: Byte);
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
    canvas: TCanvas;
begin
    // calculate window width/height
    ulWindowWidth := Bitmap.Width;
    ulWindowHeight := Bitmap.Height;

    // 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 := ulWindowWidth;
    ulBitmapWidth := ulWindowWidth;
    bmi.bmiHeader.biHeight := ulWindowHeight;
    ulBitmapHeight := ulWindowHeight;
    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);

    canvas := TCanvas.Create;
    canvas.Handle := Ahdc;
    canvas.Draw(0,0,Bitmap);

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

    AlphaBlend(hdcwnd, 0, 0,
                    ulBitmapWidth, ulBitmapHeight,
                    Ahdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf);

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

end;

Hiç yorum yok:

Yorum Gönder