Click here to Skip to main content
12,076,925 members (47,243 online)
Click here to Skip to main content

Stats

241.3K views
6.3K downloads
53 bookmarked
Posted

An Image (GIF, JPEG, BMP, ICO, WMF and EMF) Viewer

, 25 Oct 2002
A sample that can load, display, and print graphics files.
imgviewer.zip
Release
ImgViewer.exe
res
ImgViewer.ico
ImgViewerDoc.ico
Toolbar.bmp
ImgViewer.clw
ImgViewer.dsp
ImgViewer.dsw
// ImgViewerDoc.cpp : implementation of the CImgViewerDoc class
//

#include "stdafx.h"
#include <afxpriv.h>
#include "ImgViewer.h"
#include "ImgViewerDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CImgViewerDoc

IMPLEMENT_DYNCREATE(CImgViewerDoc, CDocument)

BEGIN_MESSAGE_MAP(CImgViewerDoc, CDocument)
	//{{AFX_MSG_MAP(CImgViewerDoc)
	ON_COMMAND(ID_FILE_CLOSE, OnFileClose)
	ON_COMMAND(ID_FILE_PROPERTIES, OnFileProperties)
	//}}AFX_MSG_MAP
	ON_COMMAND(ID_FILE_SEND_MAIL, OnFileSendMail)
	ON_UPDATE_COMMAND_UI(ID_FILE_SEND_MAIL, OnUpdateFileSendMail)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImgViewerDoc construction/destruction

CImgViewerDoc::CImgViewerDoc()
{
	m_pPicture = NULL;
}

CImgViewerDoc::~CImgViewerDoc()
{
	if (m_pPicture != NULL)
	{
		m_pPicture->Release();
		m_pPicture = NULL;

		TRACE(_T("CImgViewerDoc::~CImgViewerDoc - m_pPicture->Release()\n"));
	}
}

/////////////////////////////////////////////////////////////////////////////
// CImgViewerDoc diagnostics

#ifdef _DEBUG
void CImgViewerDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CImgViewerDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);

	dc << _T("m_pPicture = ") << m_pPicture << _T("\n");
	dc << _T("m_sizeInHiMetric = ") << m_sizeInHiMetric << _T("\n");
	dc << _T("m_sizeInPix = ") << m_sizeInPix << _T("\n");
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CImgViewerDoc commands

BOOL CImgViewerDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	ASSERT(::AfxIsValidString(lpszPathName));

	IPicture *pPic = NULL;

	USES_CONVERSION;
	HRESULT hr = ::OleLoadPicturePath(const_cast<LPOLESTR>(T2COLE(lpszPathName)),
									  NULL,
									  0,
									  0,
									  IID_IPicture,
									  reinterpret_cast<LPVOID *>(&pPic));
	
	TRACE(_T("CImgViewerDoc::OnOpenDocument - OleLoadPicturePath(\"%s\"): ")
		  _T("hr = 0x%08X, m_pPicture = 0x%08X\n"), lpszPathName, hr, pPic);
	
	if (SUCCEEDED(hr) && pPic != NULL)
	{
		this->DeleteContents();

		ASSERT(m_pPicture == NULL);

		m_pPicture = pPic;

		// get width and height of picture
		VERIFY(SUCCEEDED(m_pPicture->get_Width(&m_sizeInHiMetric.cx)));
		VERIFY(SUCCEEDED(m_pPicture->get_Height(&m_sizeInHiMetric.cy)));

		const int HIMETRIC_PER_INCH = 2540;

		const HDC hDCScreen = ::GetDC(NULL);
		ASSERT(hDCScreen != NULL);
		// Pixels per logical inch along width
		const int nPixelsPerInchX = ::GetDeviceCaps(hDCScreen, LOGPIXELSX);
		// Pixels per logical inch along height
		const int nPixelsPerInchY = ::GetDeviceCaps(hDCScreen, LOGPIXELSY);
		VERIFY(::ReleaseDC(NULL, hDCScreen) != 0);

		// convert himetric to pixels
		m_sizeInPix.cx = (nPixelsPerInchX * m_sizeInHiMetric.cx +
						  HIMETRIC_PER_INCH / 2) / HIMETRIC_PER_INCH;
		m_sizeInPix.cy = (nPixelsPerInchY * m_sizeInHiMetric.cy +
						  HIMETRIC_PER_INCH / 2) / HIMETRIC_PER_INCH;

		return TRUE;
	}
	else
	{
		// OleLoadPicturePath failed.
		CString strErrMsg;
		strErrMsg.Format(IDS_ERRLOADPIC, lpszPathName);
		::AfxMessageBox(strErrMsg, MB_OK | MB_ICONEXCLAMATION);

		this->OnFileClose();

		return FALSE;
	}
}

void CImgViewerDoc::DeleteContents() 
{
	if (m_pPicture != NULL)
	{
		m_pPicture->Release();
		m_pPicture = NULL;

		TRACE(_T("CImgViewerDoc::DeleteContents - m_pPicture->Release()\n"));
	}
	
	CDocument::DeleteContents();
}

void CImgViewerDoc::OnFileClose()
{
	this->DeleteContents();

	// Back to initial state
	this->OnNewDocument();
	ASSERT_VALID(this->GetDocTemplate());
	this->GetDocTemplate()->SetDefaultTitle(this);
	this->UpdateAllViews(NULL);

	ASSERT(m_pPicture == NULL);
	ASSERT(this->GetPathName().IsEmpty());
}

void CImgViewerDoc::OnFileProperties() 
{
	if (m_pPicture != NULL)
	{
		// Picture type
		short shType = PICTYPE_UNINITIALIZED;
		VERIFY(SUCCEEDED(m_pPicture->get_Type(&shType)));

		CString strType;
		VERIFY(strType.LoadString((shType >= PICTYPE_UNINITIALIZED &&
								   shType <= PICTYPE_ENHMETAFILE)
								  ? (IDS_PT_UNINITIALIZED + shType + 1)
								  : IDS_PT_UNKNOWN));

		// Picture attributes
		DWORD dwAttr = 0;
		VERIFY(SUCCEEDED(m_pPicture->get_Attributes(&dwAttr)));

		CString strAttr;

		if (dwAttr & PICTURE_SCALABLE)
		{
			strAttr.LoadString(IDS_PA_SCALABLE);
		}

		if (dwAttr & PICTURE_TRANSPARENT)
		{
			CString strTemp;
			VERIFY(strTemp.LoadString(IDS_PA_TRANSPARENT));
			if (!strAttr.IsEmpty())
			{
				strAttr += _T(" | ");
			}
			strAttr += strTemp;
		}

		if (strAttr.IsEmpty())
		{
			VERIFY(strAttr.LoadString(IDS_PA_NONE));
		}

		// Properties
		CString strProp;
		strProp.Format(IDS_PROP,
					   this->GetPathName(),
					   m_sizeInPix.cx, m_sizeInPix.cy,
					   shType,
					   strType,
					   dwAttr,
					   strAttr);

		::AfxMessageBox(strProp, MB_OK | MB_ICONINFORMATION);
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Chen Su
Software Developer
Canada Canada
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.160212.1 | Last Updated 26 Oct 2002
Article Copyright 2002 by Chen Su
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid