www.pudn.com > dip_1_1_bmp2jpeg.rar > JpegDisplay.cpp


// JpegDisplay.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "dip_1_1.h" 
#include "JpegDisplay.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CJpegDisplay dialog 
 
 
CJpegDisplay::CJpegDisplay(CWnd* pParent /*=NULL*/) 
	: CDialog(CJpegDisplay::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CJpegDisplay) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
} 
 
 
void CJpegDisplay::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CJpegDisplay) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CJpegDisplay, CDialog) 
	//{{AFX_MSG_MAP(CJpegDisplay) 
	ON_WM_PAINT() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CJpegDisplay message handlers 
 
 
BOOL CJpegDisplay::OnInitDialog()  
{ 
	CDialog::OnInitDialog(); 
 
	 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
 
 
  void   CJpegDisplay::OnPaint()      
  {    
    CPaintDC   dc(this);   //   device   context   for   painting    
 
    ShowPhoto(&dc,m_pathname);  
  }    
     
     
  //作图时调用下面这个函数,是绘jpg图片到cdc上的!    
  BOOL   CJpegDisplay::ShowPhoto(CDC   *pDC,   CString   strPath)    
{    
	IStream   *pStm;        
	CFileStatus   fstatus;        
	CFile   file;        
	LONG   cb;        
	//打开文件并检测文件的有效性    
	if(file.Open(strPath,CFile::modeRead)&&file.GetStatus(strPath,fstatus)&&((cb   =   fstatus.m_size)   !=   -1))        
	{            
		HGLOBAL   hGlobal   =   GlobalAlloc(GMEM_MOVEABLE,   cb);        
		LPVOID   pvData   =   NULL;        
		if   (hGlobal   !=   NULL)        
		{        
			pvData   =   GlobalLock(hGlobal);    
			if   (pvData   !=   NULL)        
			{        
				file.ReadHuge(pvData,   cb);        
				GlobalUnlock(hGlobal);        
				CreateStreamOnHGlobal(hGlobal,   TRUE,   &pStm);        
			}      
		}      
	}    
	else    
	{    
		return   false;    
	}   //打开文件结束    
	//显示JPEG格式的图片,    
	IPicture   *pPic;      
	//load   image   from   file   stream    
	if(SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic)))      
	{      
		OLE_XSIZE_HIMETRIC   hmWidth;        
		OLE_YSIZE_HIMETRIC   hmHeight;        
		pPic->get_Width(&hmWidth);        
		pPic->get_Height(&hmHeight);        
		double   fX,fY;        
 
		//get   image   height   and   width    
		fX   =   (double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/((double)pDC->GetDeviceCaps(HORZSIZE)*100.0);    
		fY   =   (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/((double)pDC->GetDeviceCaps(VERTSIZE)*100.0);        
		//use   render   function   display   image    
 
		int   CXS=::GetSystemMetrics(SM_CXSCREEN);    
		int   CYS=::GetSystemMetrics(SM_CYSCREEN);    
		this->SetWindowPos(NULL,(int)((CXS-(int)fX)/2),(int)((CYS-(int)fY)/2),fX,fY,SWP_SHOWWINDOW);    
 
		if(FAILED(pPic->Render(*pDC,0,0,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-hmHeight,NULL)))    
		{    
			pPic->Release();    
			return   false;    
		}    
		pPic->Release();        
	}        
	else        
	{    
		return   false;        
	}    
	return   true;    
}