zoukankan      html  css  js  c++  java
  • 下面的代码演示了使用GetDIBits直接读取位图数据。

    

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/iamshuke/archive/2010/07/20/5749948.aspx

    下面的代码演示了使用GetDIBits直接读取位图数据。

    下面的代码演示了使用GetDIBits直接读取位图数据。

    view plaincopy to clipboardprint
    ?
    include
    <math.h>
    void CDibtestDlg::OnOK()
    {
    // TODO: Add extra validation here
    HDC hDesktopDC = ::GetDC(NULL);
    HDC hTmpDC
    = CreateCompatibleDC(hDesktopDC);
    HBITMAP hBmp
    = CreateCompatibleBitmap(hDesktopDC, 351, 250); //351x250, 示例数据
    SelectObject(hTmpDC, hBmp);
    BitBlt(hTmpDC,
    0, 0, 351, 250, hDesktopDC, 0, 0, SRCCOPY);
    DeleteObject(hTmpDC);

    BITMAP bm;
    PBITMAPINFO bmpInf;
    if(GetObject(hBmp,sizeof(bm),&bm)==0)
    {
    ::ReleaseDC(NULL,hDesktopDC);
    return ;
    }

    int nPaletteSize=0;
    if(bm.bmBitsPixel<16)
    nPaletteSize
    =(int)pow(2,bm.bmBitsPixel);

    bmpInf
    =(PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)+
    sizeof(RGBQUAD)*nPaletteSize+(bm.bmWidth+7)/8*bm.bmHeight*bm.bmBitsPixel);

    BYTE
    * buf=((BYTE*)bmpInf) +
    sizeof(BITMAPINFOHEADER)+
    sizeof(RGBQUAD)*nPaletteSize;

    //-----------------------------------------------
    bmpInf->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmpInf
    ->bmiHeader.biWidth = bm.bmWidth;
    bmpInf
    ->bmiHeader.biHeight = bm.bmHeight;
    bmpInf
    ->bmiHeader.biPlanes = bm.bmPlanes;
    bmpInf
    ->bmiHeader.biBitCount = bm.bmBitsPixel;
    bmpInf
    ->bmiHeader.biCompression = BI_RGB;
    bmpInf
    ->bmiHeader.biSizeImage = (bm.bmWidth+7)/8*bm.bmHeight*bm.bmBitsPixel;
    //-----------------------------------------------

    if(!::GetDIBits(hDesktopDC,hBmp,0,(UINT)bm.bmHeight,buf,bmpInf,DIB_RGB_COLORS))
    {
    ::ReleaseDC(NULL,hDesktopDC);
    LocalFree(bmpInf);
    return ;
    }

    ::ReleaseDC(NULL,hDesktopDC);

    CString sMsg;
    sMsg.Format(
    "BitsPixel:%d,%d,height:%d",
    bm.bmBitsPixel,bm.bmWidth,bm.bmHeight);
    AfxMessageBox(sMsg);

    CClientDC dc(
    this);

    int nOffset;
    BYTE r,g,b;
    int nWidth = bm.bmWidth*bm.bmBitsPixel/8;
    nWidth
    = ((nWidth+3)/4)*4; //4字节对齐

    if(bmpInf->bmiHeader.biBitCount == 8)
    {
    for(int i=0; i<bm.bmHeight; i++)
    {
    for(int j=0; j<bm.bmWidth; j++)
    {
    RGBQUAD rgbQ;
    rgbQ
    = bmpInf->bmiColors[buf[i*nWidth+j]];
    dc.SetPixel(j,bm.bmHeight
    -i,RGB(rgbQ.rgbRed,rgbQ.rgbGreen,rgbQ.rgbBlue)); //测试显示
    }
    }
    }
    else if(bmpInf->bmiHeader.biBitCount == 16)
    {
    for(int i=0; i<bm.bmHeight; i++)
    {
    nOffset
    = i*nWidth;
    for(int j=0; j<bm.bmWidth; j++)
    {
    b
    = buf[nOffset+j*2]&0x1F;
    g
    = buf[nOffset+j*2]>>5;
    g
    |= (buf[nOffset+j*2+1]&0x03)<<3;
    r
    = (buf[nOffset+j*2+1]>>2)&0x1F;

    r
    *= 8;
    b
    *= 8;
    g
    *= 8;

    dc.SetPixel(j, bm.bmHeight
    -i, RGB(r,g,b)); //测试显示
    }
    }
    }
    else if(bmpInf->bmiHeader.biBitCount == 24)
    {
    for(int i=0; i<bm.bmHeight; i++)
    {
    nOffset
    = i*nWidth;
    for(int j=0; j<bm.bmWidth; j++)
    {
    b
    = buf[nOffset+j*3];
    g
    = buf[nOffset+j*3+1];
    r
    = buf[nOffset+j*3+2];

    dc.SetPixel(j, bm.bmHeight
    -i, RGB(r,g,b)); //测试显示
    }
    }
    }
    else if(bmpInf->bmiHeader.biBitCount == 32)
    {
    for(int i=0; i<bm.bmHeight; i++)
    {
    nOffset
    = i*nWidth;
    for(int j=0; j<bm.bmWidth; j++)
    {
    b
    = buf[nOffset+j*4];
    g
    = buf[nOffset+j*4+1];
    r
    = buf[nOffset+j*4+2];

    dc.SetPixel(j, bm.bmHeight
    -i, RGB(r,g,b)); //测试显示
    }
    }
    }

    DeleteObject(hBmp);
    LocalFree(bmpInf);

    //CDialog::OnOK();
    }
  • 相关阅读:
    Neither the JAVA_HOME nor the JRE_HOME environment variable is defined 错误解决
    linux打包压缩的时候如何剔除某些不想打包的进来的文件
    linux修改时区和时间
    Linux scp远程文件/目录传输
    nginx报错”could not build the server_names_hash”
    配置Linux+Apache+Mysql+PHP环境
    PHP运行出现Notice : Use of undefined constant 的完美解决方案
    Silverlight 资源文件的问题 不能按指定语言切换
    Silverlight 应用 WCF RIA Services 在 IIS6 部署问题总结
    构架之累
  • 原文地址:https://www.cnblogs.com/carl2380/p/2091527.html
Copyright © 2011-2022 走看看