开发者

Transparency on white background bitmap C++ winapi

well i have a question about transparency in a bitmap file which i want to save as png file, at the moment i take a screenshot of the cursor in the system and save it as png, this work fine whit arrow, hand and other cursors but in the moment of use cursors like I-beam or "not" cursor this have a problem, the explanation is that each cursor have a mask and a color bitmap which combine and result in a transparent cursor but I-beam and others no have color bitmap, th开发者_Python百科em have only mask that contains the color and mask in the same bitmap, i refer this post: C# - Capturing the Mouse cursor image well they use C# but the idea is the same.

in my code i use C++ and i manage to create a cursor but with white background color, i don't know how to convert it in transparent color, in the post i refer use a function MakeTransparent, any idea? thanks for the help :D

    CURSORINFO cursor;
    ICONINFO cursorIconInfo;
    HICON cursorIcon;
    cursor.cbSize=sizeof(CURSORINFO);
    GetCursorInfo(&cursor);
    GetIconInfo(cursor.hCursor,&cursorIconInfo);
    //cursorIcon=CopyIcon(cursor.hCursor);
    //GetIconInfo(LoadCursor(NULL,IDC_ARROW),&cursorIconInfo);
    //cursorIcon=CreateIconIndirect(&cursorIconInfo);

    CxImage* imag=new CxImage();
    /*imag->CreateFromHICON(cursorIcon);
    imag->Save("cursor.png",CXIMAGE_FORMAT_PNG);*/
    BITMAP bm;
    //CImage* imag=new CImage();
    GetObject(cursorIconInfo.hbmMask,sizeof(BITMAP),&bm);
    if(bm.bmHeight == bm.bmWidth*2){
        HDC screendc=CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
        HDC cursormaskDC=CreateCompatibleDC(screendc);
        HDC cursorfinalDC=CreateCompatibleDC(screendc);

        HBITMAP cursormask=CreateCompatibleBitmap(screendc,bm.bmWidth,bm.bmWidth);
        HBITMAP cursorfinal=CreateCompatibleBitmap(screendc,bm.bmWidth,bm.bmWidth);

        SelectObject(cursormaskDC,cursorIconInfo.hbmMask);
        SelectObject(cursorfinalDC,cursorfinal);

        BitBlt(cursorfinalDC,0,0,bm.bmWidth,bm.bmWidth,cursormaskDC,0,bm.bmWidth,SRCCOPY); 
        BitBlt(cursorfinalDC,0,0,bm.bmWidth,bm.bmWidth,cursormaskDC,0,0,SRCINVERT);

        /*cursorIconInfo.hbmColor=cursorcolor;
        cursorIconInfo.hbmMask=cursormask;
        cursorIcon=CreateIconIndirect(&cursorIconInfo);
        imag->CreateFromHICON(cursorIcon);
        imag->Save("cursorPrub.png",CXIMAGE_FORMAT_PNG);*/
        imag->CreateFromHBITMAP(cursorfinal);

        imag->Save("cursor.png",CXIMAGE_FORMAT_PNG);
        DeleteObject(cursorIconInfo.hbmMask);
        DeleteObject(cursorIconInfo.hbmColor);
        DestroyIcon(cursorIcon);

        imag->Destroy();
        return;
    }


Use GDIPlus. It work correct with alpha. Gdi works incorrect with alpha sometime.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜