Bitmap操作

Bitmap操作 

LoadBmpFile(char* pFileName)
{
    HANDLE hFile;  
    HBITMAP hBitMap;

    BITMAPFILEHEADER bf;
    BITMAPINFOHEADER bi;
  
    // First,Open the bitmap file to get the necessary information.
    hFile=CreateFile(pFileName,GENERIC_READ,FILE_SHARE_READ,
              NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);                                    
    if(hFile==INVALID_HANDLE_VALUE)
    {
        MessageBox(_T("File open failed!"),_T("Error"),MB_OK);
        return;
    }

    DWORD dwRead;
    BOOL bResult; 

    // get the information of BITMAPFILEHEADER
    bResult=ReadFile(hFile,&bf,sizeof(BITMAPFILEHEADER),&dwRead,NULL);
    if(!bResult)
    {
        CloseHandle(hFile);
        return;
    }
    // get the information of BITMAPINFOHEADER
    bResult=ReadFile(hFile,&bi,sizeof(BITMAPINFOHEADER),&dwRead,NULL);
    if(!bResult)
    {
        CloseHandle(hFile);
        return;
    }

    DWORD dwNumColors;
    if(bi.biClrUsed!=0)
    {
        dwNumColors=bi.biClrUsed;
    }
    else
    {
        switch(bi.biBitCount)
        {
            case 1:
                dwNumColors=2;
                break;
            case 4:
                dwNumColors=16;
                break;
            case 8:
                dwNumColors=256;
                break;
            case 16:
                dwNumColors=256*256;
                break;
            case 24:
                dwNumColors=0;
                break;
            default:
                MessageBox(_T("Error!"),NULL,MB_OK);
                CloseHandle(hFile);
                return;
        }
    }
    
    if(bf.bfOffBits!=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+dwNumColors*sizeof(RGBQUAD))
    {
        MessageBox(_T("ERROR"),NULL,MB_OK);
        CloseHandle(hFile);
        return;
    }

    DWORD dwPaletteSize;
    DWORD dwLineBytes,dwDataSize;

    dwPaletteSize=dwNumColors*sizeof(RGBQUAD);
    dwLineBytes=(bi.biWidth*bi.biBitCount+31)/32*4;
    dwDataSize=dwLineBytes*bi.biHeight;

    if(bf.bfSize!=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+dwNumColors*sizeof(RGBQUAD)         

               +dwDataSize)
    {
        MessageBox(_T("File size error"),NULL,MB_OK);
    }
    
    // Next,allocate memory to store the bitmap file's data(content).
    HGLOBAL hGlobal=GlobalAlloc(GHND,sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwDataSize);
    LPVOID lpGlobal=GlobalLock(hGlobal);

    SetFilePointer(hFile,sizeof(BITMAPFILEHEADER),NULL,FILE_BEGIN);
    bResult=ReadFile(hFile,lpGlobal,sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwDataSize,&dwRead,NULL);
    if(!bResult)
    {
        CloseHandle(hFile);
        return;
    }

    // Third,use the CreateDIBitmap function to create a compatible bitmap (DDB) from a DIB 
    HDC hDC=GetDC()->m_hDC;
    hBitMap=CreateDIBitmap(hDC,&bi,CBM_INIT,(LPSTR)lpGlobal+sizeof(BITMAPINFOHEADER)+dwPaletteSize,     

                     (BITMAPINFO*)lpGlobal,DIB_RGB_COLORS);
    GlobalUnlock(hGlobal);

    // Last,use the BitBlt function to show the image.
    HDC hMemDC;
    hMemDC=CreateCompatibleDC(GetDC()->m_hDC);
    SelectObject(hMemDC,hBitMap);
    BitBlt(GetDC()->m_hDC,0,0,bi.biWidth,bi.biHeight,hMemDC,0,0,SRCCOPY);
    DeleteDC(hMemDC);
}

使用:
1.在OnPaint函数中加入LoadBmpFile("c://test.bmp");程序运行后test将自动显示在对话框窗口上。
2.添加Button按钮,在其消息响应函数中加入LoadBmpFile("c://test.bmp");则在按下此Button时test显示。

对bitmap file进行操作的一般步骤:
1.获取位图信息:使用CreateFile和ReadFile打开和读取bitmap file的相关信息。
2.获取位图数据:使用GlobalAlloc分配足够的内存,并将bitmap file的data存入其中。
3.创建位图:如果需要对bitmap进行平移、旋转、放大等操作,则先对GlobalAlloc分配的内存中的bitmap内容进行操 

  作(具体算法参考相关书籍),再调用CreateDIBitmap创建bitmap,否则直接调用CreateDIBitmap创建bitmap。
4.显示和保存位图:显示bitmap通过BitBlt函数,保存bitmap可以通过文件写操作。

注意:
1.设备相关位图与设备无关位图的区别。
2.一些bitmap function也可以完成某些bitmap操作。例如bitmap的放大和缩小就可以通过StretchBlt函数完成。所以
  没有必要再用有关算法去实现bitmap的放大和缩小功能。而bitmap的平移和旋转等其它操作,并没有提供相应的函数 
  因此需要自己使用相关算法实现。
3.Windows LoadBitmap和LoadImage函数使用(这里Load的bitmap必须是工程的资源文件,所以必须先将bitmap添加到 

工程中)。

The LoadBitmap function loads the specified bitmap resource from a module's executable file.

HBITMAP LoadBitmap(
  HINSTANCE hInstance,  // handle to application instance
  LPCTSTR lpBitmapName  // name of bitmap resource
);

hInstance 
[in] Handle to the instance of the module whose executable file contains the bitmap to be loaded. 
lpBitmapName 
[in] Pointer to a null-terminated string that contains the name of the bitmap resource to be loaded.

Alternatively, this parameter can consist of the resource identifier in the low-order word and zero in

the high-order word. The MAKEINTRESOURCE macro can be used to create this value.


    HBITMAP hBitmap;
    // 第二个参数用MAKEINTRESOURCE形式,好像不能用文件名.
    hBitmap = LoadBitmap(theApp.m_hInstance,MAKEINTRESOURCE(IDB_BITMAP1));
    if ( hBitmap != NULL )
    {
        HDC hMemDC,hDC;
        hDC = GetDC()->m_hDC;
        hMemDC=CreateCompatibleDC(hDC);
        SelectObject(hMemDC,hBitmap);   
        BitBlt(hDC,0,0,200,200,hMemDC,0,0,SRCCOPY);
        DeleteDC(hMemDC);     
    }

4.位图的创建:上面主要是bitmap的Load操作(读取一个已有的bitmap文件),下面介绍bitmap的创建。

Creating the Bitmap:

When you set the MIIM_BITMAP or MF_BITMAP type flag for a menu item, you must also specify a handle to

the bitmap that the system should display for the menu item. You can provide the bitmap as a bitmap

resource or create the bitmap at run time. If you use a bitmap resource, you can use the LoadBitmap

function to load the bitmap and obtain its handle.

To create the bitmap at run time, use Microsoft Windows Graphics Device Interface (GDI) functions. GDI

provides several ways to create a bitmap at run time, but developers typically use the following method:

1.Use the CreateCompatibleDC function to create a device context compatible with the device context used

by the application's main window. 
2.Use the CreateCompatibleBitmap function to create a bitmap compatible with the application's main

window or use the CreateBitmap function to create a monochrome bitmap. 
3.Use the SelectObject function to select the bitmap into the compatible device context. 
4.Use GDI drawing functions, such as Ellipse and LineTo, to draw an image into the bitmap.

 

 

http://blog.csdn.net/fxwzzbd/archive/2007/12/03/1912762.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值