📄 zimage.cpp
字号:
//-----------------------------------------------------------------------------
// ZImage (Implementations) Version 1.00
//
// Routins 4 Showing Picture Files... (.BMP .DIB .EMF .GIF .ICO .JPG .WMF)
//
//=============================================================================
//
//
//--------------------------Example & Usage 4 Dummies--------------------------
//
// U Need 2 Add "ZImage.CPP" and "ZImage.H" Into Your Project (From FileView)
// So U Will Get Control Over The Functions In This Class,
// Then U Can Create a Picture Object And Show It On a Device Context
//
// CZImage m_image; // Create a Picture Object (An Instance Of This Class)
// #include "ZImage.h" // Make Sure U Include This Where U Gonna Create The Object...
//
// Load Picture Data Into The IPicture Interface (.BMP .DIB .EMF .GIF .ICO .JPG .WMF)
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// m_image.Load("Test.JPG"); // Load From a File - Just Load It (Show Later)
// m_image.Load(IDR_TEST, "JPG"); // Load From a Resource - Just Load It (Show Later)
// (U Must Include IDR_TEST In Your Resources Under a Custom Name, 4 Example - "JPG")
//
// When Using DC Object On a *Dialog Based* Application (CPaintDC dc(this);)
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// m_image.UpdateSizeOnDC(&dc); // Get Picture Dimentions In Pixels
// m_image.Show(&dc, CPoint(0,0), CPoint(m_image.m_Width, m_image.m_Height), 0,0);
// m_image.Show(&dc, CRect(0,0,100,100)); // Change Original Dimentions
// m_image.ShowBitmapResource(&dc, IDB_TEST, CPoint(0,0)); // Show Bitmap Resource
//
// OR When Using a Pointer On a "Regular" MFC Application (CDC* pDC)
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// m_image.UpdateSizeOnDC(pDC); // Get Picture Dimentions In Pixels
// m_image.Show(pDC, CPoint(0,0), CPoint(m_image.m_Width, m_image.m_Height), 0,0);
// m_image.Show(pDC, CRect(0,0,100,100)); // Change Original Dimentions
// m_image.ShowBitmapResource(pDC, IDB_TEST, CPoint(0,0)); // Show Bitmap Resource
//
// Show Picture Information
// ~~~~~~~~~~~~~~~~~~~~~~~~
// CString S;
// S.Format("Size = %4d\nWidth = %4d\nHeight = %4d\nWeight = %4d\n",
// m_image.m_Weight, m_image.m_Width, m_image.m_Height, m_image.m_Weight);
// AfxMessageBox(S);
//
//----------------------------Cut The Bullshit Here----------------------------
#include "stdafx.h"
#include "ZImage.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define HIMETRIC_INCH 2540
#define ERROR_TITLE "CZImage Error" // Error Title (Related To This Class)...
//-----------------------------------------------------------------------------
// Does: Constructor - Create a New CZImage Object To Hold Picture Data
// ~~~~
//
//-----------------------------------------------------------------------------
CZImage::CZImage()
//=============================================================================
{
m_IPicture = NULL;
m_Height = 0;
m_Weight = 0;
m_Width = 0;
}
//-----------------------------------------------------------------------------
// Does: Destructor - Free Data And Information From The CZImage Object
// ~~~~
//
//-----------------------------------------------------------------------------
CZImage::~CZImage()
//=============================================================================
{
if(m_IPicture != NULL) FreePictureData(); // Important - Avoid Leaks...
}
//-----------------------------------------------------------------------------
// Does: Free The Allocated Memory That Holdes The IPicture Interface Data
// ~~~~ And Clear Picture Information
//
// Note: This Might Also Be Useful If U Only Need To Show The Picture Once
// ~~~~~ Or If U Copy The Picture To The Device Context, So It Can Still
// Remain On Screen - But IPicture Data Is Not Needed No More
//
//-----------------------------------------------------------------------------
void CZImage::FreePictureData()
//=============================================================================
{
if(m_IPicture != NULL)
{
m_IPicture->Release();
m_IPicture = NULL;
m_Height = 0;
m_Weight = 0;
m_Width = 0;
}
}
//-----------------------------------------------------------------------------
// Does: Open a Resource And Load It Into IPicture (Interface)
// ~~~~ (.BMP .DIB .EMF .GIF .ICO .JPG .WMF)
//
// Note: When Adding a Bitmap Resource It Would Automatically Show On "Bitmap"
// ~~~~ This NOT Good Coz We Need To Load It From a Custom Resource "BMP"
// To Add a Custom Rresource: Import Resource -> Open As -> Custom
// (Both .BMP And .DIB Should Be Found Under "BMP")
//
// InPut: ResourceName - As a UINT Defined (Example: IDR_PICTURE_RESOURCE)
// ~~~~~ ResourceType - Type Name (Example: "JPG")
//
// OutPut: TRUE If Succeeded...
// ~~~~~~
//-----------------------------------------------------------------------------
BOOL CZImage::Load(UINT ResourceName, LPCSTR ResourceType)
//=============================================================================
{
BOOL bResult = FALSE;
HGLOBAL hGlobal = NULL;
HRSRC hSource = NULL;
LPVOID lpVoid = NULL;
int nSize = 0;
if(m_IPicture != NULL) FreePictureData(); // Important - Avoid Leaks...
hSource = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(ResourceName), ResourceType);
if(hSource == NULL)
{
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
MessageBoxEx(hWnd, "FindResource() Failed\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return(FALSE);
}
hGlobal = LoadResource(AfxGetResourceHandle(), hSource);
if(hGlobal == NULL)
{
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
MessageBoxEx(hWnd, "LoadResource() Failed\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return(FALSE);
}
lpVoid = LockResource(hGlobal);
if(lpVoid == NULL)
{
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
MessageBoxEx(hWnd, "LockResource() Failed\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return(FALSE);
}
nSize = (UINT)SizeofResource(AfxGetResourceHandle(), hSource);
if(LoadPictureData((BYTE*)hGlobal, nSize)) bResult = TRUE;
UnlockResource(hGlobal); // 16Bit Windows Needs This
FreeResource(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic Release)
m_Weight = nSize; // Update Picture Size Info...
if(m_IPicture != NULL) // Do Not Try To Read From Memory That Is Not Exist...
{
m_IPicture->get_Height(&m_Height);
m_IPicture->get_Width(&m_Width);
// Calculate Its Size On a "Standard" (96 DPI) Device Context
m_Height = MulDiv(m_Height, 96, HIMETRIC_INCH);
m_Width = MulDiv(m_Width, 96, HIMETRIC_INCH);
}
else // Picture Data Is Not a Known Picture Type
{
m_Height = 0;
m_Width = 0;
bResult = FALSE;
}
return(bResult);
}
//-----------------------------------------------------------------------------
// Does: Open a File And Load It Into IPicture (Interface)
// ~~~~ (.BMP .DIB .EMF .GIF .ICO .JPG .WMF)
//
// InPut: sFilePathName - Path And FileName Target To Save
// ~~~~~
//
// OutPut: TRUE If Succeeded...
// ~~~~~~
//-----------------------------------------------------------------------------
BOOL CZImage::Load(CString sFilePathName)
//=============================================================================
{
BOOL bResult = FALSE;
CFile PictureFile;
CFileException e;
int nSize = 0;
if(m_IPicture != NULL) FreePictureData(); // Important - Avoid Leaks...
if(PictureFile.Open(sFilePathName, CFile::modeRead | CFile::typeBinary, &e))
{
nSize = (int)PictureFile.GetLength(); // !!!类型转换截断,可能丢失数据 [10/18/2003]
BYTE* pBuffer = new BYTE[nSize];
if(PictureFile.Read(pBuffer, nSize) > 0)
{
if(LoadPictureData(pBuffer, nSize)) bResult = TRUE;
}
PictureFile.Close();
delete [] pBuffer;
}
else // Open Failed...
{
//ZYZ-info: 如果路径错误,不必弹出对话框
/*
TCHAR szCause[255];
e.GetErrorMessage(szCause, 255, NULL);
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
MessageBoxEx(hWnd, szCause, ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
*/
bResult = FALSE;
}
m_Weight = nSize; // Update Picture Size Info...
if(m_IPicture != NULL) // Do Not Try To Read From Memory That Is Not Exist...
{
m_IPicture->get_Height(&m_Height);
m_IPicture->get_Width(&m_Width);
// Calculate Its Size On a "Standard" (96 DPI) Device Context
m_Height = MulDiv(m_Height, 96, HIMETRIC_INCH);
m_Width = MulDiv(m_Width, 96, HIMETRIC_INCH);
}
else // Picture Data Is Not a Known Picture Type
{
m_Height = 0;
m_Width = 0;
bResult = FALSE;
}
return(bResult);
}
//-----------------------------------------------------------------------------
// Does: Read The Picture Data From a Source (File / Resource)
// ~~~~ And Load It Into The Current IPicture Object In Use
//
// InPut: Buffer Of Data Source (File / Resource) And Its Size
// ~~~~~
//
// OutPut: Feed The IPicture Object With The Picture Data
// ~~~~~~ (Use Draw Functions To Show It On a Device Context)
// TRUE If Succeeded...
//-----------------------------------------------------------------------------
BOOL CZImage::LoadPictureData(BYTE *pBuffer, int nSize)
//=============================================================================
{
BOOL bResult = FALSE;
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);
if(hGlobal == NULL)
{
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
//ZYZ-info: MessageBoxEx(hWnd, "Can not allocate enough memory\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
TRACE("zyz==Can not allocate enough memory!==\n");
return(FALSE);
}
void* pData = GlobalLock(hGlobal);
memcpy(pData, pBuffer, 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_IPicture)) == E_NOINTERFACE)
{
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
MessageBoxEx(hWnd, "IPicture interface is not supported\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return(FALSE);
}
else // S_OK
{
pStream->Release();
pStream = NULL;
bResult = TRUE;
}
}
FreeResource(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic Release)
return(bResult);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -