GDI GDI+在绘图时使指定颜色透明的方法

时间:2022-05-02 02:25:13 阅读: 最新文章 文档下载
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。
Skystalker 2011-04-30 01:53

今天学习GDIBITMAP贴图,用三种办法实现了指定颜色透明,总结如下 一、GDI下的方法,使用TransparentBlt,这是最麻烦的: CRect rc;

GetDlgItem(IDC_STATIC_1)->GetWindowRect(&rc); ScreenToClient(&rc);

CDC *pDC; pDC=GetDC(); CBitmap FootballBMP;

FootballBMP.LoadBitmap(IDB_BITMAP1); CDC ImageDC;

ImageDC.CreateCompatibleDC(pDC);

CBitmap *pOldImageBMP = ImageDC.SelectObject(&FootballBMP);

TransparentBlt(pDC->m_hDC, rc.left, rc.top, 300, 300, ImageDC.m_hDC, 0, 0, 300, 300, RGB(0xff,0xff,0));

二、把图片需要透明的地方直接做成透明的,这样用draw出来就是透明的。 二、利用CImage类,但这样做其他操作比如旋转就比较困难: CDC *pDC; pDC=GetDC(); CImage myimage;

myimage.Load(TEXT("Background.bmp"));

myimage.TransparentBlt(pDC->m_hDC, rc.left, rc.top, 300, 300, RGB(0xff,0xff,0)); 三、利用ImageAttributes::SetColorKey 方法,但据说速度比较慢: MSDN的例子有说明:

MSDN Library : Graphics and Multimedia / GDI+ / GDI+ Reference / Classed / Attributes / ImageAttributes Methods / SetColorKey

VOID Example_SetColorKey(HDC hdc) {

Graphics graphics(hdc);

// Create an Image object based on a BMP file. // The image has three horizontal stripes.

// The color of the top stripe has RGB components (90, 90, 20). // The color of the middle stripe has RGB components (150, 150, 150). // The color of the bottom stripe has RGB components (130, 130, 40). Image image(L "ColorKeyTest.bmp ");

Image
// Create an ImageAttributes object, and set its color key. ImageAttributes imAtt; imAtt.SetColorKey(

Color(100, 95, 30), // 要透明的颜色范围高位 如果把高位和低位颜色设成一样 比如都是 255 255 0就把黄色透明

Color(250, 245, 60),// 要透明的颜色范围低位 ColorAdjustTypeBitmap);

// Draw the image. Apply the color key.

// The bottom stripe of the image will be transparent because // 100 <= 130 <= 250 and // 95 <= 130 <= 245 and // 30 <= 40 <= 60. graphics.DrawImage( &image,

Rect(20, 20, image.GetWidth(), image.GetHeight()), // dest rect 0, 0, image.GetWidth(), image.GetHeight(), // source UnitPixel, &imAtt); }



rect

本文来源:https://www.wddqw.com/doc/d24dfc8fa0116c175f0e489c.html