注意:只针对32位带alpha通道的BMP!
第一种方法(显示小图标不清晰):
//加载位图CImage sourceImage;sourceImage.LoadFromResource(AfxGetInstanceHandle(), IDB_BMP_NX);//新建位图CImage showImage;showImage.Create(sourceImage.GetWidth(), sourceImage.GetHeight(), 32, CImage::createAlphaChannel);//转换数据if (sourceImage.GetBPP() == 32) //确认该图像包含Alpha通道 ,即改图片是否是32位像素{for (int i = 0; i < sourceImage.GetWidth(); i++){for (int j = 0; j < sourceImage.GetHeight(); j++){byte* pByte = (byte*)sourceImage.GetPixelAddress(i, j);byte* pByte2 = (byte*)showImage.GetPixelAddress(i, j);if (pByte[3] < 255)//alpha通道值{pByte2[0] = (pByte[0] * pByte[3] + 127) / 255;pByte2[1] = (pByte[1] * pByte[3] + 127) / 255;pByte2[2] = (pByte[2] * pByte[3] + 127) / 255;pByte2[3] = pByte[3];}else{pByte2[0] = pByte[0];pByte2[1] = pByte[1];pByte2[2] = pByte[2];pByte2[3] = pByte[3];}}}}//显示位图showImage.Draw(dc.GetSafeHdc(), 0, 0, sourceImage.GetWidth(), sourceImage.GetHeight(), 0, 0, showImage.GetWidth(), showImage.GetHeight());
第二种方法(推荐:小图显示清晰细腻):
//关联DCGraphics graphics(dc.m_hDC);//加载位图HBITMAP hbmp = (HBITMAP)::LoadImage(GetModuleHandle(NULL),"C:\\Users\\HP\\Desktop\\replace_part.8s.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);//获取位图数据BITMAP bm;GetObject(hbmp, sizeof(bm), &bm);BYTE* source = (BYTE*)bm.bmBits;//新建32位带alpha通道的BitmapBitmap bitmap(bm.bmWidth, bm.bmHeight, PixelFormat32bppARGB);//拷贝位图数据Gdiplus::BitmapData datas;Gdiplus::Rect rc(0, 0, bm.bmWidth, bm.bmHeight);bitmap.LockBits(&rc, ImageLockModeWrite, PixelFormat32bppARGB, &datas);byte* dest = (byte*)(datas.Scan0);for (int i = 0; i < bm.bmHeight; i++){memcpy(dest + (i * bm.bmWidthBytes),source + ((bm.bmHeight - i - 1) * bm.bmWidthBytes), bm.bmWidthBytes);}bitmap.UnlockBits(&datas);//绘制位图graphics.DrawImage(&bitmap, 0, 0);//删除资源DeleteObject(hbmp);