How to draw ARGB bitmap using GDI+?

前端 未结 4 1216
醉梦人生
醉梦人生 2021-01-20 01:02

I have valid HBITMAP handle of ARGB type. How to draw it using GDI+?

I\'ve tried method: graphics.DrawImage(Bitmap::FromHBITMAP(m_hBitmap, NULL), 0, 0); But it doesn

相关标签:
4条回答
  • 2021-01-20 01:31

    .NET offers the static Image.FromHbitmap method. You can then use this image and invoke DrawImage on the target Graphics object.

    0 讨论(0)
  • 2021-01-20 01:39

    I've got working sample:

    // 1. Get info using bitmap handle: image size, bits
    BITMAP bmpInfo;
    ::GetObject(m_hBitmap, sizeof(BITMAP), &bmpInfo);
    int cxBitmap = bmpInfo.bmWidth;
    int cyBitmap = bmpInfo.bmHeight;
    void* bits = bmpInfo.bmBits;

    // 2. Create & draw new GDI+ bitmap using bits with pixel format PixelFormat32bppARGB
    Gdiplus::Graphics graphics(dcMemory);
    Gdiplus::Bitmap bitmap(cxBitmap, cyBitmap, cxBitmap*4, PixelFormat32bppARGB, (BYTE*)bits);
    graphics.DrawImage(&bitmap, 0, 0);

    0 讨论(0)
  • 2021-01-20 01:44

    Ah... but .Net doesn't use HBITMAP and GDI+ is a C++ library atop the basic Windows GDI, so I'm assuming you're using non-.Net C++.

    GDI+ has a Bitmap class, which has a FromHBITMAP() method.

    Once you have the GDI+ Bitmap instance, you can use it with the GDI+ library.

    Of course, if you can write your program in C# using .Net it will be a lot easier.

    0 讨论(0)
  • 2021-01-20 01:56

    I had similar issues getting my transparent channel to work. In my case, I knew what the background color should be used for the transparent area (it was solid). I used the Bitmap.GetHBITMAP(..) method and passed in the background color to be used for the transparent area. This was a much easier solution that other attempts I was trying using LockBits and re-creating the Bitmap with PixelFormat32bppARGB, as well as cloning. In my case, the Bitmap was ignoring the alpha channel since it was created from Bitmap.FromStream.

    I also had some very strange problems with the background area of my image being changed slightly. For example, instead of pure white, it was off white like 0xfff7f7. This was the case whether I was using JPG (with blended colors) or with PNG and transparent colors.

    See my question and solution at

    GDI+ DrawImage of a JPG with white background is not white

    0 讨论(0)
提交回复
热议问题