下面的代码演示了使用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,width:%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();
}

转载于:https://www.cnblogs.com/carl2380/archive/2011/06/27/2091527.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值