*************************************************************************************************
1、圖檔的顯示
// 下面是GDI+要使用的幾個頭檔案
#ifndef ULONG_PTR
#define ULONG_PTR unsigned long*
#endif
#include "Gdiplus.h"
#include <afxole.h>
#include <Atlbase.h>
Gdiplus::GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_gdiplusToken;
// 初始化GDI+庫
void InitGuiPlus()
{
Gdiplus::GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
}
// 關閉GDI+庫
void CleanupGuiPlus()
{
Gdiplus::GdiplusShutdown(m_gdiplusToken);
}
Gdiplus::Bitmap * m_pBmp;
// 加載圖檔
Gdiplus::Bitmap * LoadPic(LPCSTR szPicUrl)
{
Gdiplus::Bitmap * pBmp = NULL;
CFileFind fFind;
if (!fFind.FindFile(szPicUrl))
{
return pBmp;
}
USES_CONVERSION;
// 打開剛才浏覽的圖檔
pBmp = new Gdiplus::Bitmap(A2W(szPicUrl));
return pBmp;
}
// 解除安裝圖檔
void UnLoadPic()
{
if (m_pBmp != NULL)
{
delete m_pBmp;
m_pBmp = NULL;
}
}
// 顯示圖檔 (在WM_PAINT的響應處理函數對其調用)
void ShowPic(CDC *pDC, Gdiplus::Bitmap *pbmpSrc, int nSrcWidth, int nSrcHeight)
{
ASSERT(pbmpSrc != NULL);
Gdiplus::Graphics graphics(pDC->m_hDC);
graphics.DrawImage(pbmpSrc, 0, 0, nSrcWidth, nSrcHeight);
}
*************************************************************************************************
2、利用GDI+對圖檔縮放後儲存(頭檔案和初始化代碼同1)
// 擷取圖檔的編碼資訊
BOOL GetImageEncoderCLSID( CLSID& clsid, const wchar_t* format)
{
UINT num=0, size=0;
Gdiplus::GetImageEncodersSize(&num, &size);
Gdiplus::ImageCodecInfo* pImageCodecInfo = (Gdiplus::ImageCodecInfo*)malloc(size);
if(pImageCodecInfo == NULL)
{
return FALSE;
}
Gdiplus::GetImageEncoders(num, size, pImageCodecInfo );
for( UINT i = 0; i<num; ++i )
{
if( wcscmp(pImageCodecInfo[i].MimeType, format) == 0 )
{
clsid = pImageCodecInfo[i].Clsid;
free(pImageCodecInfo);
return TRUE;
}
}
free(pImageCodecInfo);
pImageCodecInfo = NULL;
return FALSE;
}
// 按尺寸儲存圖檔
BOOL SavePicture(LPCSTR pszFileUrl, Gdiplus::Image *pSrcBmp, int nSavedWidth, int nSavedHeight)
{
ASSERT(pSrcBmp != NULL);
// 建立預儲存的圖檔
Gdiplus::Bitmap bmpSaved(nSavedWidth, nSavedHeight);
Gdiplus::Graphics * pGraphics = Gdiplus::Graphics::FromImage(&bmpSaved);
// 将pSrcBmp畫到pGraphics中
pGraphics->DrawImage(pSrcBmp, 0, 0, nSavedWidth, nSavedHeight);
if (pGraphics == NULL)
{
return FALSE;
}
CLSID clImageClsid;
// 擷取圖檔的編碼資訊
if (GetImageEncoderCLSID(clImageClsid, L"image/jpeg"))
{
USES_CONVERSION;
// 儲存入片
bmpSaved.Save(A2W(pszFileUrl), &clImageClsid, NULL);
delete pGraphics;
pGraphics = NULL;
return TRUE;
}
delete pGraphics;
pGraphics = NULL;
return FALSE;
}