#include "atlimage.h"
#include "gdiplus.h"
#pragma comment( lib, "gdiplus.lib" )
using namespace Gdiplus;
using namespace std;
BOOL CVS2010Image::SavePicWithChar(const CHAR* cCharacter, const char* cFilePath,const char* imageData,int imageSize)
{
BOOL bRet = FALSE;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, 0x00400000);
BYTE* pMem = (BYTE*)GlobalLock(hMem);
memcpy(pMem, imageData, imageSize);
GlobalUnlock(hMem);
IStream* pIStm;
if (CreateStreamOnHGlobal(hMem, TRUE, &pIStm) == S_OK)
{
ULONG_PTR m_gdiplusToken = 0;
GdiplusStartupInput gdiplusStartupInput = 0;
GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
//括号不可以去掉
{
Image image(pIStm);
Graphics imageGraphics(&image); //通过Image对象创建一个绘图句柄,使用这个句柄对图片进行操作
imageGraphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
WCHAR *string = char2wchar(cCharacter);
//WCHAR *string = (WCHAR*)cCharacter;
FontFamily fontFamily(L"楷体");
Gdiplus::Font myFont(&fontFamily, 40, FontStyleRegular, UnitPoint); //第二个是字体大小
SolidBrush blackBrush(Color(255, 0, 0, 0)); //半透明+文字RGB颜色
PointF school_site((REAL)10, (REAL)10);//文字放置的像素坐标
StringFormat format;
format.SetAlignment(StringAlignmentNear); //文本排列方式,即在对应位置居中、靠左、靠右
//wcscpy_s(string, CT2CW(str)); //如果使用MFC中的CString,需要这样转换成WCHAR
imageGraphics.DrawString(string, wcslen(string), &myFont, school_site, &format, &blackBrush );//把string绘制到图上
CLSID pngClsid;
GetEncoderClsidOwn( L"image/jpeg", &pngClsid);
WCHAR *wFilepath = char2wchar(cFilePath);
Status s = image.Save(wFilepath, &pngClsid, NULL );//保存添加了汉字的图像
if(string)delete(string);
if(wFilepath)delete(wFilepath);
}
GdiplusShutdown(m_gdiplusToken);
}
return bRet;
}
BOOL CVS2010Image::GetEncoderClsidOwn(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0;
UINT size = 0;
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1;
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1;
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
//free(pImageCodecInfo);
return j;
}
}
free(pImageCodecInfo);
return -1;
}