HBITMAP HdTextureSelectDlg::LoadPicture(CString mFile) { CString pFSize; WCHAR wpath[MAX_PATH]; //maps a character string to a wide-character (Unicode) string MultiByteToWideChar(CP_ACP, 0, mFile, -1, wpath, MAX_PATH); IPicture* pPic; //Creates a new picture object and initializes it from the contents of a stream OleLoadPicturePath(wpath, NULL, NULL, NULL, IID_IPicture,(LPVOID*)&pPic); if (pPic==NULL) return NULL; HBITMAP hPic = NULL; pPic->get_Handle((UINT*)&hPic); //Returns the Windows GDI handle of the picture long nWidth=THUMWIDTH; long nHeight=THUMHEIGHT; long mWid,mHei; pPic->get_Height(&mHei); pPic->get_Width(&mWid); // Create Brushes for Border and BackGround HBRUSH hBrushBorder=::CreateSolidBrush(RGB(218,218,218)); HBRUSH hBrushBk=::CreateSolidBrush(RGB(255,255,255)); //(RGB(218,218,218) // Border Size RECT rcBorder; rcBorder.left=rcBorder.top=0; rcBorder.right=THUMWIDTH; rcBorder.bottom=THUMHEIGHT; const float fRatio=(float)THUMHEIGHT/THUMWIDTH; int XDest, YDest, nDestWidth, nDestHeight; // Calculate Rect to fit to canvas const float fImgRatio=(float)mHei/mWid; if(fImgRatio > fRatio) { nDestWidth=(int)(THUMHEIGHT/fImgRatio); XDest=(THUMWIDTH-nDestWidth)/2; YDest=0; nDestHeight=THUMHEIGHT; } else { XDest=0; nDestWidth=THUMWIDTH; nDestHeight=(int)(THUMWIDTH*fImgRatio); YDest=(THUMHEIGHT-nDestHeight)/2; } //creates a new image (icon, cursor, or bitmap) and copies the attributes of the specified image to the new one. HBITMAP hPicRet = (HBITMAP)CopyImage(hPic, IMAGE_BITMAP, nDestWidth, nDestHeight , LR_COPYDELETEORG); CClientDC cdc(this); //creates a memory device context (DC) compatible with the specified device HDC hDC=::CreateCompatibleDC(cdc.m_hDC); // creates a bitmap compatible with the device that is associated with the specified device context. HBITMAP bm = CreateCompatibleBitmap(cdc.m_hDC, THUMWIDTH, THUMHEIGHT); //selects an object into the specified device context HBITMAP pOldBitmapImage = (HBITMAP)SelectObject(hDC,bm); // Draw Background ::FillRect(hDC, &rcBorder, hBrushBk); ::FrameRect(hDC, &rcBorder, hBrushBorder); HBITMAP hBmReturn= (HBITMAP)::SelectObject(hDC, pOldBitmapImage); CDC hdcSrc, hdcDst; hdcSrc.CreateCompatibleDC(NULL); hdcDst.CreateCompatibleDC(NULL); // Load the bitmaps into memory DC CBitmap* hbmSrcT = (CBitmap*) hdcSrc.SelectObject(hPicRet); CBitmap* hbmDstT = (CBitmap*) hdcDst.SelectObject(hBmReturn); hdcDst.BitBlt(XDest+1,YDest+1,nDestWidth-2, nDestHeight-2, &hdcSrc,0,0,SRCCOPY); pOldBitmapImage = (HBITMAP)SelectObject(hdcDst.m_hDC,bm); // Release used DC and Object DeleteDC(hDC); DeleteDC (hdcSrc ); DeleteDC (hdcDst ); DeleteObject(hBrushBorder); DeleteObject(hBrushBk); DeleteObject (hPic ); DeleteObject (hPicRet ); // DeleteObject (bm); // DeleteObject (hBmReturn); // delete hbmSrcT ; // delete hbmDstT ; return pOldBitmapImage;}
{
CString pFSize;
WCHAR wpath[MAX_PATH];
//maps a character string to a wide-character (Unicode) string
MultiByteToWideChar(CP_ACP, 0, mFile, -1, wpath, MAX_PATH); IPicture* pPic;
//Creates a new picture object and initializes it from the contents of a stream
OleLoadPicturePath(wpath, NULL, NULL, NULL, IID_IPicture,(LPVOID*)&pPic); if (pPic==NULL) return NULL;
HBITMAP hPic = NULL;
pPic->get_Handle((UINT*)&hPic); //Returns the Windows GDI handle of the picture long nWidth=THUMWIDTH;
long nHeight=THUMHEIGHT; long mWid,mHei;
pPic->get_Height(&mHei);
pPic->get_Width(&mWid); // Create Brushes for Border and BackGround
HBRUSH hBrushBorder=::CreateSolidBrush(RGB(218,218,218));
HBRUSH hBrushBk=::CreateSolidBrush(RGB(255,255,255)); //(RGB(218,218,218) // Border Size
RECT rcBorder;
rcBorder.left=rcBorder.top=0;
rcBorder.right=THUMWIDTH;
rcBorder.bottom=THUMHEIGHT; const float fRatio=(float)THUMHEIGHT/THUMWIDTH; int XDest, YDest, nDestWidth, nDestHeight;
// Calculate Rect to fit to canvas
const float fImgRatio=(float)mHei/mWid;
if(fImgRatio > fRatio)
{
nDestWidth=(int)(THUMHEIGHT/fImgRatio);
XDest=(THUMWIDTH-nDestWidth)/2;
YDest=0;
nDestHeight=THUMHEIGHT;
}
else
{
XDest=0;
nDestWidth=THUMWIDTH;
nDestHeight=(int)(THUMWIDTH*fImgRatio);
YDest=(THUMHEIGHT-nDestHeight)/2;
}
//creates a new image (icon, cursor, or bitmap) and copies the attributes of the specified image to the new one.
HBITMAP hPicRet = (HBITMAP)CopyImage(hPic, IMAGE_BITMAP, nDestWidth, nDestHeight , LR_COPYDELETEORG); CClientDC cdc(this);
//creates a memory device context (DC) compatible with the specified device
HDC hDC=::CreateCompatibleDC(cdc.m_hDC);
// creates a bitmap compatible with the device that is associated with the specified device context.
HBITMAP bm = CreateCompatibleBitmap(cdc.m_hDC, THUMWIDTH, THUMHEIGHT);
//selects an object into the specified device context
HBITMAP pOldBitmapImage = (HBITMAP)SelectObject(hDC,bm); // Draw Background
::FillRect(hDC, &rcBorder, hBrushBk);
::FrameRect(hDC, &rcBorder, hBrushBorder); HBITMAP hBmReturn= (HBITMAP)::SelectObject(hDC, pOldBitmapImage); CDC hdcSrc, hdcDst; hdcSrc.CreateCompatibleDC(NULL);
hdcDst.CreateCompatibleDC(NULL); // Load the bitmaps into memory DC
CBitmap* hbmSrcT = (CBitmap*) hdcSrc.SelectObject(hPicRet);
CBitmap* hbmDstT = (CBitmap*) hdcDst.SelectObject(hBmReturn); hdcDst.BitBlt(XDest+1,YDest+1,nDestWidth-2, nDestHeight-2, &hdcSrc,0,0,SRCCOPY); pOldBitmapImage = (HBITMAP)SelectObject(hdcDst.m_hDC,bm); // Release used DC and Object
DeleteDC(hDC);
DeleteDC (hdcSrc );
DeleteDC (hdcDst );
DeleteObject(hBrushBorder);
DeleteObject(hBrushBk);
DeleteObject (hPic );
DeleteObject (hPicRet );
// DeleteObject (bm);
// DeleteObject (hBmReturn);
// delete hbmSrcT ;
// delete hbmDstT ; return pOldBitmapImage;}
http://www.codeproject.com/bitmap/cximage.asp
{
wchar_t wszFile[MAX_PATH];
MultiByteToWideChar( CP_ACP, 0, szFile, -1, wszFile, MAX_PATH ); IPicture *pPicture;
OleLoadPicturePath( wszFile, NULL, 0, 0, IID_IPicture, (void**)&pPicture );
if( pPicture == NULL )
return FALSE; OLE_XPOS_HIMETRIC nWidth, nHeight;
pPicture->get_Width( &nWidth );
pPicture->get_Height( &nHeight ); pPicture->Render( hDC, r.left, r.top, r.right-r.left+1, r.bottom-r.top+1, 0, nHeight-1, nWidth, -nHeight, NULL );
pPicture->Release();
return TRUE;
}
#pragma once
#include <atlbase.h>//////////////////
// Picture object--encapsulates IPicture
//
class CPicture {
public:
CPicture();
~CPicture(); // Load frm various sosurces
BOOL Load(UINT nIDRes);
BOOL Load(LPCTSTR pszPathName);
BOOL Load(CFile& file);
BOOL Load(CArchive& ar);
BOOL Load(IStream* pstm); // render to device context
BOOL Render(CDC* pDC, CRect rc=CRect(0,0,0,0),
LPCRECT prcMFBounds=NULL) const;
CSize GetImageSize(CDC* pDC=NULL) const;
operator IPicture*() {
return m_spIPicture;
}
void GetHIMETRICSize(OLE_XSIZE_HIMETRIC& cx, OLE_YSIZE_HIMETRIC& cy) const {
cx = cy = 0;
const_cast<CPicture*>(this)->m_hr = m_spIPicture->get_Width(&cx);
ASSERT(SUCCEEDED(m_hr));
const_cast<CPicture*>(this)->m_hr = m_spIPicture->get_Height(&cy);
ASSERT(SUCCEEDED(m_hr));
} void Free() {
if (m_spIPicture) {
m_spIPicture.Release();
}
}protected:
CComQIPtr<IPicture>m_spIPicture; // ATL smart pointer to IPicture
HRESULT m_hr; // last error code
};CPicture.cpp
#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()
{
}CPicture::~CPicture()
{
}//////////////////
// 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; // create memory file and load it
CMemFile file(lpRsrc, len);
BOOL bRet = Load(file);
FreeResource(hRsrc);
GlobalFree(lpRsrc);
return bRet;
}//////////////////
// Load from path name.
//
BOOL CPicture::Load(LPCTSTR pszPathName)
{
CFile file;
if (!file.Open(pszPathName, CFile::modeRead|CFile::shareDenyWrite))
return FALSE;
BOOL bRet = Load(file);
file.Close();
return bRet;
}//////////////////
// Load from CFile
//
BOOL CPicture::Load(CFile& file)
{
CArchive ar(&file, CArchive::load | CArchive::bNoFlushOnDelete);
return Load(ar);
}//////////////////
// Load from archive--create stream and load from stream.
//
BOOL CPicture::Load(CArchive& ar)
{
CArchiveStream arcstream(&ar);
return Load((IStream*)&arcstream);
}//////////////////
// Load from stream (IStream). This is the one that really does it: call
// OleLoadPicture to do the work.
//
BOOL CPicture::Load(IStream* pstm)
{
Free();
HRESULT hr = OleLoadPicture(pstm, 0, FALSE,
IID_IPicture, (void**)&m_spIPicture);
ASSERT(SUCCEEDED(hr) && m_spIPicture);
return TRUE;
}//////////////////
// Render to device context. Covert to HIMETRIC for IPicture.
//
BOOL CPicture::Render(CDC* pDC, CRect rc, LPCRECT prcMFBounds) const
{
ASSERT(pDC); if (rc.IsRectNull()) {
CSize sz = GetImageSize(pDC);
rc.right = sz.cx;
rc.bottom = sz.cy;
}
long hmWidth,hmHeight; // HIMETRIC units
GetHIMETRICSize(hmWidth, hmHeight);
m_spIPicture->Render(*pDC, rc.left, rc.top, rc.Width(), rc.Height(),
0, hmHeight, hmWidth, -hmHeight, prcMFBounds); return TRUE;
}//////////////////
// Get image size in pixels. Converts from HIMETRIC to device coords.
//
CSize CPicture::GetImageSize(CDC* pDC) const
{
if (!m_spIPicture)
return CSize(0,0);
LONG hmWidth, hmHeight; // HIMETRIC units
m_spIPicture->get_Width(&hmWidth);
m_spIPicture->get_Height(&hmHeight);
CSize sz(hmWidth,hmHeight);
if (pDC==NULL) {
CWindowDC dc(NULL);
dc.HIMETRICtoDP(&sz); // convert to pixels
} else {
pDC->HIMETRICtoDP(&sz);
}
return sz;
}
操作步骤:
1,创建新工程(单文档)
2,将CPicture.h&CPicture.cpp添加到工程3,在"文件"菜单项的"打开"中添加消息响应函数,
在函数中创建Picture的对象,
CPicture M_Picture;
之后想在工作区窗口中显示JPG或别的格式的图象,
对于里面的Load函数和Show函数不知道如何应用!!
再次求助!!
谢谢