将图像的像素数据保存到文本文件

    技术2022-05-11  2

    主要的代码如下: void CICETIMDlg::OnBtnSavepixel() {  // TODO: Add your control notification handler code here  if(m_dib.GetHeight()==0)  {   AfxMessageBox("对不起,请先打开位图文件");   return;  }  CString fileName;  char szFilter[]="文本文件(*.txt)|*.txt|All Files(*.*)|*.*||";  CFileDialog dlg(false,"*.txt",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);  if(dlg.DoModal()==IDOK)  {   UpdateData(true);   fileName=dlg.GetPathName();  }  else  {   return;  }  //从CString转换到char*  fileName.Replace(_T(" //"),_T(""));  char* filename=(char*)fileName.GetBuffer(fileName.GetLength());  fileName.ReleaseBuffer();  ofstream outfile;  outfile.open(filename);  int nHeight=m_dib.GetHeight();  int nWidth=m_dib.GetWidth();  int nBitCount=m_dib.GetBitCount();  long lLineBytes=m_dib.GetLineBytes();  outfile<<"*****图片信息*****/r/n";  outfile<<"图像高度:"<<nHeight<<"/r/n";  outfile<<"图像宽度:"<<nWidth<<"/r/n";  outfile<<"图像位数:"<<nBitCount<<"/r/n";  outfile<<"图像每行所占的字节数:"<<lLineBytes<<"/r/n";  outfile<<"****************"<<"/r/n";  BYTE* pdib=m_dib.GetDibData();  for(int i=0;i<nHeight;i++)  {   for(int j=0;j<lLineBytes;j++)   {    BYTE B,G,R;    B=*(pdib+lLineBytes*(nHeight-1-i)+j);    if(nBitCount==8)    {     outfile<<"("<<(nHeight-1-i)<<","<<j<<")="<<(int)B<<"/r/n";    }    else    {     outfile<<"("<<(nHeight-1-i)<<","<<j<<")=B"      <<(int)B<<"/r/n";     j++;     G=*(pdib+lLineBytes*(nHeight-1-i)+j);     outfile<<"("<<(nHeight-1-i)<<","<<j<<")=G"      <<(int)G<<"/r/n";     j++;     R=*(pdib+lLineBytes*(nHeight-1-i)+j);     outfile<<"("<<(nHeight-1-i)<<","<<j<<")=R"      <<(int)R<<"/r/n";    }   }   outfile<<"/r/n";  }  outfile.close(); } 对于24的图片来讲,保存后的文本文件内容类似于: *****图片信息***** 图像高度:162 图像宽度:179 图像位数:24 图像每行所占的字节数:540 **************** (161,0)=B178 (161,1)=G178 (161,2)=R178 (161,3)=B192 (161,4)=G192 (161,5)=R192 (161,6)=B215 (161,7)=G215 (161,8)=R215 (161,9)=B204 (161,10)=G204 (161,11)=R204 (161,12)=B234 (161,13)=G234 (161,14)=R234 (161,15)=B221 (161,16)=G221 (161,17)=R221 (161,18)=B227 (161,19)=G227 (161,20)=R227 (161,21)=B227 (161,22)=G227 (161,23)=R227 (161,24)=B221 对于8位的图片来说,保存后的文本文件内容类似于: *****图片信息***** 图像高度:512 图像宽度:512 图像位数:8 图像每行所占的字节数:512 **************** (511,0)=80 (511,1)=76 (511,2)=88 (511,3)=92 (511,4)=96 (511,5)=100 (511,6)=104 (511,7)=104 (511,8)=100 (511,9)=100 (511,10)=100 (511,11)=104 (511,12)=108 (511,13)=108 (511,14)=112 (511,15)=112 (511,16)=112 (511,17)=112 (511,18)=112 (511,19)=116 (511,20)=116 (511,21)=120 (511,22)=124 (511,23)=124 (511,24)=120 (511,25)=116 (511,26)=116 (511,27)=116 (511,28)=112 (511,29)=112 (511,30)=108 (511,31)=104 (511,32)=100 (511,33)=96 (511,34)=92 (511,35)=92 (511,36)=96 (511,37)=100 (511,38)=108 (511,39)=108 (511,40)=100 (511,41)=88 (511,42)=76 (511,43)=68 (511,44)=68 (511,45)=72 (511,46)=72 (511,47)=72 (511,48)=68 (511,49)=60 (511,50)=44 (511,51)=32 (511,52)=28 (511,53)=28

    最新回复(0)