#pragma once
/********************************************************************
file name : g:/工具源码/WinCE/CE600WinCode/COMImage.h
author : Clark
created : 27:1:2011 16:09
purpose : 实现IImage操作
*********************************************************************/
#include <Imaging.h>
class IImageEx
{
public:
static BOOL Init();
static void UnInit();
static HBITMAP LoadImageFromFile(TCHAR * pFileImage);
public:
explicit IImageEx();
~IImageEx();
BOOL LoadBuf(HINSTANCE hResource, UINT ResID, TCHAR* lpType);
void loadFile(LPCWSTR lpResourceName);
void drawImage(HDC* pDC, RECT rtDest, RECT rtSrc);
static IImagingFactory *g_pIImgFactory;
IImage* m_pIImage;
};
////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <INITGUID.h>
#include "COMImage.h"
#include <AFX.h>
#include <string.h>
#pragma comment (lib,"Ole32.lib")
IImagingFactory* IImageEx::g_pIImgFactory = NULL;
BOOL IImageEx::Init()
{
CoInitializeEx(NULL,COINIT_MULTITHREADED);
HRESULT hResult = CoCreateInstance(CLSID_ImagingFactory,NULL,
CLSCTX_INPROC_SERVER,
IID_IImagingFactory,
(void **)&g_pIImgFactory);
return (hResult == S_OK);
}
void IImageEx::UnInit()
{
if(g_pIImgFactory)
{
g_pIImgFactory->Release();
g_pIImgFactory = NULL;
}
CoUninitialize();
}
IImageEx::IImageEx():m_pIImage(NULL){}
IImageEx::~IImageEx()
{
if( NULL != m_pIImage)
{
m_pIImage->Release();
delete m_pIImage;
}
m_pIImage = NULL;
}
HBITMAP IImageEx::LoadImageFromFile(TCHAR * pFileImage)
{
HBITMAP hResult = 0;
IImage *pImageBmp = NULL;
ImageInfo imageInfo;
HRESULT hr = g_pIImgFactory->CreateImageFromFile(pFileImage, &pImageBmp);
if (SUCCEEDED(hr)&& SUCCEEDED(pImageBmp->GetImageInfo(&imageInfo)))
{
CWindowDC dc(0);
CDC dcBitmap;
dcBitmap.CreateCompatibleDC(&dc);
hResult = CreateCompatibleBitmap(dc.GetSafeHdc(),imageInfo.Width, imageInfo.Height);
HGDIOBJ hOldBitmap = SelectObject(dcBitmap, hResult);
//note:内存不足的话,Draw操作可能会失败
pImageBmp->Draw(dcBitmap, CRect(0, 0, imageInfo.Width, imageInfo.Height), NULL);
SelectObject(dcBitmap, hOldBitmap);
DeleteDC(dcBitmap);
pImageBmp->Release();
}
return hResult;
}
void IImageEx::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_pIImgFactory->CreateImageFromFile (lpResourceName, &m_pIImage))) //从文件中创建对象
{
TRACE(_T("Failed to COM_IMAGE!"));
AfxMessageBox(lpResourceName);
DeleteObject(hrRes);
exit(0);
}
DeleteObject(hrRes);
}
BOOL IImageEx::LoadBuf(HINSTANCE hResource, UINT ResID, TCHAR* lpType)
{
if(NULL == g_pIImgFactory)
return FALSE;
if(!hResource)
return FALSE;
HRSRC hRes = FindResource(hResource , MAKEINTRESOURCE(ResID) , lpType);
if(!hRes)
{
return FALSE;
}
HGLOBAL hGlobal = LoadResource(hResource , hRes);
if(!hGlobal)
{
return FALSE;
}
LPVOID pData = LockResource( hGlobal );
DWORD dwSize = SizeofResource(hResource , hRes);
g_pIImgFactory->CreateImageFromBuffer(pData , dwSize , BufferDisposalFlagNone , &m_pIImage);
FreeResource(hGlobal);
pData = NULL;
return true;
}
void IImageEx::drawImage(HDC* pDC, RECT rtDest, RECT rtSrc)
{
ImageInfo imfo;
m_pIImage->GetImageInfo(&imfo);
double dDotPermmX = imfo.Xdpi / 25.4;
double dDotPermmY = imfo.Ydpi / 25.4;
//pSrcRect指向以像素点为单位的区域
RECT rcSrc =
{
(LONG)(rtSrc.left / dDotPermmX / 0.01),
(LONG)(rtSrc.top / dDotPermmY / 0.01),
(LONG)(rtSrc.right / dDotPermmX / 0.01),
(LONG)(rtSrc.bottom / dDotPermmY / 0.01)
};
m_pIImage->Draw(*pDC, &rtDest, &rcSrc);
}