CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2015
    Posts
    8

    [RESOLVED] Gdiplus: DrawImage not working?

    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!

    Code:
    void CAppView::OnDraw(CDC* pDC)
    {
    
    	int width = 600, height = 600;
    
    	GError* pError = NULL;
    
    	rsvg_init();
    	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));
    
    
    
    	Gdiplus::GdiplusShutdown(gdiplusToken);
    	image.ReleaseDC();
    	image.Destroy();
    	cairo_surface_flush(surface);
    	cairo_destroy(cr);
    	cairo_surface_destroy(surface);
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Gdiplus: DrawImage not working?

    Well, I don't see any error checking in the code snippet you posted.

    Is it impossible ? Or you have just omitted it?
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2015
    Posts
    8

    Re: Gdiplus: DrawImage not working?

    Thanks Victor, I have resolved the problem.
    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);

    image.SetHasAlphaChannel(TRUE);

    HDC imageHDC = image.GetDC();

    BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
    AlphaBlend(imageHDC, 0, 0, width, height, svgDC, 0, 0, width, height, blend);

    image.ReleaseDC();

    Best regards!

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: [RESOLVED] Gdiplus: DrawImage not working?

    Glad to see you solved it!
    Victor Nijegorodov

  5. #5
    Join Date
    Oct 2019
    Posts
    1

    Re: [RESOLVED] Gdiplus: DrawImage not working?

    Hi Isaak,

    From your code it seems you successfuly compiled Librsvg (or got even pre-built version). Could you provide me with the instructions how you achieved it? Or is there any tutorial how to built it?
    I am struggling with it for almost 3 days, can not even understand from which point should I start...

    Thank you in advance.

    P.S. if some Users who see this message thread may help me with building, I would be very thankful

  6. #6
    Join Date
    Mar 2020
    Posts
    5

    Re: [RESOLVED] Gdiplus: DrawImage not working?

    Isaak, you are genius. Thanks for being useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured