04-29 02:46
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[C/C++] MFC / CImage 클래스를 이용한 다양한 포맷의 이미지 출력 본문

Programming/C&C++

[C/C++] MFC / CImage 클래스를 이용한 다양한 포맷의 이미지 출력

cinema4dr12 2014. 6. 15. 08:57


  Project Name

  ImgOut

  Application Type

  Single Document

  Document/View Architecture Support

  Yes

  Resource

  한국어

  Use Unicode Libraries

  Yes

  Project Style

  MFC Standard

  Visual Style and Colors

  Windows Native/Default

  Use of MFC  Use MFC in a shared DLL

 

[<atlimage.h> 헤더 추가]

"ImgOutView.cpp" 소스에 다음과 같이 <atlimage.h> 헤더를 추가한다:


// ImgOutView.cpp : implementation of the CImgOutView class
//

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "ImgOut.h"
#endif

#include "ImgOutDoc.h"
#include "ImgOutView.h"
#include <atlimage.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


"ImgOutDoc.h" 헤더에 다음과 같이 <atlimage.h> 헤더를 추가한다:


// ImgOutDoc.h : interface of the CImgOutDoc class
//


#pragma once
#include <atlimage.h>



[CImgOutDoc 클래스에 멤버 추가]

"ImgOutDoc.h"


public:
	CImage m_Img;



[ImgOutDoc 클래스에 OnOpenDocument() 함수 재정의]

Class View > CImgOutDoc > Property > Overrides > OnOpenDocument > <Add>OnOpenDocument

BOOL CImgOutDoc::OnOpenDocument(LPCTSTR lpszPathName) { if (!CDocument::OnOpenDocument(lpszPathName)) return FALSE; // 이미지 파일 로드 HRESULT hResult = m_Img.Load(lpszPathName); // 이미지 파일 로드 실행 시 에러 메시지 출력 if(FAILED(hResult)) { CString strTmp = _T("ERROR: Failed to load "); strTmp += lpszPathName; AfxMessageBox(strTmp); } return TRUE; }


[CImgOutView 클래스에 WM_PAINT 메시지 핸들러 함수 등록 및 작성]

Class View > CImgOutView > Property > WM_PAINT > <Add>OnPaint


void CImgOutView::OnPaint()
{
	CPaintDC dc(this); // device context for painting

	CImgOutDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// 로드된 이미지
	CImage img = pDoc->m_Img;

	// 화면 DC에 출력
	if(img != NULL)
		img.BitBlt(dc.m_hDC, 0, 0);
	else
		return;
}


[빌드 및 실행]

Ctrl + Shift + B

Ctrl + F5

Comments