Click here to Skip to main content
15,883,873 members
Articles / Desktop Programming / MFC

An Image Decoder Based on IImgCtx

Rate me:
Please Sign up or sign in to vote.
4.70/5 (23 votes)
25 May 20045 min read 176.1K   3.4K   57  
This article shows how to decode image with IImgCtx interface provided by IE
// ImgViewerView.cpp : implementation of the CImgViewerView class
//

#include "stdafx.h"
#include "ImgViewer.h"

#include "ImgViewerDoc.h"
#include "ImgViewerView.h"
#include "WndImgCtx.h"

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

/////////////////////////////////////////////////////////////////////////////
// CImgViewerView

IMPLEMENT_DYNCREATE(CImgViewerView, CScrollView)

BEGIN_MESSAGE_MAP(CImgViewerView, CScrollView)
	//{{AFX_MSG_MAP(CImgViewerView)
	ON_WM_ERASEBKGND()
	//}}AFX_MSG_MAP
	// Standard printing commands
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImgViewerView construction/destruction

CImgViewerView::CImgViewerView()
{
	// TODO: add construction code here
	m_hBitmap = NULL;
}

CImgViewerView::~CImgViewerView()
{
	CleanUp();
}

BOOL CImgViewerView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CImgViewerView drawing

void CImgViewerView::OnDraw(CDC* pDC)
{
	CImgViewerDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

	if (m_hBitmap)
	{
		HDC hMemDC = CreateCompatibleDC(pDC->GetSafeHdc()); 
		if (hMemDC)
		{
			// select new bitmap into memory DC 
			HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, m_hBitmap);

			// show it
			CSize sizeTotal;
			::GetBitmapDimensionEx(m_hBitmap, &sizeTotal);
			::BitBlt(pDC->GetSafeHdc(), 0, 0, sizeTotal.cx, sizeTotal.cy,
				hMemDC, 0, 0, SRCCOPY);

			// select old bitmap back into memory DC and get handle to bitmap
			SelectObject(hMemDC, hOldBitmap); 

			DeleteDC(hMemDC); 
		}
	}
}

void CImgViewerView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	sizeTotal.cx = sizeTotal.cy = 0;
	
	CleanUp();

	CImgViewerDoc* pDoc = GetDocument();
	CString strPath = pDoc->GetPathName();
	if (!strPath.IsEmpty())
		m_hBitmap = GetBitmapFromFile(strPath);

	if (m_hBitmap)
		GetBitmapDimensionEx(m_hBitmap, &sizeTotal);
	
	SetScrollSizes(MM_TEXT, sizeTotal);

	Invalidate();
	UpdateWindow();
}

/////////////////////////////////////////////////////////////////////////////
// CImgViewerView diagnostics

#ifdef _DEBUG
void CImgViewerView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CImgViewerView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CImgViewerDoc* CImgViewerView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CImgViewerDoc)));
	return (CImgViewerDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CImgViewerView message handlers

void CImgViewerView::CleanUp()
{
	if (m_hBitmap)
	{
		DeleteObject(m_hBitmap);
		m_hBitmap = NULL;
	}
}

BOOL CImgViewerView::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	CBrush br;
	br.CreateSolidBrush(RGB(0, 0, 0));
	FillOutsideRect(pDC, &br);
	br.DeleteObject();

	return TRUE;
}

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


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions