现在的位置: 首页 > 综合 > 正文

多文档结构中,CImage类读入图像,与保存图像方法

2013年10月11日 ⁄ 综合 ⁄ 共 2994字 ⁄ 字号 评论关闭

代码中加入了很多文档结构元素,主要是为了保存该方法,不保证人人都适用。

 

//读入外部图像:

 

void CCMDITestApp::OnFileOpen()
{
 // TODO: 在此添加命令处理程序代码
 CString ImageStyle=_T("Bitmap(*.bmp)|*.bmp|Txt(*.txt)|*.txt|All Files(*.*)|*.*||");
 CFileDialog dlg(TRUE,NULL,NULL,NULL,ImageStyle,NULL);
 dlg.m_ofn.Flags |=OFN_ALLOWMULTISELECT|OFN_EXPLORER;//同时打开多幅图像

 //08/15/2002 Show new style file dialog in NT/2000/XP
 if (GetVersion() < 0x80000000)  //NT/2000/XP
  (dlg.m_ofn).lStructSize =88;
 else //95/98/ME. This is default.
  (dlg.m_ofn).lStructSize =76;

 CString sBuffer;
 dlg.GetOFN().lpstrFile = sBuffer.GetBuffer(10000); // Strictly, the buffer size should be something like this: (c_cMaxFiles * (MAX_PATH + 1)) + 1
 dlg.GetOFN().nMaxFile = 10000;

 POSITION pos;

 if(dlg.DoModal()==IDOK)
 { 
  pos=dlg.GetStartPosition();

  CString str[100];
  CString FileExt[100];

  int n=-1,i;
  while(pos!=NULL)
  {
   n++;
   str[n]=dlg.GetNextPathName(pos);
   //FileExt[n] = dlg.GetFileExt();
   //由文件路径转换为文件扩展名
   //FileExt[n]=str[n].Right(str[n].GetLength()-str[n].ReverseFind('.')-1);
  }

  for(i=0;i<=n;i++)
  {
   for(int j=i;j<=n;j++)
   {
    if( (str[i].GetLength()==str[j].GetLength() && strcmp((char *)(LPCTSTR)str[i],(char *)(LPCTSTR)str[j])<0) || str[i].GetLength()<str[j].GetLength() )
    {
     str[n+1]=str[j];
     str[j]=str[i];
     str[i]=str[n+1];
    }
   }
   //由文件路径转换为文件扩展名
   FileExt[i]=str[i].Right(str[i].GetLength()-str[i].ReverseFind('.')-1);
  }

  CMultiDocTemplate* pDoc;
  for(i=0;i<=n;i++)
  {
   if ((FileExt[i]==_T("bmp"))||(FileExt[i]==_T("BMP")))
   {
    pDoc=pImageTemplate;
    CheckOpenDocumentFile(str[i],pDoc);
   }
   else if ((FileExt[i]==_T("txt"))||(FileExt[i]==_T("TXT")))
   {
    pDoc=pEditTemplate;
    CheckOpenDocumentFile(str[i],pDoc);
   }
   else
   {
    AfxMessageBox(_T("This File is not supported!"));
    return;
   }
  }
  //Order-open files END

  sBuffer.ReleaseBuffer();
 }
 else
 {
  sBuffer.ReleaseBuffer();
  return;
 }
}

 

//保存图像:

 

void CCMDITestView::OnFileSaveAs()
{
 // TODO: 在此添加命令处理程序代码
 CCMDITestDoc* pDoc = GetDocument();

 CFileDialog dlg(FALSE,_T("bmp"),pDoc->m_strFileName,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
  _T("Bmp File(*.bmp)|*.bmp||"),NULL,NULL);
 if (dlg.DoModal()==IDOK)
 {
  HRESULT hResult=m_MyImage.Save(dlg.GetPathName(),Gdiplus::ImageFormatBMP);
  if (FAILED(hResult))
  {
   //------------------------------------------写入失败,判断图像是否是只读格式-------------------------------//
   CFileStatus  Status;      
   if(CFile::GetStatus(pDoc->m_strFilePath,Status))
   {
    if((Status.m_attribute&CFile::readOnly)==FILE_ATTRIBUTE_READONLY)
     MessageBox(_T("文件为只读格式,无法保存!"),_T("信息提示"),MB_OK);
    else
     AfxMessageBox(_T("保存失败"));
   }
  }
 }
}

void CCMDITestView::OnFileSave()
{
 // TODO: 在此添加命令处理程序代码
 CCMDITestDoc* pDoc = GetDocument();
 HRESULT hResult=m_MyImage.Save(pDoc->m_strFilePath);
 if (FAILED(hResult))
 {
  //------------------------------------------写入失败,判断图像是否是只读格式-------------------------------//
  CFileStatus  Status;      
  if(CFile::GetStatus(pDoc->m_strFilePath,Status))
  {
   if((Status.m_attribute&CFile::readOnly)==FILE_ATTRIBUTE_READONLY)
    MessageBox(_T("文件为只读格式,无法保存!"),_T("信息提示"),MB_OK);
   else
    AfxMessageBox(_T("保存失败"));
  }
 }
}

抱歉!评论已关闭.