资源描述:
《gdi gdi+在绘图时使指定颜色透明的方法》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、Skystalker2011-04-3001:53今天学习GDI的BITMAP贴图,用三种办法实现了指定颜色透明,总结如下一、GDI下的方法,使用TransparentBlt,这是最麻烦的: CRectrc; GetDlgItem(IDC_STATIC_1)->GetWindowRect(&rc); ScreenToClient(&rc); CDC*pDC; pDC=GetDC(); CBitmapFootballBMP; FootballBMP.LoadBitmap(IDB_BITMAP1); CDCImageDC; ImageDC.CreateCompatibleDC(pDC); CBi
2、tmap*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(); CImagemyimage; myimage.Load(TEXT("Background.bmp")); myimage.Tr
3、ansparentBlt(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 / ImageAttributes / ImageAttributes Methods / SetColorKeyVOID Example_SetColorKey(HDC hdc){ Graphics gra
4、phics(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
5、(130, 130, 40). Image image(L"ColorKeyTest.bmp"); // Create an ImageAttributes object, and set its color key. ImageAttributes imAtt; imAtt.SetColorKey( Color(100, 95, 30), // 要透明的颜色范围高位 如果把高位和低位颜色设成一样 比如都是2552550,那就把黄色透明 Color(250, 245, 60),// 要透明的颜色范围低位 C
6、olorAdjustTypeBitmap); // 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()),
7、// dest rect 0, 0, image.GetWidth(), image.GetHeight(), // source rect UnitPixel, &imAtt);}