Jump to content
  • Advertisement
Sign in to follow this  

Gdiplus: DrawImage not working?

This topic is 692 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

I need some help from you: I try to render an SVG image on the screen (with alpha channel). The problem is, when I try to draw the image on the screen, I see absolutely nothing.
First of all, my SVG image is loaded correctly, and the ATL CImage from the below code, contains a correct image. The problem start when I try to use Gdiplus + alpha channel.
For instance, if I use PixelFormat24bppRGB instead of PixelFormat32bppARGB, the image is drawn correctly, but alpha channel is not preserved - a black halo is shown around my image.
Do you see any problem in my code?

PS: I use Win7, 32 bit + Visual Studio 2013 Ultimate.

Many thanks!

void CAppView::OnDraw(CDC* pDC)

    int width = 600, height = 600;

    GError* pError = NULL;

    g_my_svg = rsvg_handle_new_from_file("d:\\myImage.svg", &pError);
    rsvg_handle_get_dimensions(g_my_svg, &g_dimension);

    cairo_surface_t *surface = cairo_win32_surface_create_with_dib(CAIRO_FORMAT_ARGB32, width, height);
    cairo_t* cr = cairo_create(surface);

    rsvg_handle_render_cairo(g_my_svg, cr);

    HDC srcHDC = cairo_win32_surface_get_dc(surface);

    // Create ATL CImage, then, copy the content of srcHDC in the CImage HDC
    CImage image;
    image.Create(width, height, 32);

    HDC imageHDC = image.GetDC();

    TransparentBlt(imageHDC, 0, 0, width, height, srcHDC, 0, 0, width, height, RGB(0, 0, 0));

    // Initialize Gdiplus
    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

// Problems starts from this point.

    // Create a Gdiplus bitmap, and copy the content of CImage in bitmap, then write the bitmap on the screen.
    Gdiplus::Bitmap bmp(image.GetWidth(), image.GetHeight(), image.GetPitch(), PixelFormat32bppARGB, static_cast<BYTE*>(image.GetBits()));
    Gdiplus::Graphics graphics(pDC->GetSafeHdc());
    graphics.DrawImage(&bmp, Gdiplus::Rect(0, 0, width, height));


Share this post

Link to post
Share on other sites

Hi jariRG,


I already tried, no change. I tried also to load data into that gdiplus::bitmap in many other ways, but again, no change. If that bitmap contains alpha channel, DrawImage shows nothing. It's wird because, for instance, if I create that bitmap using another constructor like:


    Gdiplus::Bitmap bmp(image.GetWidth(), image.GetHeight(), PixelFormat32bppARGB);


then, I set color to some pixels, like this:


    for (int x = 0; x < 300; x++)

        for (int y = 0; y < 300; y++)

        bmp.SetPixel (x, y, myColor);


DrawImage works perfect in this case.



Try PixelFormat32bppPARGB, premultiplied ARGB format.

Share this post

Link to post
Share on other sites

Actually the problem was related to the way of copying from srcHDC to ImageHDC. I have done like this:

HDC svgDC = cairo_win32_surface_get_dc(surface);

image.Create(width, height, 32);


HDC imageHDC = image.GetDC();

AlphaBlend(imageHDC, 0, 0, width, height, svgDC, 0, 0, width, height, blend);


Best regards!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!