请教怎样打开一个24位图

htting 2007-11-21 11:31:14
初学vc
想在mfc工程中打开一个24位图片
在网上下载了一个例子
其中file->open中只有下面一段程序
void CImageViewApp::OnFileOpen()
{
static int nIndex = 1;
char szFilter[] = "BMP Files(*.BMP)|*.BMP|All Files(*.*)|*.*||";

CPreviewFileDlg FileDlg( TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter );
FileDlg.m_ofn.nFilterIndex = (DWORD) nIndex;

if( FileDlg.DoModal() == IDOK ){
CString PathName = FileDlg.GetPathName();
PathName.MakeUpper();
OpenDocumentFile( PathName );
nIndex = (int) FileDlg.m_ofn.nFilterIndex;
}

}
照抄之后却还是打不开图,请高手指点折是为什么,怎样才能打开图呢
谢谢
...全文
105 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
d_bc 2007-11-22
  • 打赏
  • 举报
回复
楼上的把bitmap的结构介绍的很清楚了,OpenDocumentFile( PathName ); 返回CFile*,通过这个CFile的到文件handle,然后传给楼主提供的函数,就可以操作这个bitmap的内存了。


windows下可以利用APIs简单方便的操作bitmap,比如LoadBitmap(),CBitmap...
youyifang 2007-11-21
  • 打赏
  • 举报
回复
下面的一段代码是返回一个设备无关位图的读取位图:

HANDLE ReadDIBFile(HANDLE hFile)
{
BITMAPFILEHEADER bmfHeader;
DWORD dwBitsSize;
UINT nNumColors; // Number of colors in table
HANDLE hDIB;
HANDLE hDIBtmp; // Used for GlobalRealloc() //MPB
LPBITMAPINFOHEADER lpbi;
DWORD offBits;
DWORD dwRead;

// get length of DIB in bytes for use when reading

dwBitsSize = GetFileSize(hFile, NULL);

// Allocate memory for header & color table. We'll enlarge this
// memory as needed.

hDIB = GlobalAlloc(GMEM_MOVEABLE, (DWORD)(sizeof(BITMAPINFOHEADER) +
256 * sizeof(RGBQUAD)));

if (!hDIB)
return NULL;

lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);

if (!lpbi)
{
GlobalFree(hDIB);
return NULL;
}

// read the BITMAPFILEHEADER from our file

if (!ReadFile(hFile, (LPBYTE)&bmfHeader, sizeof (BITMAPFILEHEADER),
&dwRead, NULL))
goto ErrExit;

if (sizeof (BITMAPFILEHEADER) != dwRead)
goto ErrExit;

if (bmfHeader.bfType != 0x4d42) // 'BM'
goto ErrExit;

// read the BITMAPINFOHEADER

if (!ReadFile(hFile, (LPBYTE)lpbi, sizeof(BITMAPINFOHEADER), &dwRead,
NULL))
goto ErrExit;

if (sizeof(BITMAPINFOHEADER) != dwRead)
goto ErrExit;

// Check to see that it's a Windows DIB -- an OS/2 DIB would cause
// strange problems with the rest of the DIB API since the fields
// in the header are different and the color table entries are
// smaller.
//
// If it's not a Windows DIB (e.g. if biSize is wrong), return NULL.

if (lpbi->biSize == sizeof(BITMAPCOREHEADER))
goto ErrExit;

// Now determine the size of the color table and read it. Since the
// bitmap bits are offset in the file by bfOffBits, we need to do some
// special processing here to make sure the bits directly follow
// the color table (because that's the format we are susposed to pass
// back)

if (!(nNumColors = (UINT)lpbi->biClrUsed))
{
// no color table for 24-bit, default size otherwise

if (lpbi->biBitCount != 24)
nNumColors = 1 << lpbi->biBitCount; // standard size table
}

// fill in some default values if they are zero

if (lpbi->biClrUsed == 0)
lpbi->biClrUsed = nNumColors;

if (lpbi->biSizeImage == 0)
{
lpbi->biSizeImage = ((((lpbi->biWidth * (DWORD)lpbi->biBitCount) +
31) & ~31) >> 3) * lpbi->biHeight;
}

// get a proper-sized buffer for header, color table and bits

GlobalUnlock(hDIB);
hDIBtmp = GlobalReAlloc(hDIB, lpbi->biSize + nNumColors *
sizeof(RGBQUAD) + lpbi->biSizeImage, 0);

if (!hDIBtmp) // can't resize buffer for loading
goto ErrExitNoUnlock; //MPB
else
hDIB = hDIBtmp;

lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);

// read the color table

ReadFile (hFile, (LPBYTE)(lpbi) + lpbi->biSize,
nNumColors * sizeof(RGBQUAD), &dwRead, NULL);

// offset to the bits from start of DIB header

offBits = lpbi->biSize + nNumColors * sizeof(RGBQUAD);

// If the bfOffBits field is non-zero, then the bits might *not* be
// directly following the color table in the file. Use the value in
// bfOffBits to seek the bits.

if (bmfHeader.bfOffBits != 0L)
SetFilePointer(hFile, bmfHeader.bfOffBits, NULL, FILE_BEGIN);

if (ReadFile(hFile, (LPBYTE)lpbi + offBits, lpbi->biSizeImage, &dwRead,
NULL))
goto OKExit;

ErrExit:
GlobalUnlock(hDIB);

ErrExitNoUnlock:
GlobalFree(hDIB);
return NULL;

OKExit:
GlobalUnlock(hDIB);
return hDIB;
}

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧