结合CDIB类,对图像的打开、显示、保存

  需要先建一个显示类,这个显示类的基类我选的是CStatic,以下代码均放在这个显示类中,另外需在这个类的.h文件中添加

CDib m_CDib;

  另外,CDIB类文件需要自己添加。

1、图像的打开:

 1 void CStaticImage::ImageOpen()
 2 {
 3     CString pathName;          //文件路径
 4     CString fileName;             //文件名
 5  
 6     //定义一个文件对话框,并设置属性
 7     CFileDialog  Dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_FILEMUSTEXIST,
 8         NULL, NULL);
 9     if(Dlg.DoModal() == IDOK)
10     {
11         pathName = Dlg.GetPathName();
12         
13         fileName = Dlg.GetFileTitle();
14         
15         SetWindowText(fileName);
16     }
17  
18     CFile file;
19     CFileException e;
20  
21     if(file.Open(pathName, CFile::modeRead,&e))
22     {
23         m_Dib.Read(&file);
24         file.Close();
25         RedrawWindow();
26     }
27 }

 

2、图像的显示:

 1 void CStaticImage::OnPaint() 
 2 {
 3     CPaintDC dc(this); // device context for painting
 4     
 5     // TODO: Add your message handler code here
 6 
 7     CRect rect;
 8     GetClientRect(&rect);            //获取客户区大小
 9     CPoint point;
10     CSize size;
11 
12     size  = rect.Size();
13     point = rect.TopLeft();
14 
15     m_Dib.Draw(&dc, point, size);
16 }

 

3、图像的保存:

void CStaticImage::ImageSave()
{

    CString pathName;
    CString fileName;
    CString strFilter = _T(".bmp");  
    
    CFileDialog  Dlg(FALSE, "*.bmp", _T("path.bmp"),  OFN_OVERWRITEPROMPT |OFN_CREATEPROMPT , strFilter); 
    if(Dlg.DoModal() == IDOK)
    {
        CFile file;
        CFileException e;
        pathName = Dlg.GetPathName();
        if(file.Open(pathName, CFile::modeCreate | CFile::modeReadWrite | CFile::shareExclusive 
            | CFile::shareDenyWrite | CFile::modeNoTruncate, &e))
        {
            m_Dib.Write(&file);
            file.Close();
        }
    }
}

 

posted @ 2018-08-12 16:10  星宸垠海  阅读(172)  评论(0)    收藏  举报