Labels:

The Following is an example program is to load and show an Image using CImage.

First Create a new MFC Application and name it as MyCImage. Then Follow the below steps,

1. Add the the following header files in the CMyCImage.h ,

#include "atlimage.h"
#include "atltypes.h"

2. Create the CImage object by the following code in CMyCImage.h ,

CImage img;

3.Create the FileOpen function and make that like below,

void CMyCImageView::OnFileOpen()
{
CFileDialog FileDialog(TRUE,NULL,NULL,OFN_HIDEREADONLY,"All Files(*.*)|*.*|");
if(FileDialog.DoModal() == IDOK)
{
CString PathName = FileDialog.GetPathName();
img.Load(PathName);
}
Invalidate();
}

4.Edit the OnDraw function and make it like below,

void CMyCImageView::OnDraw(CDC* pDC)
{
CMyCImageDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;

CRect m_ShowDRect;
m_ShowDRect=CRect(0,0,img.GetWidth(),img.GetHeight());
SetScrollSizes(MM_TEXT,CSize(img.GetWidth(),img.GetHeight()));
img.Draw(pDC->m_hDC,CRect(&m_ShowDRect));
}

5.Change the executable mode into Release mode and also change the Character set into Multi-Byte Character Set in the project settings.

6.Now Compile and Run.





Comments (0)

Post a Comment