BITMAP m_bmp;
CBitmap m_bitmap;
读取 1.bmp 到内存:
BOOL CYourClassName::Read1bmp()
{
HBITMAP hBitmap = (HBITMAP)::LoadImage(NULL, _T("1.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (NULL == hBitmap || !m_bitmap.Attach(hBitmap))
{
MessageBox(_T("1.bmp 读取失败。"));
return FALSE;
}
if (!m_bitmap.GetBitmap(&m_bmp) || 32 != m_bmp.bmBitsPixel)
{
MessageBox(_T("转成 32bit 的位图。"));
return FALSE;
}
DWORD count = m_bmp.bmWidth * m_bmp.bmHeight;
m_bmp.bmBits = new DWORD [count];
if (NULL == m_bmp.bmBits)
{
MessageBox(_T("申请内存失败。"));
OnCancel();
}
m_bitmap.GetBitmapBits(count * sizeof(DWORD), m_bmp.bmBits);
}
这样就都在 m_bmp.bmBits 里了。
然后用这个函数读:
DWORD CYourClassName::GetColorDWORD(const CPoint & point)
{
DWORD index = point.x * m_bmp.bmBitsPixel / 8 + point.y * m_bmp.bmWidthBytes;
DWORD realColor = *reinterpret_cast<DWORD*>((BYTE*)m_bmp.bmBits + index);
return realColor;
}