使用vc++6.0编写的程序。保存view为bmp图像文件。保存成功,但是使用windows图片和传真查看器,显示绘图失 20
使用vc++6.0编写的程序。保存view为bmp图像文件。保存成功,但是使用windows图片和传真查看器,显示绘图失败。是什么问题呢?代码是:BITMAPINFObi...
使用vc++6.0编写的程序。保存view为bmp图像文件。保存成功,但是使用windows图片和传真查看器,显示绘图失败。是什么问题呢?
代码是:
BITMAPINFO bi;//信息头
void *pBits=NULL;
CRect rect;//客户区窗口
GetClientRect(&rect);
int nWidth = rect.Width();
int nHeight= rect.Height();
//填充信息头
ZeroMemory(&bi,sizeof(bi));
bi.bmiHeader.biSize=sizeof(bi.bmiHeader);
bi.bmiHeader.biHeight=nHeight;
bi.bmiHeader.biWidth=nWidth;
bi.bmiHeader.biPlanes=1;
bi.bmiHeader.biBitCount=24;
bi.bmiHeader.biCompression=BI_RGB;
bi.bmiHeader.biSizeImage=3*nWidth*nHeight;
//拷贝客户区至内存DC
HDC hActiveDC=::GetDC(this->m_hWnd);
HDC hActiveWndCompatibleDC=CreateCompatibleDC(hActiveDC);
HBITMAP hActiveWndCompactibleBitmap=CreateCompatibleBitmap(hActiveDC,nWidth,nHeight);
SelectObject(hActiveWndCompatibleDC,hActiveWndCompactibleBitmap);
BitBlt(hActiveWndCompatibleDC,0,0,nWidth,nHeight,hActiveDC,0,0,SRCCOPY);
//保存内存DC
HDC hBmpFileDC=CreateCompatibleDC(hActiveWndCompatibleDC);
HBITMAP hBmpFileBitmap=CreateDIBSection(hActiveWndCompatibleDC,&bi,DIB_RGB_COLORS,&pBits,NULL,0);
SelectObject(hBmpFileDC,hBmpFileBitmap);
BitBlt(hBmpFileDC,0,0,nWidth,nHeight,hActiveWndCompatibleDC,0,0,SRCCOPY);
CString tempFileName;//目标文件名
CFileDialog dlg(FALSE, _T("bmp"), _T("Temp.bmp"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Bitmap Files (*.bmp) |*.bmp |All Files (*.*) |*.* | |"), this);
if(dlg.DoModal() == IDOK)
{
tempFileName=dlg.GetPathName();
}
HANDLE hFile=CreateFile(tempFileName,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile!=INVALID_HANDLE_VALUE)
{
DWORD dwRet=0;
//填充文件头
BITMAPFILEHEADER bmfHeader;
ZeroMemory(&bmfHeader,sizeof(bmfHeader));
bmfHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize=bi.bmiHeader.biSizeImage+bmfHeader.bfOffBits;
bmfHeader.bfType= 'MB';
WriteFile(hFile,&bmfHeader,sizeof(bmfHeader),&dwRet,NULL);
WriteFile(hFile,&bi.bmiHeader,sizeof(bi.bmiHeader),&dwRet,NULL);
WriteFile(hFile,pBits,bi.bmiHeader.biSizeImage,&dwRet,NULL);
CloseHandle(hFile);
}
使用windows自带的绘图,可以成功打开。 展开
代码是:
BITMAPINFO bi;//信息头
void *pBits=NULL;
CRect rect;//客户区窗口
GetClientRect(&rect);
int nWidth = rect.Width();
int nHeight= rect.Height();
//填充信息头
ZeroMemory(&bi,sizeof(bi));
bi.bmiHeader.biSize=sizeof(bi.bmiHeader);
bi.bmiHeader.biHeight=nHeight;
bi.bmiHeader.biWidth=nWidth;
bi.bmiHeader.biPlanes=1;
bi.bmiHeader.biBitCount=24;
bi.bmiHeader.biCompression=BI_RGB;
bi.bmiHeader.biSizeImage=3*nWidth*nHeight;
//拷贝客户区至内存DC
HDC hActiveDC=::GetDC(this->m_hWnd);
HDC hActiveWndCompatibleDC=CreateCompatibleDC(hActiveDC);
HBITMAP hActiveWndCompactibleBitmap=CreateCompatibleBitmap(hActiveDC,nWidth,nHeight);
SelectObject(hActiveWndCompatibleDC,hActiveWndCompactibleBitmap);
BitBlt(hActiveWndCompatibleDC,0,0,nWidth,nHeight,hActiveDC,0,0,SRCCOPY);
//保存内存DC
HDC hBmpFileDC=CreateCompatibleDC(hActiveWndCompatibleDC);
HBITMAP hBmpFileBitmap=CreateDIBSection(hActiveWndCompatibleDC,&bi,DIB_RGB_COLORS,&pBits,NULL,0);
SelectObject(hBmpFileDC,hBmpFileBitmap);
BitBlt(hBmpFileDC,0,0,nWidth,nHeight,hActiveWndCompatibleDC,0,0,SRCCOPY);
CString tempFileName;//目标文件名
CFileDialog dlg(FALSE, _T("bmp"), _T("Temp.bmp"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Bitmap Files (*.bmp) |*.bmp |All Files (*.*) |*.* | |"), this);
if(dlg.DoModal() == IDOK)
{
tempFileName=dlg.GetPathName();
}
HANDLE hFile=CreateFile(tempFileName,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile!=INVALID_HANDLE_VALUE)
{
DWORD dwRet=0;
//填充文件头
BITMAPFILEHEADER bmfHeader;
ZeroMemory(&bmfHeader,sizeof(bmfHeader));
bmfHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize=bi.bmiHeader.biSizeImage+bmfHeader.bfOffBits;
bmfHeader.bfType= 'MB';
WriteFile(hFile,&bmfHeader,sizeof(bmfHeader),&dwRet,NULL);
WriteFile(hFile,&bi.bmiHeader,sizeof(bi.bmiHeader),&dwRet,NULL);
WriteFile(hFile,pBits,bi.bmiHeader.biSizeImage,&dwRet,NULL);
CloseHandle(hFile);
}
使用windows自带的绘图,可以成功打开。 展开
1个回答
展开全部
保存的时候:
DWORD dwSize;
int nTable = 0;//灰度图时nTable=256,24位图时nTable=0
dwSize = sizeof(BITMAPFILEHEADER);
::WriteFile(hFile, &bmfHeader, dwSize, &dwRet, NULL );
dwSize = sizeof(BITMAPINFOHEADER) + nTable * sizeof(RGBQUAD);
::WriteFile(hFile, &bi, dwSize, &dwRet, NULL );
dwSize = dwImageSize;
WriteFile(hFile, pBits, dwSize, &dwRet, NULL );
DWORD dwSize;
int nTable = 0;//灰度图时nTable=256,24位图时nTable=0
dwSize = sizeof(BITMAPFILEHEADER);
::WriteFile(hFile, &bmfHeader, dwSize, &dwRet, NULL );
dwSize = sizeof(BITMAPINFOHEADER) + nTable * sizeof(RGBQUAD);
::WriteFile(hFile, &bi, dwSize, &dwRet, NULL );
dwSize = dwImageSize;
WriteFile(hFile, pBits, dwSize, &dwRet, NULL );
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询