Click here to Skip to main content
15,909,199 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: alpha channel problem Pin
Chris Losinger25-Feb-08 4:06
professionalChris Losinger25-Feb-08 4:06 
GeneralRe: alpha channel problem Pin
Mark Salsbery25-Feb-08 5:30
Mark Salsbery25-Feb-08 5:30 
GeneralRe: alpha channel problem Pin
Chris Losinger25-Feb-08 5:48
professionalChris Losinger25-Feb-08 5:48 
GeneralRe: alpha channel problem Pin
Mark Salsbery25-Feb-08 5:54
Mark Salsbery25-Feb-08 5:54 
GeneralRe: alpha channel problem Pin
Chris Losinger25-Feb-08 4:04
professionalChris Losinger25-Feb-08 4:04 
QuestionRe: alpha channel problem Pin
trioum25-Feb-08 18:07
trioum25-Feb-08 18:07 
GeneralRe: alpha channel problem Pin
Mark Salsbery26-Feb-08 6:04
Mark Salsbery26-Feb-08 6:04 
GeneralRe: alpha channel problem Pin
Mark Salsbery26-Feb-08 7:01
Mark Salsbery26-Feb-08 7:01 
Here's an AlphaBlend() example, using what I know from your sample code.
Note this assumes the FreeImage image is 32bpp with alpha channel...
<font color="Green">// FreeImage functions for Mark's reference :)
//FreeImage_GetWidth(pFIBitmap)
//FreeImage_GetHeight(pFIBitmap)
//FreeImage_GetBits(pFIBitmap)
//FreeImage_GetInfo(pFIBitmap)
//FreeImage_GetInfoHeader(pFIBitmap)</font>

LONG lImageWidth = FreeImage_GetWidth(pFIBitmap);
LONG lImageHeight = FreeImage_GetHeight(pFIBitmap);
WORD wBitsPerPixel = 32;
 
LONG lBytesPerRow = (((lImageWidth * (long)wBitsPerPixel + 31L) & (~31L)) / 8L);
 
BITMAPINFO bmi;
memset(&bmi, 0, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = lImageWidth;
bmi.bmiHeader.biHeight = lImageHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = wBitsPerPixel;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = lBytesPerRow * lImageHeight;
<font color="Green">//bmi.bmiHeader.biXPelsPerMeter = 0;
//bmi.bmiHeader.biYPelsPerMeter = 0;
//bmi.bmiHeader.biClrUsed = 0;
//bmi.bmiHeader.biClrImportant = 0;</font>

HDC hdc = ::CreateCompatibleDC(0);

BYTE* pBitmapBits;
 
HBITMAP hBitmap = ::CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)&pBitmapBits, NULL, 0);

if (hBitmap)
{   
    <font color="Green">//// Test - fill the DIBsection pixel data with arbitrary ARGB values
    //RGBQUAD *pCurPixel = (RGBQUAD *)pBitmapBits;
    //int PixelCount = lImageWidth * lImageHeight;
    //while (PixelCount > 0)
    //{
    //    (*pCurPixel).rgbRed =      0x00;
    //    (*pCurPixel).rgbGreen =    0x00;
    //    (*pCurPixel).rgbBlue =     0xA0;
    //    (*pCurPixel).rgbReserved = 0x80;  <font color="Red">//<--- per-pixel alpha channel value goes here</font>
    //    pCurPixel++;
    //    PixelCount--;
    //}</font>

    <font color="Green">// Copy freeimage pixel data to the DIBSection</font> <font color="Red">- NOTE!!! Assumes source image is 32-bit data and same size!!</font>
    memcpy(pBitmapBits, FreeImage_GetBits(pFIBitmap), bmi.bmiHeader.biSizeImage);

    HGDIOBJ hOldBitmap = ::SelectObject(hdc, hBitmap);

    HDC hClientDC = ::GetDC(hwndWindowToDrawOn);

    BLENDFUNCTION bf;
    bf.BlendOp = AC_SRC_OVER;
    bf.BlendFlags = 0;
    bf.SourceConstantAlpha = 0xFF;
    bf.AlphaFormat = AC_SRC_ALPHA;

    ::AlphaBlend(hClientDC, 0, 0, lImageWidth, lImageHeight,
                        hdc, 0, 0, lImageWidth, lImageHeight, bf);

    ::ReleaseDC(hwndWindowToDrawOn, hClientDC);
    ::SelectObject(hdc, hOldBitmap);
    ::DeleteObject(hBitmap);
}

::DeleteDC(hdc);

Mark






Mark Salsbery
Microsoft MVP - Visual C++

Java | [Coffee]

GeneralRe: alpha channel problem Pin
trioum26-Feb-08 19:12
trioum26-Feb-08 19:12 
GeneralRe: alpha channel problem Pin
Mark Salsbery26-Feb-08 20:38
Mark Salsbery26-Feb-08 20:38 
GeneralEnable The Button Control Pin
bhat24-Feb-08 20:07
bhat24-Feb-08 20:07 
GeneralRe: Enable The Button Control Pin
Maxwell Chen24-Feb-08 20:12
Maxwell Chen24-Feb-08 20:12 
GeneralRe: Enable The Button Control Pin
bhat24-Feb-08 20:26
bhat24-Feb-08 20:26 
GeneralRe: Enable The Button Control Pin
Maxwell Chen24-Feb-08 20:34
Maxwell Chen24-Feb-08 20:34 
GeneralRe: Enable The Button Control Pin
bhat24-Feb-08 21:05
bhat24-Feb-08 21:05 
GeneralRe: Enable The Button Control Pin
Hamid_RT25-Feb-08 6:53
Hamid_RT25-Feb-08 6:53 
Generalexception in string Pin
George_George24-Feb-08 19:41
George_George24-Feb-08 19:41 
GeneralRe: exception in string Pin
James R. Twine25-Feb-08 4:41
James R. Twine25-Feb-08 4:41 
GeneralRe: exception in string Pin
George_George25-Feb-08 18:24
George_George25-Feb-08 18:24 
GeneralRe: exception in string Pin
James R. Twine25-Feb-08 23:45
James R. Twine25-Feb-08 23:45 
GeneralRe: exception in string Pin
George_George26-Feb-08 14:09
George_George26-Feb-08 14:09 
GeneralRe: exception in string Pin
James R. Twine27-Feb-08 1:03
James R. Twine27-Feb-08 1:03 
GeneralRe: exception in string Pin
George_George27-Feb-08 3:04
George_George27-Feb-08 3:04 
Generalcomandline compilation Pin
johnalek24-Feb-08 19:38
johnalek24-Feb-08 19:38 
GeneralRe: comandline compilation Pin
Maxwell Chen24-Feb-08 19:43
Maxwell Chen24-Feb-08 19:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.