GDI+一括生成画像

2150 ワード

GDI+バッチを使用してpngピクチャを生成し、各ピクチャに日付文字列を描画します.
初期読み込みsample.pngはテンプレートです.
#include <Windows.h>
#include <time.h>
#include <GdiPlus.h>
using namespace Gdiplus;
#pragma comment(lib,"gdiplus.lib")
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
	UINT  num = 0;          // number of image encoders
	UINT  size = 0;         // size of the image encoder array in bytes

	ImageCodecInfo* pImageCodecInfo = NULL;

	GetImageEncodersSize(&num, &size);
	if(size == 0)
		return -1;  // Failure

	pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
	if(pImageCodecInfo == NULL)
		return -1;  // Failure

	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;  // Success
		}    
	}

	free(pImageCodecInfo);
	return -1;  // Failure
}
int main()
{
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR           gdiplusToken;

	

	// Initialize GDI+.
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	int pic_width = 1920;
	int pic_height = 1080;

	Image sample(TEXT("sample.png"));
	int smp_height = sample.GetHeight();
	int smp_width = sample.GetWidth();

	FontFamily   fontFamily(L"Corbel");
	Font         font(&fontFamily, 112, FontStyleBold, UnitPoint);
	RectF        rectF(.0f, .0f, sample.GetWidth(),sample.GetHeight());
	SolidBrush   solidBrush(Color(200, 0, 0, 0));
	WCHAR buffer[40];
	int y = 2012, m = 2, d = 26, t = 0;


	Graphics g(&sample);
	g.SetTextRenderingHint(TextRenderingHint::TextRenderingHintAntiAlias);
	CLSID pngClsid;
	GetEncoderClsid(L"image/png", &pngClsid);
	Color TransColor(0, 0, 0, 211);
	while(t<360)
	{
		wsprintf(buffer,TEXT("%4d-%02d-%02d"),y,m,d);
		g.DrawString(buffer, -1, &font, rectF, NULL, &solidBrush);
		wcscat(buffer,TEXT(".png"));
		sample.Save(buffer,&pngClsid);
		g.Clear(TransColor);
		d++;
		t++;
		if (d==32)
		{
			d=1;
			m++;
			if (m==13)
			{
				y++;
				m=1;
			}
		}
	}


}