VC内存映像读取BMP文件问题
voidCMainWindow::OnFileOpen(){CFileDialogfile(TRUE);if(!file.DoModal())return;//打开要映射...
void CMainWindow::OnFileOpen()
{
CFileDialog file(TRUE);
if(!file.DoModal())
return;
//打开要映射的文件
HANDLE hFile = ::CreateFile(file.GetPathName(), GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
MessageBox(file.GetPathName());
if(hFile == INVALID_HANDLE_VALUE)
{
int err = GetLastError();
MessageBox("错误");
}
//创建内存映射对象
HANDLE hMap = ::CreateFileMapping(hFile, NULL, PAGE_READONLY, NULL, NULL, NULL);
if(hMap == INVALID_HANDLE_VALUE)
{
MessageBox("失败1");
return;
}
//映射整个BMP文件到内存,返回这块内存的首地址
LPVOID lpBase = ::MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
//下面是获取BMP文件信息的过程
BITMAPFILEHEADER *pFileHeader;
BITMAPINFO *pInfoHeader;
//取得file-header指针,以获得位图像素
pFileHeader = (BITMAPFILEHEADER*)lpBase;
if(pFileHeader->bfType != MAKEWORD('B', 'M'))
{
MessageBox("本程序仅读取BMP文件");
::UnmapViewOfFile(lpBase);
::CloseHandle(hMap);
::CloseHandle(hFile);
return;
}
BYTE *pBits = (BYTE*)lpBase + pFileHeader->bfOffBits;
//取得info-header指针,以获得文件的大小
pInfoHeader = (BITMAPINFO*)((BYTE)lpBase + sizeof(BITMAPFILEHEADER));
m_nHeight = pInfoHeader->bmiHeader.biHeight;
m_nWidth = pInfoHeader->bmiHeader.biWidth;
//下面是显示BMP文件到内存设备的过程
CClientDC dc(this);
//创建一个与指定DC兼容的未初始化的位图, 选入到内存兼容DC中
HBITMAP hBitmap = ::CreateCompatibleBitmap(dc, m_nWidth, m_nHeight);
::SelectObject(m_hMemDC, hBitmap);
//把图像像据放到建立的设备中
int nRet = ::SetDIBitsToDevice(m_hMemDC,
0,
0,
m_nWidth,
m_nHeight,
0,
0,
0,
m_nHeight,
pBits,
pInfoHeader,
DIB_RGB_COLORS);
::InvalidateRect(m_hWnd, NULL, TRUE);
::DeleteObject(hBitmap);
::UnmapViewOfFile(lpBase);
::CloseHandle(hMap);
}
这是部分代码
调试的时候 运行到m_nHeight = pInfoHeader->bmiHeader.biHeight;
m_nWidth = pInfoHeader->bmiHeader.biWidth;
提示“读取位置 0x00000016 时发生访问冲突” 这是什么原因 请高手们帮帮忙
有加分~ 展开
{
CFileDialog file(TRUE);
if(!file.DoModal())
return;
//打开要映射的文件
HANDLE hFile = ::CreateFile(file.GetPathName(), GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
MessageBox(file.GetPathName());
if(hFile == INVALID_HANDLE_VALUE)
{
int err = GetLastError();
MessageBox("错误");
}
//创建内存映射对象
HANDLE hMap = ::CreateFileMapping(hFile, NULL, PAGE_READONLY, NULL, NULL, NULL);
if(hMap == INVALID_HANDLE_VALUE)
{
MessageBox("失败1");
return;
}
//映射整个BMP文件到内存,返回这块内存的首地址
LPVOID lpBase = ::MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
//下面是获取BMP文件信息的过程
BITMAPFILEHEADER *pFileHeader;
BITMAPINFO *pInfoHeader;
//取得file-header指针,以获得位图像素
pFileHeader = (BITMAPFILEHEADER*)lpBase;
if(pFileHeader->bfType != MAKEWORD('B', 'M'))
{
MessageBox("本程序仅读取BMP文件");
::UnmapViewOfFile(lpBase);
::CloseHandle(hMap);
::CloseHandle(hFile);
return;
}
BYTE *pBits = (BYTE*)lpBase + pFileHeader->bfOffBits;
//取得info-header指针,以获得文件的大小
pInfoHeader = (BITMAPINFO*)((BYTE)lpBase + sizeof(BITMAPFILEHEADER));
m_nHeight = pInfoHeader->bmiHeader.biHeight;
m_nWidth = pInfoHeader->bmiHeader.biWidth;
//下面是显示BMP文件到内存设备的过程
CClientDC dc(this);
//创建一个与指定DC兼容的未初始化的位图, 选入到内存兼容DC中
HBITMAP hBitmap = ::CreateCompatibleBitmap(dc, m_nWidth, m_nHeight);
::SelectObject(m_hMemDC, hBitmap);
//把图像像据放到建立的设备中
int nRet = ::SetDIBitsToDevice(m_hMemDC,
0,
0,
m_nWidth,
m_nHeight,
0,
0,
0,
m_nHeight,
pBits,
pInfoHeader,
DIB_RGB_COLORS);
::InvalidateRect(m_hWnd, NULL, TRUE);
::DeleteObject(hBitmap);
::UnmapViewOfFile(lpBase);
::CloseHandle(hMap);
}
这是部分代码
调试的时候 运行到m_nHeight = pInfoHeader->bmiHeader.biHeight;
m_nWidth = pInfoHeader->bmiHeader.biWidth;
提示“读取位置 0x00000016 时发生访问冲突” 这是什么原因 请高手们帮帮忙
有加分~ 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询