在MFC中将8位,24位位图转换为32位 [英] converting 8 bit, 24 bit Bitmap to 32 bit in mfc

查看:202
本文介绍了在MFC中将8位,24位位图转换为32位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VC ++中将8位,24位位图转换为32位

解决方案


假设您正在询问 Windows(tm)位图 [ ^ ]最简单的方法是使用Windows图像库:

- GDI + [ Windows Imaging Component [ CxImage [ GDI [ CreateDIBSection [ BITMAPINFO [ BitBlt [


converting 8 bit, 24 bit Bitmap to 32 bit in VC++

解决方案

Hi,
Supposing your are asking about
Windows(tm) bitmaps[^] the simplest is to use a Windows image library:

- GDI+[^] distributed with XP and over and downloadable for previous Win OS.
- The Windows Imaging Component[^] distributed with Vista and over.
- CxImage[^] from CodeProject which is system independant.

With any of these read the library documentation and use the provided API (should be few lines of code in any of them).

Alternatively you can use the GDI[^] API:
Assuming you have a bmp0 HBITMAP with 8 or 24 bits color depth:
Create a dc0 memory DC,
Select bmp0 into dc0,
Use CreateDIBSection[^] with dc0 and a correctly initialized BITMAPINFO[^] with bmp0 size and 24 bits color depth. The result is a bmp1 HBITMAP,
Create a dc1 memory dc compatible with dc0,
Select bmp1 into dc1, keep track of the returned bmpDef HBITMAP,
BitBlt[^] dc0 into dc1,
Select bmpDef into dc1.
DeleteObject() everything except bmp1,

You are done :)

cheers,
AR


I have some ideas:
- when converting 24 to 32 bit you can simply copy components of color, and put maximum value to alpha;
- when converting 8 to 32 bit you you can mutilply all components to value of maximum 32 bit color component divided to maximum 8 bit color component


HBITMAP convert_8to32(HBITMAP hbmp)
{
    BITMAP  bmp;
    if(GetObject(hbmp,sizeof(bmp),&bmp))
    {
        if(8==bmp.bmBitsPixel)
        {
            int             cy    = 0<bmp.bmheight?bmp.bmheight:-bmp.bmheight;
            unsigned int    bpl8  = (bmp.bmWidth+3)&~3;
            unsigned int    bpl32 = 4*bmp.bmWidth;
            unsigned char*  lp8   = (unsigned char*)malloc(bpl8*cy);
            unsigned char*  lp32  = (unsigned char*)malloc(bpl32*cy);
            HDC             mdc   = CreateCompatibleDC(0);
            BITMAPINFO*     pbmi  = (BITMAPINFO*)malloc(sizeof(BITMAPINFO)+(256*sizeof(RGBQUAD)));
            int             x,y;
            unsigned char*  lpdst = lp32;
            unsigned char*  lpsrc = lp8;

            pbmi->bmiHeader.biSize = sizeof(BITMAPINFO)+(256*sizeof(RGBQUAD));
            GetDIBits(mdc,hbmp,0,cy,lp8,pbmi,DIB_RGB_COLORS);
            for(y=0;y<cy;y++)
            {
                for(x=0;x<bmp.bmwidth;x++)
                {
                    lpdst[(x<<2)+0] = pbmi->bmiColors[lpsrc[x]].rgbBlue;
                    lpdst[(x<<2)+1] = pbmi->bmiColors[lpsrc[x]].rgbGreen;
                    lpdst[(x<<2)+2] = pbmi->bmiColors[lpsrc[x]].rgbRed;
                    lpdst[(x<<2)+3] = 0x00;
                }
                lpdst += bpl32;
                lpsrc += bpl8;
            }

            pbmi->bmiHeader.biSize          = sizeof(BITMAPINFO);
            pbmi->bmiHeader.biPlanes        = 1;
            pbmi->bmiHeader.biBitCount      = 32;
            pbmi->bmiHeader.biCompression   = 0;
            pbmi->bmiHeader.biSizeImage     = bpl32*cy;
            pbmi->bmiHeader.biClrUsed       = 0;
            pbmi->bmiHeader.biClrImportant  = 0;

            HBITMAP hbmp32 = CreateDIBitmap(mdc,&pbmi->bmiHeader,CBM_INIT,lp32,pbmi,DIB_RGB_COLORS);

            DeleteDC(mdc);
            free(pbmi);
            free(lp8);
            free(lp32);
            return hbmp32;
        }
    }
    return 0;
}


have not tested yet - but should work.

code reformatted.
-Emilio-

@-Emilio-: thanks, but there are some ''>'' too and i dont know how to remove. ps: hey i''ve got it.


这篇关于在MFC中将8位,24位位图转换为32位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆