gpt4 book ai didi

c++ - 直接在从 .png 加载的 CImage 上绘制时设置文本颜色

转载 作者:行者123 更新时间:2023-11-28 07:09:24 25 4
gpt4 key购买 nike

我的问题是:如果我直接在之前从 .PNG 文件加载的 CImage 上绘制一些文本。文本本身是透明的(您可以通过它看到背景颜色)并且无法使其具有所需的颜色。

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 );

我尝试了不同的 RGB 值和带有或不带有透明颜色的 .PNG,但结果相同。只有当我从 .BMP 或 .JPG 加载时它才有效(但我需要一个 .PNG)。.PNG 有点问题,但我不知道如何正确设置文本颜色。

忘记说了我也像这样尝试过 Alpha RGB:

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

...但没有任何变化...有什么建议吗?

最佳答案

正如@enhzflep 所说,GDI 无法正确处理 alpha channel ,因此在处理 32 Bpp 图像时,您必须像这样使用 GDI+ 功能:

#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()

关于c++ - 直接在从 .png 加载的 CImage 上绘制时设置文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21255217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com