Figure 3 Initialize and Terminate GDI Library class CMyApp : public CWinApp {
protected:
GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_gdiplusToken;
•••
};
BOOL CMyApp::InitInstance()
{
VERIFY(GdiplusStartup(&m_gdiplusToken,
&m_gdiplusStartupInput, NULL)==Ok);
•••
}
int CMyApp::ExitInstance()
{
GdiplusShutdown(m_gdiplusToken);
return CWinApp::ExitInstance();
}
Figure 4 Displaying an Image
Picture.h ////////////////////////////////////////////////////////////////
// MSDN Magazine March 2002
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles w/ Visual C++ 6.0 for Windows XP and probably Windows 2000 too.
// Set tabsize = 3 in your editor.
//
#pragma once
//////////////////
// Picture object based on GDI+ Image class
//
class CPicture {
public:
CPicture();
~CPicture();
// Load from various places
BOOL Load(LPCTSTR pszPathName);
BOOL Load(UINT nIDRes);
BOOL Load(IStream* pstm);
// render to device context
BOOL Render(CDC* pDC, CRect rc=CRect(0,0,0,0)) const;
CSize GetImageSize(CDC* pDC=NULL) const;
operator BOOL() {
return m_pImage!=NULL;
}
Image* GetImage() {
return m_pImage;
}
void Rotate(RotateFlipType rft);
void Free();
protected:
BOOL m_bUseEmbeddedColorManagement; // set this before loading
Image* m_pImage; // GDI+ Image object
HGLOBAL m_hMem; // used for resource image
};
Picture.cpp ////////////////////////////////////////////////////////////////
// MSDN Magazine March 2002
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles w/ Visual C++ 6.0 for Windows XP and probably Windows 2000 too.
// Set tabsize = 3 in your editor.
//
#include "StdAfx.h"
#include "Picture.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////////////////////////////////////////////
// CPicture implementation
//
CPicture::CPicture()
{
m_bUseEmbeddedColorManagement = FALSE;
m_pImage = NULL;
m_hMem = NULL;
}
CPicture::~CPicture()
{
Free();
}
//////////////////
// Free Image object and global memory (for resource images) if any
//
void CPicture::Free()
{
if (m_pImage) {
delete m_pImage;
m_pImage = NULL;
}
if (m_hMem) {
GlobalFree(m_hMem);
m_hMem = NULL;
}
}
//////////////////
// Load from path name.
//
BOOL CPicture::Load(LPCTSTR pszPathName)
{
Free();
USES_CONVERSION;
m_pImage = Image::FromFile(A2W(pszPathName),
m_bUseEmbeddedColorManagement);
return m_pImage->GetLastStatus()==Ok;
}
//////////////////
// Load from resource. Looks for "IMAGE" type.
//
BOOL CPicture::Load(UINT nIDRes)
{
// find resource in resource file
HINSTANCE hInst = AfxGetResourceHandle();
HRSRC hRsrc = ::FindResource(hInst,
MAKEINTRESOURCE(nIDRes),
"IMAGE"); // type
if (!hRsrc)
return FALSE;
// load resource into memory
DWORD len = SizeofResource(hInst, hRsrc);
BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
if (!lpRsrc)
return FALSE;
// Allocate global memory on which to create stream
HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
memcpy(pmem,lpRsrc,len);
IStream* pstm;
CreateStreamOnHGlobal(m_hMem,FALSE,&pstm);
// load from stream
Load(pstm);
// free/release stuff
GlobalUnlock(m_hMem);
pstm->Release();
FreeResource(lpRsrc);
return m_pImage->GetLastStatus()==Ok;
}
//////////////////
// Load from stream.
//
BOOL CPicture::Load(IStream* pstm)
{
Free();
m_pImage = Image::FromStream(pstm, m_bUseEmbeddedColorManagement);
return m_pImage->GetLastStatus()==Ok;
}
//////////////////
// Rotate image
//
void CPicture::Rotate(RotateFlipType rft)
{
if (m_pImage) {
m_pImage->RotateFlip(rft);
}
}
//////////////////
// Render to device context.
//
BOOL CPicture::Render(CDC* pDC, CRect rc) const
{
ASSERT(pDC);
if (rc.IsRectNull()) {
CSize sz = GetImageSize(pDC);
rc.right = sz.cx;
rc.bottom = sz.cy;
}
Graphics graphics(pDC->m_hDC);
graphics.DrawImage(m_pImage, rc.left, rc.top, rc.Width(), rc.Height());
return TRUE;
}
//////////////////
// Get image size in pixels.
//
CSize CPicture::GetImageSize(CDC* pDC) const
{
CSize sz(0,0);
if (m_pImage) {
sz = CSize(m_pImage->GetWidth(), m_pImage->GetHeight());
}
return sz;
}
Figure 5 RotateFlipType Options // enum types for Image::RotateFlip
//
enum RotateFlipType
{
RotateNoneFlipNone = 0,
Rotate90FlipNone = 1,
Rotate180FlipNone = 2,
Rotate270FlipNone = 3,
RotateNoneFlipX = 4,
Rotate90FlipX = 5,
Rotate180FlipX = 6,
Rotate270FlipX = 7,
RotateNoneFlipY = Rotate180FlipX,
Rotate90FlipY = Rotate270FlipX,
Rotate180FlipY = RotateNoneFlipX,
Rotate270FlipY = Rotate90FlipX,
RotateNoneFlipXY = Rotate180FlipNone,
Rotate90FlipXY = Rotate270FlipNone,
Rotate180FlipXY = RotateNoneFlipNone,
Rotate270FlipXY = Rotate90FlipNone
};
|