15,978
社区成员
发帖
与我相关
我的任务
分享
BOOL CGdiEffect::LoadResImage(int nResID,Image * &lpImage, LPCTSTR lpType)
{
// lpImage = NULL;
HRSRC hRsrc = ::FindResource (AfxGetResourceHandle(),MAKEINTRESOURCE(nResID),lpType);
if (!hRsrc)
return FALSE;
// load resource into memory ---------------------------------
DWORD len = SizeofResource(AfxGetResourceHandle(), hRsrc);
BYTE* lpRsrc = (BYTE*)LoadResource(AfxGetResourceHandle(), hRsrc);
if (!lpRsrc)
return FALSE;
// Allocate global memory on which to create stream ----------
HGLOBAL m_hMem = GlobalAlloc(GMEM_MOVEABLE, len);
BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
memcpy(pmem,lpRsrc,len);
IStream* pstm;
HRESULT ht = CreateStreamOnHGlobal(m_hMem,TRUE,&pstm);
if (ht != S_OK)
{
GlobalUnlock(m_hMem);
pstm->Release();
return FALSE ;
}
// load from stream -------------------------------------------
lpImage=Gdiplus::Image::FromStream(pstm);
// free/release stuff -----------------------------------------
GlobalUnlock(m_hMem);
pstm->Release();
FreeResource(lpRsrc);
return TRUE ;
}
//---------测试代码--------------------------------------
ULONG_PTR token;
GdiplusStartupInput input ;
GdiplusStartup(&token, &input, NULL);
CGdiEffect ef ;
Image *pImg = NULL;
ef.LoadResImage(IDB_BITMAP1, pImg, RT_BITMAP);//这个失败了
//ef.LoadResImage(IDB_PNG1, pImg,TEXT("png"));//这个成功了
CWindowDC dc(this);
Graphics g(dc.GetSafeHdc());
//Image img(TEXT("f:\\ScrollBarButtons.bmp"));
g.DrawImage(pImg, 0, 0);
//g.DrawImage(&img, 0, 0);
if(pImg)
delete pImg;
delete []buffer;
GdiplusShutdown(token) ;
//=======================================================================
// 函数原型: BOOL CSYSGlobal::BMP2JPEG(LPCTSTR lpszBMPFileName, LPCTSTR lpszJPEGFileName , long lQuality /*= 100*/ )
// 功能描述: BMP文件转换成JPEG文件
// 参数说明: 名称 类型 说明
// lpszBMPFileName LPCTSTR 要转换的BMP文件名( 已存在文件 )
// lpszJPEGFileName LPCTSTR JPEG文件名( 根据BMP转换 新生成文件 )
// lQuality long 图片质量 ( 默认100% )
// 返 回 值:
// 依 赖 于: GDI+, ATL字符转换( 用 A2W() 转换ASCII -> Unicode )
// 具体 做法如下:
// 1. 加入GDI+支持: 在StdAfx.h的 "#endif // _AFX_NO_AFXCMN_SUPPORT" 前加入如下代码:
// #include <gdiplus.h>
// #include <GdiPlusEnums.h>
// using namespace Gdiplus;
// #pragma comment( lib, "gdiplus.lib" )
//
// //加入ATL字符转换支持: 在StdAfx.h中加入
// #include <atlconv.h>
//-----------------------------------------------------------------------
// 引 用 表: 无
// 更 新 表: 无
// 被引用于: 全局
// 创建日期: 2007年12月21日 16时02分57秒
// 修改日期:
// 修改说明:
//=======================================================================
BOOL CSYSGlobal::BMP2JPEG(LPCTSTR lpszBMPFileName, LPCTSTR lpszJPEGFileName, long lQuality /*= 100*/ )
{
USES_CONVERSION; //调用 A2W() 等宏时,必须先执行该行!!!
CLSID codecClsid;
EncoderParameters encoderParameters;
long quality;
Status stat;
GdiplusStartupInput gdiplusStartupInput;
ULONG gdiplusToken;
BOOL bRet = TRUE;
WCHAR wszcBmpFileName[ 512 ];
WCHAR wszJpgFileName[ 512 ];
WCHAR *lpwcBmpFileName = NULL;
WCHAR *lpwcJpgFileName = NULL;
#ifdef UNICODE
wcscpy( wszcBmpFileName, lpszBMPFileName );
wcscpy( wszJpgFileName, lpszJPEGFileName );
#else
lpwcBmpFileName = A2W( lpszBMPFileName );
lpwcJpgFileName = A2W( lpszJPEGFileName );
wcscpy( wszcBmpFileName, lpwcBmpFileName );
wcscpy( wszJpgFileName, lpwcJpgFileName );
#endif
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
{
// Get an image from the disk.
Image image( wszcBmpFileName );
// Get the CLSID of the JPEG codec.
CSYSGlobal::GetCodecClsid( L"image/jpeg", &codecClsid );
// Before we call Image::Save, we must initialize an
// EncoderParameters object. The EncoderParameters object
// has an array of EncoderParameter objects. In this
// case, there is only one EncoderParameter object in the array.
// The one EncoderParameter object has an array of values.
// In this case, there is only one value ( of type LONG )
// in the array. We will set this value to 0, 50, and 100.
encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;
// Save the image as a JPEG with quality level 0.
quality = lQuality;
encoderParameters.Parameter[0].Value = &quality;
stat = image.Save( wszJpgFileName, &codecClsid, &encoderParameters);
if( stat == Ok )
{
//AfxMessageBox( _T( "保存JPEG文件成功" ) );
bRet = TRUE;
}
else
{
AfxMessageBox( _T( "保存JPEG文件失败!" ) );
bRet = FALSE;
}
}
GdiplusShutdown(gdiplusToken);
return bRet;
}