应用CImage类用文件和流的方式显示图片和放大缩小图片

1.是MFC工程
2.创建默认工程:单文档工程
3.工程名:JPEGTest
4.1在CJPEGTestView类中添加
HANDLE    m_hFile;
CImage m_Image;
void LoadMemImage( void *  pMemData,  long  len);
afx_msg void OnFileOpen();
4.2在stdafx.h中添加#include
4.3在CJPEGTestView.cpp中添加
BEGIN_MESSAGE_MAP(CJPEGTestView, CView)
ON_COMMAND(ID_FILE_OPEN,            &CJPEGTestView::OnFileOpen)//这个是添加的
END_MESSAGE_MAP()
4.4
void CJPEGTestView::OnFileOpen()
{
CString strFilter;
CSimpleArray aguidFileTypes;
HRESULT hResult;

// 获取CImage支持的图像文件的过滤字符串
hResult = m_Image.GetExporterFilterString(strFilter,aguidFileTypes,
_T( "All Image Files") );
if ( FAILED(hResult) )
{
MessageBox(_T("GetExporterFilter调用失败!"));
return;
}
CFileDialog dlg( TRUE, NULL, NULL, OFN_FILEMUSTEXIST, strFilter );
if ( IDOK != dlg.DoModal() )
return;

m_Image.Destroy();

{
//在此处是用流的方式显示JPEG
m_hFile = ::CreateFile(dlg.GetFileName(),GENERIC_READ,0,NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_READONLY,NULL); // 用这个函数比OpenFile好
if ( m_hFile == INVALID_HANDLE_VALUE)
{
MessageBox(_T("打开文件失败"));
CloseHandle( m_hFile ); // 一定注意在函数退出之前对句柄进行释放。
return;
}
DWORD filesize=GetFileSize( m_hFile,NULL );

char* buffer=new char[filesize+1]; // 最后一位为 '/0',C-Style 字符串的结束符。

DWORD readsize;
ReadFile( m_hFile,buffer,filesize,&readsize,NULL);

buffer[filesize]=0;

LoadMemImage(buffer,readsize);

delete[] buffer; // 注意是delete[] 而不是 delete
CloseHandle(m_hFile); // 关闭句柄。
}

// 设置主窗口标题栏内容
CString str;
str.LoadString(AFX_IDS_APP_TITLE);

AfxGetMainWnd()->SetWindowText(str + _T("_") + dlg.GetFileName());

Invalidate(); // 强制调用OnDraw
}
void CJPEGTestView::LoadMemImage( void *  pMemData,  long  len)
{
HGLOBAL hGlobal  =  GlobalAlloc(GMEM_MOVEABLE, len);

void *  pData  =  GlobalLock(hGlobal);

memcpy(pData, pMemData, len);

GlobalUnlock(hGlobal);

IStream *  pStream  =  NULL;
if  (CreateStreamOnHGlobal(hGlobal, TRUE,  & pStream)  ==  S_OK)
{
if ( FAILED( m_Image.Load(pStream)) )
{
MessageBox(_T("调用图像文件失败!"));
return;
}
pStream -> Release();
}
GlobalFree(hGlobal);
}
void CJPEGTestView::OnDraw(CDC* pDC)
{
CJPEGTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!m_Image.IsNull())
{
m_Image.Draw(pDC->m_hDC,0,0);
}
}
5.还可以添加放大缩小功能
在:CJPEGTestDoc类中添加:
public:
inline float GetZoomFactor() { return m_ZoomFactor; }

protected:
float m_ZoomFactor;//在构造函数中初始化1.00
BEGIN_MESSAGE_MAP(CJPEGTestDoc, CDocument)
ON_COMMAND(AFX_ID_PREVIEW_ZOOMOUT, OnViewZoomout)
ON_COMMAND(AFX_ID_PREVIEW_ZOOMIN, OnViewZoomin)
END_MESSAGE_MAP()
void CJPEGTestDoc::OnViewZoomout()
{
if (m_ZoomFactor<=0.0625) return;

if        (m_ZoomFactor == 2.00f) m_ZoomFactor = 1.50f;
else if (m_ZoomFactor == 1.50f) m_ZoomFactor = 1.00f;
else if (m_ZoomFactor == 1.00f)    m_ZoomFactor = 0.75f;
else if (m_ZoomFactor == 0.75f)    m_ZoomFactor = 0.50f;
else                            m_ZoomFactor/=2;

CStatusBar& statusBar = ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->GetStatusBar();
CString s;
s.Format(_T("%4.1f %%"),m_ZoomFactor*100);
statusBar.SetPaneText(2, s);

UpdateAllViews(NULL,WM_USER_NEWIMAGE);
}
void CJPEGTestDoc::OnViewZoomin()
{
if (m_ZoomFactor>=16) return;

if        (m_ZoomFactor == 0.50f) m_ZoomFactor = 0.75f;
else if (m_ZoomFactor == 0.75f) m_ZoomFactor = 1.00f;
else if (m_ZoomFactor == 1.00f)    m_ZoomFactor = 1.50f;
else if (m_ZoomFactor == 1.50f)    m_ZoomFactor = 2.00f;
else                            m_ZoomFactor*=2;

CStatusBar& statusBar = ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->GetStatusBar();
CString s;
s.Format(_T("%4.0f %%"),m_ZoomFactor*100);
statusBar.SetPaneText(2, s);

UpdateAllViews(NULL,WM_USER_NEWIMAGE);
}
6.显示的时候,做相应的更改
void CJPEGTestView::OnDraw(CDC* pDC)
{
CJPEGTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!m_Image.IsNull())
{
float zoom=pDoc->GetZoomFactor();
if (zoom==1)
m_Image.Draw(pDC->m_hDC,0,0);
else
{
pDC->SetStretchBltMode( HALFTONE ); //防止缩小图片失真
m_Image.Draw(pDC->GetSafeHdc(),CRect(0,0,(int)(m_Image.GetWidth()*zoom),(int)(m_Image.GetHeight()*zoom)));
}   
}
}
差不多了。就这些,也是从网上看了相关的资料后,自己动手实验一下。

 

转载自:http://blog.sina.com.cn/s/blog_5f432e6a0100u993.html

你可能感兴趣的:(MFC)