#include "stdafx.h" #include "Picture.h"#ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif////////////////////////////////////////////////////////////////////// // Construction/Destruction //////////////////////////////////////////////////////////////////////CPicture::CPicture() { m_pPicture = NULL; }CPicture::~CPicture() { UnLoad(); }bool CPicture::Load(CString sResourceType, CString sResource) { bool bResult = false; if (m_pPicture != NULL) UnLoad(); if (m_pPicture == NULL) { BYTE* pBuff = NULL; int nSize = 0; if (GetResource(sResource.GetBuffer(0), sResourceType.GetBuffer(0), pBuff, nSize)) { if (nSize > 0) { pBuff = new BYTE[nSize]; if (GetResource(sResource.GetBuffer(0), sResourceType.GetBuffer(0), pBuff, nSize)) { if (LoadFromBuffer(pBuff, nSize)) bResult = true; } delete [] pBuff; } } } return bResult; }bool CPicture::Load(CString sFileName) { bool bResult = false; if (m_pPicture != NULL) UnLoad(); if (m_pPicture == NULL) { CFile cFile; CFileException e; if (cFile.Open(sFileName, CFile::modeRead | CFile::typeBinary, &e)) { BYTE* pBuff = new BYTE[cFile.GetLength()]; if (cFile.Read(pBuff, cFile.GetLength()) > 0) { if (LoadFromBuffer(pBuff, cFile.GetLength())) bResult = true; } delete [] pBuff; } } return bResult; }void CPicture::UnLoad() { if (m_pPicture != NULL) { m_pPicture->Release(); m_pPicture = NULL; } }bool CPicture::Draw(CDC* pDC) { if (m_pPicture != NULL) { long hmWidth; long hmHeight; m_pPicture->get_Width(&hmWidth); m_pPicture->get_Height(&hmHeight); int nWidth = MulDiv(hmWidth, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH); int nHeight = MulDiv(hmHeight, pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH);
return Draw(pDC, 0, 0, nWidth, nHeight); } return false; }bool CPicture::Draw(CDC* pDC, CPoint Pos) { if (m_pPicture != NULL) { long hmWidth; long hmHeight; m_pPicture->get_Width(&hmWidth); m_pPicture->get_Height(&hmHeight); int nWidth = MulDiv(hmWidth, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH); int nHeight = MulDiv(hmHeight, pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH);
return Draw(pDC, Pos.x, Pos.y, nWidth, nHeight); } return false; }bool CPicture::Draw(CDC* pDC, CPoint Pos, CSize Size) { if (m_pPicture != NULL) { long hmWidth; long hmHeight; m_pPicture->get_Width(&hmWidth); m_pPicture->get_Height(&hmHeight); int nWidth = Size.cx; int nHeight = Size.cy;
return Draw(pDC, Pos.x, Pos.y, nWidth, nHeight); } return false; }bool CPicture::Draw(CDC* pDC, double nSizeRatio) { if (m_pPicture != NULL) { long hmWidth; long hmHeight; m_pPicture->get_Width(&hmWidth); m_pPicture->get_Height(&hmHeight); int nWidth = int(MulDiv(hmWidth, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH) * nSizeRatio); int nHeight = int(MulDiv(hmHeight, pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH) * nSizeRatio);
return Draw(pDC, 0, 0, nWidth, nHeight); } return false; }bool CPicture::Draw(CDC* pDC, CPoint Pos, double nSizeRatio) { if (m_pPicture != NULL) { long hmWidth; long hmHeight; m_pPicture->get_Width(&hmWidth); m_pPicture->get_Height(&hmHeight); int nWidth = int(MulDiv(hmWidth, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH) * nSizeRatio); int nHeight = int(MulDiv(hmHeight, pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH) * nSizeRatio);
return Draw(pDC, Pos.x, Pos.y, nWidth, nHeight); } return false; }bool CPicture::Draw(CDC* pDC, int x, int y, int cx, int cy) { long hmWidth; long hmHeight; m_pPicture->get_Width(&hmWidth); m_pPicture->get_Height(&hmHeight); if (m_pPicture->Render(pDC->m_hDC, x, y, cx, cy, 0, hmHeight, hmWidth, -hmHeight, NULL) == S_OK) return true;
my example:
HBITMAP CPicture::LoadImageFromID(UINT nIDRes)
{
try
{
HMODULE hInst=GetModuleHandle(NULL);
LPCTSTR lpRes = MAKEINTRESOURCE(nIDRes);
HRSRC hRsrc = ::FindResource(hInst, lpRes, "JPG");
if (NULL == hRsrc)
return FALSE;HGLOBAL hGlobal = LoadResource(hInst, hRsrc);
if (NULL == hGlobal)
return FALSE;DWORD dwSize = SizeofResource(hInst, hRsrc);
LPVOID lpData = LockResource(hGlobal);
if (NULL == lpData)
return FALSE;// alloc memory based on file size
HGLOBAL hJPG = ::GlobalAlloc(GMEM_MOVEABLE, dwSize);
LPVOID lpJGP = ::GlobalLock(hJPG);
memcpy(lpJGP, lpData, dwSize);
::GlobalUnlock(hJPG);//LPVOID pvData = GlobalLock(hGlobal);
//_ASSERTE(NULL != pvData);// read file and store in global memoryLPSTREAM pstm = NULL;
// create IStream* from global memory
HRESULT hr = CreateStreamOnHGlobal(hJPG, TRUE, &pstm);
_ASSERTE(SUCCEEDED(hr) && pstm);// Create IPicture from image file
LPPICTURE gpPicture;hr = ::OleLoadPicture(pstm, dwSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture);
_ASSERTE(SUCCEEDED(hr) && gpPicture);
pstm->Release();OLE_HANDLE m_picHandle;gpPicture->get_Handle(&m_picHandle);
return (HBITMAP)m_picHandle;
}
catch (...)
{}
return NULL;
}
//call
CPicture pic;
HBITMAP hbmp=pic.LoadImageFromID(IDR_TODAY);//IDR_TODAY is jpg id
(HBITMAP)::SendMessage(::GetDlgItem(this->m_hWnd,IDC_TODAY), STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbmp);
http://www.vckbase.com/document/finddoc.asp?keyword=JPG
http://search.csdn.net/Expert/topic/783/783299.xml?temp=.6597559
#if !defined(AFX_PICTURE_H__6098A4C3_D6D5_4711_BC7B_1595F459B480__INCLUDED_)
#define AFX_PICTURE_H__6098A4C3_D6D5_4711_BC7B_1595F459B480__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000class CPicture
{
public:
CPicture();
virtual ~CPicture(); bool Load(CString sResourceType, CString sResource);
bool Load(CString sFileName);
bool Draw(CDC* pDC);
bool Draw(CDC* pDC, CPoint Pos);
bool Draw(CDC* pDC, CPoint Pos, CSize Size);
bool Draw(CDC* pDC, double nSizeRatio);
bool Draw(CDC* pDC, CPoint Pos, double nSizeRatio);
CSize GetSize(CDC* pDC);
private:
static bool GetResource(LPSTR lpName, LPSTR lpType, void* pResource, int& nBufSize);
void UnLoad();
bool LoadFromBuffer(BYTE* pBuff, int nSize);
bool Draw(CDC* pDC, int x, int y, int cx, int cy);
IPicture* m_pPicture;
enum
{
HIMETRIC_INCH = 2540
};
};#endif // !defined(AFX_PICTURE_H__6098A4C3_D6D5_4711_BC7B_1595F459B480__INCLUDED_)
#include "stdafx.h"
#include "Picture.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////CPicture::CPicture()
{
m_pPicture = NULL;
}CPicture::~CPicture()
{
UnLoad();
}bool CPicture::Load(CString sResourceType, CString sResource)
{
bool bResult = false; if (m_pPicture != NULL)
UnLoad(); if (m_pPicture == NULL)
{
BYTE* pBuff = NULL;
int nSize = 0;
if (GetResource(sResource.GetBuffer(0), sResourceType.GetBuffer(0), pBuff, nSize))
{
if (nSize > 0)
{
pBuff = new BYTE[nSize]; if (GetResource(sResource.GetBuffer(0), sResourceType.GetBuffer(0), pBuff, nSize))
{
if (LoadFromBuffer(pBuff, nSize))
bResult = true;
} delete [] pBuff;
}
}
}
return bResult;
}bool CPicture::Load(CString sFileName)
{
bool bResult = false; if (m_pPicture != NULL)
UnLoad(); if (m_pPicture == NULL)
{
CFile cFile;
CFileException e; if (cFile.Open(sFileName, CFile::modeRead | CFile::typeBinary, &e))
{
BYTE* pBuff = new BYTE[cFile.GetLength()]; if (cFile.Read(pBuff, cFile.GetLength()) > 0)
{
if (LoadFromBuffer(pBuff, cFile.GetLength()))
bResult = true;
} delete [] pBuff;
}
}
return bResult;
}void CPicture::UnLoad()
{
if (m_pPicture != NULL)
{
m_pPicture->Release();
m_pPicture = NULL;
}
}bool CPicture::Draw(CDC* pDC)
{
if (m_pPicture != NULL)
{
long hmWidth;
long hmHeight;
m_pPicture->get_Width(&hmWidth);
m_pPicture->get_Height(&hmHeight); int nWidth = MulDiv(hmWidth, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH);
int nHeight = MulDiv(hmHeight, pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH);
return Draw(pDC, 0, 0, nWidth, nHeight);
}
return false;
}bool CPicture::Draw(CDC* pDC, CPoint Pos)
{
if (m_pPicture != NULL)
{
long hmWidth;
long hmHeight;
m_pPicture->get_Width(&hmWidth);
m_pPicture->get_Height(&hmHeight); int nWidth = MulDiv(hmWidth, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH);
int nHeight = MulDiv(hmHeight, pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH);
return Draw(pDC, Pos.x, Pos.y, nWidth, nHeight);
}
return false;
}bool CPicture::Draw(CDC* pDC, CPoint Pos, CSize Size)
{
if (m_pPicture != NULL)
{
long hmWidth;
long hmHeight;
m_pPicture->get_Width(&hmWidth);
m_pPicture->get_Height(&hmHeight); int nWidth = Size.cx;
int nHeight = Size.cy;
return Draw(pDC, Pos.x, Pos.y, nWidth, nHeight);
}
return false;
}bool CPicture::Draw(CDC* pDC, double nSizeRatio)
{
if (m_pPicture != NULL)
{
long hmWidth;
long hmHeight;
m_pPicture->get_Width(&hmWidth);
m_pPicture->get_Height(&hmHeight); int nWidth = int(MulDiv(hmWidth, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH) * nSizeRatio);
int nHeight = int(MulDiv(hmHeight, pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH) * nSizeRatio);
return Draw(pDC, 0, 0, nWidth, nHeight);
}
return false;
}bool CPicture::Draw(CDC* pDC, CPoint Pos, double nSizeRatio)
{
if (m_pPicture != NULL)
{
long hmWidth;
long hmHeight;
m_pPicture->get_Width(&hmWidth);
m_pPicture->get_Height(&hmHeight); int nWidth = int(MulDiv(hmWidth, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH) * nSizeRatio);
int nHeight = int(MulDiv(hmHeight, pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH) * nSizeRatio);
return Draw(pDC, Pos.x, Pos.y, nWidth, nHeight);
}
return false;
}bool CPicture::Draw(CDC* pDC, int x, int y, int cx, int cy)
{
long hmWidth;
long hmHeight;
m_pPicture->get_Width(&hmWidth);
m_pPicture->get_Height(&hmHeight); if (m_pPicture->Render(pDC->m_hDC, x, y, cx, cy, 0, hmHeight, hmWidth, -hmHeight, NULL) == S_OK)
return true;
return false;
}bool CPicture::LoadFromBuffer(BYTE* pBuff, int nSize)
{
bool bResult = false; HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);
void* pData = GlobalLock(hGlobal);
memcpy(pData, pBuff, nSize);
GlobalUnlock(hGlobal); IStream* pStream = NULL; if (CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
{
HRESULT hr;
if ((hr = OleLoadPicture(pStream, nSize, FALSE, IID_IPicture, (LPVOID *)&m_pPicture)) == S_OK)
bResult = true;
pStream->Release();
} return bResult;
}bool CPicture::GetResource(LPSTR lpName, LPSTR lpType, void* pResource, int& nBufSize)
{
HRSRC hResInfo;
HANDLE hRes;
HMODULE hInst = NULL;
LPSTR lpRes = NULL;
int nLen = 0;
bool bResult = FALSE; // Find the resource
hResInfo = FindResource(hInst, lpName, lpType); if (hResInfo == NULL)
return false; // Load the resource
hRes = LoadResource(hInst, hResInfo); if (hRes == NULL)
return false; // Lock the resource
lpRes = (char*)LockResource(hRes); if (lpRes != NULL)
{
if (pResource == NULL)
{
nBufSize = SizeofResource(hInst, hResInfo);
bResult = true;
}
else
{
if (nBufSize >= (int)SizeofResource(hInst, hResInfo))
{
memcpy(pResource, lpRes, nBufSize);
bResult = true;
}
} UnlockResource(hRes);
} // Free the resource
FreeResource(hRes); return bResult;
}CSize CPicture::GetSize(CDC* pDC)
{
CSize rcResult = CSize(0,0);
if (m_pPicture != NULL)
{
long hmWidth;
long hmHeight;
m_pPicture->get_Width(&hmWidth);
m_pPicture->get_Height(&hmHeight); rcResult.cx = MulDiv(hmWidth, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH);
rcResult.cy = MulDiv(hmHeight, pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH);
} return rcResult;
}