www.pudn.com > ViewerCarry.rar > PhookImageOp.cpp


// PhookImageOp.cpp: implementation of the CPhookImageOp class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "thumbviewer.h" 
#include "PhookImageOp.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CPhookImageOp::CPhookImageOp() 
{ 
    m_pHuageImage = NULL; 
    m_nImageSize = 0; 
	m_pImageBuff = new char[MAX_IMAGE_SIZE+1]; 
	memset(m_pImageBuff,0,MAX_IMAGE_SIZE+1); 
} 
 
CPhookImageOp::~CPhookImageOp() 
{ 
    m_pHuageImage = NULL; 
    m_nImageSize = 0; 
	 
	if(NULL != m_pImageBuff) 
	{ 
		delete m_pImageBuff;//删除图片缓冲 
	    m_pImageBuff = NULL; 
    } 
} 
 
int CPhookImageOp::OpenImage(CString sImageFile) 
{ 
  if(!m_fImage.Open(sImageFile,CFile::modeRead|CFile::typeBinary,&m_exImage)) 
  { 
     TCHAR szErrorTmp[1024]={0}; 
     m_exImage.GetErrorMessage(szErrorTmp, 1024); 
     TCHAR szError[1024]={0}; 
     _stprintf(szError,_T("不能打开文件,错误码=<%s>"),szError); 
     AfxMessageBox(szError); 
     return -1; 
  } 
    return 0; 
} 
 
int CPhookImageOp::CreateImage(CString sImageFile) 
{ 
	if(!m_fImage.Open(sImageFile,CFile::modeCreate|CFile::modeReadWrite|CFile::typeBinary,&m_exImage)) 
   { 
     TCHAR szErrorTmp[1024]={0}; 
     m_exImage.GetErrorMessage(szErrorTmp, 1024); 
     TCHAR szError[1024]={0}; 
     _stprintf(szError,_T("不能创建文件,错误码=<%s>"),szError); 
     AfxMessageBox(szError); 
     return -1; 
   } 
    return 0; 
} 
 
int CPhookImageOp::GetImageLen() 
{ 
   int nImageLen=0; 
   if( (nImageLen=m_fImage.GetLength()) <= 0) 
   { 
      AfxMessageBox("图片大小等于0!"); 
    return -1; 
   } 
   return nImageLen; 
} 
 
int CPhookImageOp::ReadHugeImage(int nImageSize) 
{ 
    int nImageLen=0; 
	if(nImageSize > MAX_IMAGE_SIZE) 
	{ 
	    AfxMessageBox("最大文件大小已经超过一次读取的最大量"); 
		return -1; 
	} 
    if((NULL != m_pHuageImage) && (0 >= nImageSize)) 
	{ 
     if((nImageLen = m_fImage.ReadHuge(m_pHuageImage,nImageSize)) <= 0) 
     { 
         AfxMessageBox("读文件出错!"); 
       return -1; 
     } 
  }//end if 
  return nImageLen; 
} 
//循环读文件 
int CPhookImageOp::ReadImage(int nImageSize) 
{ 
    int nbyte=0,nImageBuffCount=0,nTmpCopySize=0; 
	while(nImageBuffCount < nImageSize) 
	{ 
	   nbyte=m_fImage.Read(m_pImageBuff+nImageBuffCount,MAX_READIMAGE_COUNT); 
	   if(nImageBuffCount <= MAX_IMAGE_SIZE) 
	   { 
           nImageBuffCount +=nbyte; 
       }  
	   else 
	   { 
	       AfxMessageBox("图片不能超过16M,否则将不能处理"); 
		   return -1; 
	   } 
	}//end while 
 
	return 0; 
} 
// 
int CPhookImageOp::WriteImage(CString &sImageBuff,int nImageSize) 
{ 
    int nImageBuffCount=0; 
	LPTSTR  pImageBuff = sImageBuff.GetBuffer(0); 
	while(nImageBuffCount < nImageSize)//写文件 
	{ 
		m_fImage.Write(pImageBuff+nImageBuffCount,MAX_WRITEIMAGE_COUNT); 
	    nImageBuffCount +=MAX_WRITEIMAGE_COUNT; 
	}//end while 
	return 0; 
} 
 
int CPhookImageOp::WriteImage(char *sImageBuff,int nImageSize) 
{ 
    int nImageBuffCount=0; 
	LPTSTR  pImageBuff = sImageBuff; 
	while(nImageBuffCount < nImageSize)//写文件 
	{ 
		m_fImage.Write(pImageBuff+nImageBuffCount,MAX_WRITEIMAGE_COUNT); 
	    nImageBuffCount +=MAX_WRITEIMAGE_COUNT; 
	}//end while 
	  return 0; 
} 
void CPhookImageOp::CloseImage() 
{ 
    m_fImage.Close(); 
} 
 
void far *CPhookImageOp::AllocImageMem() 
{ 
    if((m_pHuageImage = GlobalAlloc(GPTR,m_nImageSize)) == NULL) 
	{ 
       TCHAR szError[1024]={0}; 
       _stprintf(szError,_T("GlobalAlloc分配内存错误=<%s>"),GetLastError()); 
       AfxMessageBox(szError); 
       return NULL; 
	} 
    return m_pHuageImage; 
} 
 
int CPhookImageOp::FreeImageMem() 
{ 
    
  if(NULL != GlobalFree(m_pHuageImage)) 
  { 
        AfxMessageBox("释放内存错误!");  
    return -1; 
  } 
  return 0; 
} 
 
void * CPhookImageOp::GetImageBuff() 
{ 
      return (void*)m_pImageBuff; 
} 
 
CString CPhookImageOp::CreatImageStorePath(CString sStorePath) 
{ 
    CString sTmp(IMAGE_STORE_PATH); 
	CString sFileRoute = sTmp + "\\" + sStorePath; 
 
	//查看录是否存在,不存在则创建目录 
	if(!PathFileExists(sFileRoute)) 
	{ 
	  if(CreateDirectory(sFileRoute,NULL) == 0) 
	  { 
	      AfxMessageBox("创建目录错误"); 
	  } 
    }//end if 
    return sFileRoute;  
} 
 
CString CPhookImageOp::GetNowPath() 
{ 
    TCHAR sDrive[_MAX_DRIVE]={0}; 
	TCHAR sDir[_MAX_DIR]={0}; 
	TCHAR sFname[_MAX_FNAME]={0}; 
	TCHAR sExt[_MAX_EXT]={0}; 
	TCHAR sFilename[MAX_PATH]={0}; 
 
	::GetModuleFileName(NULL,sFilename,MAX_PATH); 
	_tsplitpath(sFilename, sDrive, sDir, sFname, sExt); 
	CString rVal; 
	rVal.Format(_T("%s%s"), sDrive, sDir); 
	return rVal; 
}