-2

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, them 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;
    }
Community
  • 1
  • 1
  • No, the idea is certainly **not** the same. The C# code that you're trying to copy is written using the .NET Framework, which not only wraps GDI+, but also provides a host of convenient, reusable code libraries for the programmer. You have to do all the work yourself when you're writing native, unmanaged C++ because those libraries haven't already been written for you. You can't simply translate C# code into C++ code, but it's not a language problem, it's a library problem. The `MakeTransparent()` function will be nowhere to be found. – Cody Gray - on strike Jul 07 '11 at 08:25
  • thanks cody i investigate that and you are right, but then how i convert a bitmap with white background to a transparent bitmap?, i use BitBlt that suggest for that, and use SRCAND and SRCPAINT to do the job but does not work, i got a black image or a white image, i don't know how to use the I-beam mask for create transparency :/ – Diego Fernando Murillo Valenci Jul 08 '11 at 18:03

1 Answers1

-1

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

Unick
  • 644
  • 9
  • 23
  • hello thanks, i not used gdi i use CxImage that contains jpeglib is very nice and simple but the problem is not from the library is more from logic i guess, in the post that i refer they use a function MakeTransparent (C#) simply but in C++ this function not exist – Diego Fernando Murillo Valenci Jul 05 '11 at 18:55
  • This answer isn't very useful. It doesn't explain *how* the asker is supposed to use GDI+ from a C++ application. And it's also quite vague: "Gdi works incorrect with alpha sometime". No it doesn't. It works exactly as it was designed. There's nothing random about it, and it doesn't happen only sometimes. Much more useful would be explaining the limitations of GDI and comparing them with GDI+. – Cody Gray - on strike Jul 07 '11 at 08:26
  • I mean, what GDI+ supports alpha blending. "First, GDI+ expands on the features of GDI by providing new capabilities, such as gradient brushes and alpha blending." (http://msdn.microsoft.com/en-us/library/ms536338(VS.85).aspx). Sorry I does not have sample using GDI+, but Internet contains many samples. I think bad answer is better than nothing. – Unick Jul 07 '11 at 10:47
  • Yeah, notice I didn't say you were *wrong*, I said the answer isn't very useful. Incorporating the link to MSDN would have been a good start on improving your answer. As would explaining that the missing link is alpha blending. GDI only supports a very primitive form of transparency, where one color is replaced throughout the image with a transparent color. It doesn't have any alpha channels. Your answer, though, doesn't explain all of that. It doesn't tell someone who doesn't know anything about GDI+ or alpha channels anything that they need to know. – Cody Gray - on strike Jul 09 '11 at 05:28