⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpic.cpp

📁 在窗口里的图像预览
💻 CPP
字号:


#include "cPic.h"

/**************************************************************************
 *                                                                        *
 *                  " T H E   _ F I L E _  C L A S S "                    *
 *                                                                        *
 **************************************************************************/

/*
WHY THIS CLASS?
JUST TO MAKE THINGS EASIER WHEN READING THE IMAGE FILE
*/

//constructor
_FILE_::_FILE_()
{
	m_File = NULL;
}
//destructor !
_FILE_::~_FILE_()
{
//	this->Close();
}

//opens a file either for reading or writing
BOOL _FILE_::Open(char * FileName ,FILE_OPENMODE Open_Mode)
{

	switch (Open_Mode)
	{
		case OFM_READ:
			m_File = fopen(FileName,"rb");
			break;
		case OFM_WRITE:
			m_File = fopen(FileName,"wb");
			break;
	}

	if(!m_File) return FALSE;
	return TRUE;
}


//Reads data from the file stream.
BOOL _FILE_::Read(VOID * zBuffer,DWORD cSize)
{
	if(fread(zBuffer,sizeof(BYTE),cSize,m_File) < cSize)
		return FALSE;
	return TRUE;
}

//Writes data to the file stream.
BOOL _FILE_::Write(VOID * zBuffer,DWORD cSize)
{
	if(fwrite(zBuffer,sizeof(BYTE),cSize,m_File) < cSize)
       return FALSE;
	return TRUE;
}

//Gets the File Size
LONG _FILE_::GetSize(VOID)
{
	long t_location =  ftell(m_File);
	fseek(this->m_File,0,SEEK_END);
	long f_size     =  ftell(this->m_File );
	fseek(this->m_File ,t_location,SEEK_SET);
	return f_size;
}

//Closes the File
VOID _FILE_::Close(VOID)
{
	if(m_File)
	{
		fclose(this->m_File);
	    this->m_File  = NULL;
	}
	return;
}


/**************************************************************************
 *                                                                        *
 *               " T H E   _ P I C T U R E _   C L A S S "                *
 *                                                                        *
 **************************************************************************/

/*
  A CLASS FOR READING IMAGE FILES LIKE JPG,BMP,WMF,GIF...LIKE THE ONE IN VISUAL BASIC
  AND DRAWING THEM AS WELL
*/


//constructor
_PICTURE_::_PICTURE_()
{
	m_pic = NULL;
	hGlobal = NULL;
}
//destructor
_PICTURE_::~_PICTURE_()
{
	this->FreePicture();
}

/*
OUR MAIN FUNCTION
*/
HBITMAP _PICTURE_::LoadPicture(char *FileName)
{
	_FILE_ cFile;  //the image file
	
	//if we couldn't open the image file then we should get out of here
    if(!cFile.Open(FileName,OFM_READ)) return NULL;

	//we must know exactly the buffer size to allocate
	//in order to read the image in it
	//so get the image file size
    long nSize = cFile.GetSize();
	//allocate enough memory to hold the image
	//it must be allocated using GlobalAlloc
	//otherwise it will fail...try using new or malloc and you'll see!!!
	hGlobal = GlobalAlloc(GMEM_MOVEABLE,nSize);
	//get pointer to first byte
	void* pData = GlobalLock(hGlobal);
	//read the file in the prev allocated memory
	if(!cFile.Read(pData,nSize)) return NULL;;
	//we don't need the file any more so close it
	cFile.Close();
	GlobalUnlock(hGlobal);
	IStream* pStream = NULL;
    //ole routines....
	//creates a stream from our handle
	//don't know why and how?
	if (CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) != S_OK)
	return NULL;

	HRESULT hr;
	//aha..now let ole reads the image and load it for us
	if ((hr = OleLoadPicture(pStream, nSize, FALSE, IID_IPicture, (LPVOID *)&this->m_pic)) != S_OK)
	return NULL;
	HBITMAP hbmp = NULL;
	//return an HBITMAP to our image like the LoadImage function does
	//we might need the handle
	this->m_pic->get_Handle((OLE_HANDLE *) &hbmp);
	return hbmp;
}


//an overloaded version of the above to draw the picture
//as well in a given DC
HBITMAP _PICTURE_::LoadPicture(char *FileName,HDC hdc)
{
	HBITMAP hbmp = this->LoadPicture(FileName);
	this->DrawPicture(hdc,0,0,this->_GetWidth(),_GetHeight());
	return hbmp;
}

//returns the width of the loaded image
DWORD _PICTURE_::_GetWidth(VOID)
{
    
	if(! m_pic) return 0;
	OLE_XSIZE_HIMETRIC pWidth;
	m_pic->get_Width(&pWidth);
	HDC tDC = CreateCompatibleDC(0);
	int nWidth	= MulDiv(pWidth,GetDeviceCaps(tDC,LOGPIXELSX), HIMETRIC_INCH);
	DeleteDC(tDC);
	return (DWORD) nWidth;
}


//returns the height of the loaded image
DWORD _PICTURE_::_GetHeight(VOID)
{
	if(! m_pic) return 0;
	OLE_YSIZE_HIMETRIC pHeight;
	m_pic->get_Height(&pHeight);
	HDC tDC = CreateCompatibleDC(0);
	int nHeight	= MulDiv(pHeight,GetDeviceCaps(tDC,LOGPIXELSY), HIMETRIC_INCH);
	DeleteDC(tDC);
	return (DWORD) nHeight;
}

//returns the current DC of the loaded image
HDC _PICTURE_::_GetDC(VOID)
{
	if(! m_pic) return NULL;
	HDC pDC ;
	m_pic->get_CurDC(&pDC);
	return  pDC;
}

//returns the Handle of the loaded image(HBITMAP)
HBITMAP _PICTURE_::_GetHandle( VOID )
{
	if(! m_pic) return NULL;
	HBITMAP hbmp;
	m_pic->get_Handle((OLE_HANDLE *) &hbmp);
	return  hbmp;
}


//Draws the image in a specified DC..with given dimensions
//specify -1 for width and height if you like to draw
//with original dimensions
BOOL _PICTURE_::DrawPicture(HDC hdc,long x,long y,long cx,long cy)
{
	if(! m_pic) return FALSE;
	LONG pHeight,pWidth;
	if(cx == -1) cx = this->_GetWidth();
	if(cy == -1) cy = this->_GetHeight();
	m_pic->get_Width(&pWidth);
    m_pic->get_Height(&pHeight);
	if (m_pic->Render(hdc, x, y, cx, cy,0, pHeight, pWidth,-pHeight, NULL) != S_OK)
		return FALSE ;
	return TRUE;
}

//Cleans up
VOID _PICTURE_::FreePicture(VOID)
{
	if (m_pic)
	{
		m_pic->Release();
		m_pic = NULL;
	}
	if (hGlobal)
	{
		GlobalFree(hGlobal);
		hGlobal = NULL;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -