Pergunta

My problem is: if I draw some text directly on a CImage previously loaded from a .PNG file. the text itself is transparent (you can see the background color through it) and there is no way to have it of the desired color.

CImage image;
image.Load ( "myimage.png" ) ;

//Draw some text
HDC dc = image.GetDC ();
SetTextColor ( dc, RGB( 0 , 0 , 0 ) ); ///< I think the problem is here
DrawText ( dc, "Hello world!", -1, CRect ( 0 , 0 , 200 , 200 ), 0 );
image.ReleaseDC ();

//Render of the image somewhere
image.Draw ( someOutDC , 0 , 0 );

I've tried different RGBs value and .PNG with or without transparent color but same result. Only if I load from a .BMP or .JPG it works (but I need a .PNG). There's something about the .PNG but I have no idea how to correctly set the text color.

I've forgot to say that I've also tried the Alpha RGB like this:

SetTextColor ( dc , RGB (0,0,0) + 255 << 24 );

... but nothing change ... any suggestions?

Foi útil?

Solução

As @enhzflep said, GDI can't handle alpha channel correctly so when working with 32 Bpp image you have to use GDI+ functionality like this:

#include <GdiPlus.h>
#pragma comment(lib,"gdiplus.lib")

//....

Gdiplus::Graphics graphics ( image.GetDC () );
Gdiplus::Font font ( &FontFamily ( L"Arial" ), 10 );
Gdiplus::SolidBrush brush ( Color ( 255, 0, 0, 0 ) );
graphics.DrawString ( L"Hello world", -1, &font, PointF(0.0f, 0.0f), &brush );
image.ReleaseDC()

Outras dicas

You need to set the the BkMode to TRANPARENT CDC::SetBkMode. So the text color is used to draw the chars and the background is transparent.

Drawing PNG transparent with an alpha channel is not possible with GDI. You can do this with GDI+. See sample here

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top