- 注册时间
- 2021-4-16
- 最后登录
- 2024-3-8
- 在线时间
- 3 小时
编程入门
- 龙马币
- 96
|
VS2015用CxImage读入各种图片格式后在内存中转换为HBITMAP位图
- //将CxImage转为位图
- HBITMAP Draw::ChangeCxImageToBMP(HDC hDC,CxImage* cximage)
- {
- //创建HBITMAP
- HBITMAP BMP=CreateCompatibleBitmap(hDC,cximage->GetWidth(),cximage->GetHeight());
- BITMAP bm;
- GetObject(BMP,sizeof(BITMAP),&bm);
- int pb=bm.bmBitsPixel/8; //获取位图位数
- int BytesPerLineOut = (bm.bmWidth*pb + 3) / 4 * 4; //计算每行像素的数据长度
- BYTE *pxd = new unsigned char[bm.bmHeight*BytesPerLineOut]; //声明暂存数组
- if(cximage->GetBpp()>=16)
- {
- //直接获取像素数据
- int pbx=cximage->GetBpp()/8; //获取CxImage图片的位数
- int BytesPerLine = (bm.bmWidth*pbx + 3) / 4 * 4; //计算CxImage每行像素的数据长度
- BYTE *pDib = cximage->GetBits(); //获取CxImage图片的像素数据
- for (int i = 0; i < bm.bmHeight; i++)
- {
- for (int j = 0; j < bm.bmWidth; j++)
- {
- int srcRGB_B=(bm.bmHeight-1-i)*BytesPerLine+j*pbx;
- int desRGB_B=i*bm.bmWidthBytes+j*pb;
- pxd[desRGB_B] = pDib[srcRGB_B];
- pxd[desRGB_B+1] = pDib[srcRGB_B+1];
- pxd[desRGB_B+2] = pDib[srcRGB_B+2];
- }
- }
- }
- else
- {
- //通过调色板获取像素数据
- for (int i = 0; i < bm.bmHeight; i++)
- {
- for (int j = 0; j < bm.bmWidth; j++)
- {
- RGBQUAD srcRGB_B=cximage->GetPixelColor(j,bm.bmHeight-1-i);
- int desRGB_B=i*bm.bmWidthBytes+j*pb;
- pxd[desRGB_B] = srcRGB_B.rgbBlue;
- pxd[desRGB_B+1] = srcRGB_B.rgbGreen;
- pxd[desRGB_B+2] = srcRGB_B.rgbRed;
- }
- }
- }
- SetBitmapBits(BMP,bm.bmHeight*bm.bmWidthBytes,pxd);
- delete[] pxd;
- return BMP;
- }
复制代码
|
|