16位颜色模式下bmp的显示
要在wince的pda上显示bmp图片,已经在模拟环境里成功的显示了
各种图片,1bpp,2bpp,4bpp,8bpp,16bpp,24bpp,32bpp。现在移植
到pda的16位颜色模式下却不能显示其中的大部分图片。我考虑到
模拟环境是在pc机上的,支持24位颜色模式,而pda只有16位颜色
模式,而且不支持调色板。那位大虾能够帮忙看一下我的代码,
移植以后在16位颜色模式下应该做那些改动?我实在是找不出来
原因所在了!不胜感激
程序使用了createdibsection函数来生成hbitmap句柄,没有使用任何
调色板,在vc,embeded vc下运行正常
读文件代码如下:
struct DIBINFO : public BITMAPINFO
{
RGBQUAD arColors[255]; // Color table info - adds an extra 255 entri
to palette
operator LPBITMAPINFO() { return (LPBITMAPINFO) this; }
operator LPBITMAPINFOHEADER() { return &bmiHeader; }
RGBQUAD* ColorTable() { return bmiColors; }
};
CFile file;
if (!file.Open(lpszFileName, CFile::modeRead))
return false;
DWORD dwFileStart = file.GetPosition();
BITMAPFILEHEADER BmpFileHdr;
int nBytes;
nBytes = file.Read(&BmpFileHdr, sizeof(BmpFileHdr));
if (nBytes != sizeof(BmpFileHdr))
{
TRACE0("Failed to read file header\n");
return false;
}
// Check that we have the magic 'BM' at the start.
if (BmpFileHdr.bfType != ((WORD) ('M' << 8) | 'B'))
{
TRACE0("Not a bitmap file\n");
return false;
}
nBytes = file.Read(&bmInfo, sizeof(BITMAPINFOHEADER));
if (nBytes != sizeof(BITMAPINFOHEADER))
{
TRACE0("Failed to read BITMAPINFOHEADER\n");
return FALSE;
}
// Check that we have a real Windows DIB file.
if (bmInfo.bmiHeader.biSize != sizeof(BITMAPINFOHEADER))
{
TRACE0(" File is not a Windows DIB\n");
return FALSE;
}
int nColorSize = NumColorEntries(bmInfo.bmiHeader.biBitCount);
if (nColorSize > 0 )
{
// Read the color table from the file.
int nColorTableSize = nColorSize * sizeof(RGBQUAD);
nBytes = file.Read(bmInfo.ColorTable(), nColorTableSize);
if (nBytes != nColorTableSize)
{
TRACE0("Failed to read color table\n");
return FALSE;
}
}
int nBitsSize = BmpFileHdr.bfSize - BmpFileHdr.bfOffBits;
// Allocate the memory for the bits and read the bits from the file.
BYTE* pBits = (BYTE*) malloc(nBitsSize);
if (!pBits)
{
TRACE0("Out of memory for DIB bits\n");
return FALSE;
}
// Seek to the bits in the file.
file.Seek(dwFileStart + BmpFileHdr.bfOffBits, CFile::begin);
// read the bits
nBytes = file.Read(pBits, nBitsSize);
if (nBytes != nBitsSize)
{
TRACE0("Failed to read bits\n");
free(pBits);
return FALSE;
}
// Everything went OK.
bmInfo.bmiHeader.biSizeImage = nBitsSize;
if (!&bmInfo || !pBits)
return false;
HDC hDC = NULL;
hDC = ::GetDC(NULL);
if (!hDC)
{
TRACE0("Unable to get DC\n");
AfxThrowResourceException();
}
VOID *m_ppvBits;
m_hBitmap = CreateDIBSection(hDC, (const BITMAPINFO *)&bmInfo,
DIB_RGB_COLORS, &m_ppvBits, NULL, 0);
::ReleaseDC(NULL, hDC);
if (!m_hBitmap)
{
TRACE0("CreateDIBSection failed\n");
AfxThrowResourceException();
}
DWORD dwImageSize = bmInfo.bmiHeader.biSizeImage;
memcpy(m_ppvBits, pBits, dwImageSize);
显示的代码如下
CShowBmpTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (pDoc->m_hBitmap)
{
CDC pMemDC;
pMemDC.CreateCompatibleDC(pDC);
if (!pMemDC.GetSafeHdc())
return ;
HBITMAP m_hOldBitmap;
m_hOldBitmap = (HBITMAP) ::SelectObject(pMemDC.GetSafeHdc(), pDoc->m_hBi
p);
if (!m_hOldBitmap)
{
pMemDC.DeleteDC();
return ;
}
BOOL bResult = pDC->StretchBlt(pDoc->m_destRect.left,
pDoc->m_destRect.top,
pDoc->bmInfo.bmiHeader.biWidth,
pDoc->bmInfo.bmiHeader.biHeight,
&pMemDC,
pDoc->m_srcRect.left ,
p);
if (!m_hOldBitmap)
{
pMemDC.DeleteDC();
return ;
}
BOOL bResult = pDC->StretchBlt(pDoc->m_destRect.left,
pDoc->m_destRect.top,
pDoc->bmInfo.bmiHeader.biWidth,
pDoc->bmInfo.bmiHeader.biHeight,
&pMemDC,
pDoc->m_srcRect.left ,
pDoc->m_srcRect.top ,
pDoc->bmInfo.bmiHeader.biWidth ,
pDoc->bmInfo.bmiHeader.biHeight,
SRCCOPY);
pMemDC.DeleteDC();
pDC->SelectObject(m_hOldBitmap);
--