程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> mfc- MFC讀取24位bmp圖像出問題了,求大神來解決

mfc- MFC讀取24位bmp圖像出問題了,求大神來解決

編輯:編程綜合問答
MFC讀取24位bmp圖像出問題了,求大神來解決

我要實現的是將24位bmp文件讀入並在用戶區顯示,但是顯示結果都不對的,誰能來幫忙看下?

注:因為對bmp文件還要做後續的處理(比如RGB轉CMYK這樣的),所以我現在寫的代碼只是一個測試是否正確讀取bmp的程序。用其它控件去讀取圖片對我來說是毫無用處的。

這是我的代碼:
void CMy3View::OnLoad()
{
// TODO: 在此添加命令處理程序代碼
CString strFilter,strFilename,info;
unsigned short format,bit_per_pix,r,g,b;
unsigned int offset,bmp_width;
int bmp_height;
//打開bmp文件
strFilter=_T("bmp images(*.bmp)|*.bmp||");
CFileDialog dlg(true,NULL,NULL,OFN_EXPLORER|OFN_ENABLESIZING|OFN_FILEMUSTEXIST,strFilter);
if(dlg.DoModal()==IDOK){
strFilename=dlg.GetPathName();
CFile m_bmp(strFilename,CFile::modeRead);
m_bmp.SeekToBegin();
//讀取bmp文件頭
m_bmp.Read(&format,sizeof(unsigned short));
if(0x4d42!=format){//查看圖片文件格式
AfxMessageBox(_T("The format of this bmp image is not supported by Windows!"));
}
m_bmp.Seek(0xa,CFile::begin); //讀取到位圖數據需要的偏移量
m_bmp.Read(&offset,sizeof(unsigned int));
m_bmp.Seek(0x12,CFile::begin); //讀取位圖寬高
m_bmp.Read(&bmp_width,sizeof(unsigned int));
m_bmp.Read(&bmp_height,sizeof(int));
m_bmp.Seek(0x1c,CFile::begin); //讀取位圖格式(256色,24位,32位)
m_bmp.Read(&bit_per_pix,sizeof(unsigned short));
m_bmp.Seek(offset,CFile::begin);//定位到位圖數據
CClientDC dc(this);
int x,y;
if(24==bit_per_pix){ //如果是24位位圖,則執行下列代碼
if(bmp_height>0)
for(y=0;y<bmp_height;y++){
for(x=0;x<(int)bmp_width;x++){
m_bmp.Read(&b,sizeof(unsigned short));
m_bmp.Read(&g,sizeof(unsigned short));
m_bmp.Read(&r,sizeof(unsigned short));
dc.SetPixel(x,(bmp_height-y-1),RGB(r,g,b));
}
}
}

m_bmp.Close();
}
}
這是原始圖片:
圖片說明
這是我的程序讀取的結果:
圖片說明

最佳回答:


m_bmp.Read(&b,sizeof(unsigned short)); 讀取時為什麼 是 short?這樣一次就讀取了 6 個字節的數據。但實現一個像素的字節數應該是 3 才對。

所以導致:
(1) 圖像數據不對
(2) 圖像數據只有一半

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved