/*-----------------------------------------
* Copyright (c) 2010 Clark
* 文件名称: COM_IMAGE.h
* 摘要: 处理图片
* 作者: 陈泽丹
* 版本: 1.0
----------------------------------------*/
#pragma once
#include <Imaging.h>
#include <vector>
class COM_IMAGE
{
public:
static bool G_Init();
static void G_UnInit();
COM_IMAGE();
virtual ~COM_IMAGE(void);
void loadFile(LPCWSTR lpResourceName);
void drawImage(CDC* io_pDC, RECT i_rectDest, RECT i_rectSrc);
int Width();
int Hight();
void Release();
IImage* getIImage();
private:
static IImagingFactory* g_pImageFactory;
IImage* m_pIImage;
};
////////////////////////////////////////
#include "StdAfx.h"
#include <INITGUID.h>
#include "COM_IMAGE.h"
#include "AFX.h"
//#pragma comment (lib,”Ole32.lib”)
//#pragma comment(lib,"Imaging.lib")
//#pragma comment(lib,"uuid.lib")
IImagingFactory* COM_IMAGE::g_pImageFactory = NULL;
bool COM_IMAGE::G_Init()
{
HRESULT hr;
if (FAILED(hr = CoInitializeEx(NULL, COINIT_MULTITHREADED)))
{
TRACE(_T("Failed to COM_IMAGE!"));
MessageBox (NULL, TEXT("COINIT_MULTITHREADED ERROR!"), TEXT(""), MB_TOPMOST | MB_OK);
exit(0);
return false;
}
if (SUCCEEDED (CoCreateInstance (CLSID_ImagingFactory, //创建图像对象
NULL,
CLSCTX_INPROC_SERVER,
IID_IImagingFactory,
(void **)&g_pImageFactory)))
{
return true;
}
else
{
g_pImageFactory = NULL;
return false;
}
}
void COM_IMAGE::G_UnInit()
{
g_pImageFactory->Release();
CoUninitialize(); //释放COM资源
}
COM_IMAGE::COM_IMAGE():m_pIImage(NULL)
{
}
COM_IMAGE::~COM_IMAGE(void)
{
Release();
}
void COM_IMAGE::loadFile(LPCWSTR lpResourceName)
{
HRSRC hrRes;
HRESULT hr;
HGLOBAL hGlobal;
LPSTR lpRes;
DWORD dwSize;
HMODULE hModule_Current;
hModule_Current = GetModuleHandle (NULL); //获得文件句柄
hrRes = FindResource (hModule_Current, lpResourceName, RT_RCDATA); //查找资源
dwSize = SizeofResource (GetModuleHandle (NULL), hrRes); //获得资源文件大小
hGlobal = LoadResource (GetModuleHandle (NULL), hrRes); //加载文件
lpRes = (LPSTR)LockResource (hGlobal); //锁定资源文件
if (!SUCCEEDED (hr=g_pImageFactory->CreateImageFromFile (lpResourceName, &m_pIImage))) //从文件中创建对象
{
TRACE(_T("Failed to COM_IMAGE!"));
AfxMessageBox(lpResourceName);
DeleteObject(hrRes);
exit(0);
}
DeleteObject(hrRes);
}
IImage* COM_IMAGE::getIImage()
{
return m_pIImage;
}
void COM_IMAGE::drawImage(CDC* io_pDC, RECT i_rectDest, RECT i_rectSrc)
{
ImageInfo imfo;
m_pIImage->GetImageInfo(&imfo);
double dDotPermmX = imfo.Xdpi / 25.4;
double dDotPermmY = imfo.Ydpi / 25.4;
//pSrcRect指向以像素点为单位的区域
RECT rcSrc =
{
(LONG)(i_rectSrc.left / dDotPermmX / 0.01),
(LONG)(i_rectSrc.top / dDotPermmY / 0.01),
(LONG)(i_rectSrc.right / dDotPermmX / 0.01),
(LONG)(i_rectSrc.bottom / dDotPermmY / 0.01)
};
m_pIImage->Draw(*io_pDC, &i_rectDest, &rcSrc);
}
int COM_IMAGE::Width()
{
ImageInfo imfo;
m_pIImage->GetImageInfo(&imfo);
return imfo.Width;
}
int COM_IMAGE::Hight()
{
ImageInfo imfo;
m_pIImage->GetImageInfo(&imfo);
return imfo.Height;
}
void COM_IMAGE::Release()
{
if( NULL != m_pIImage)
m_pIImage->Release();
m_pIImage = NULL;
}