#include <iostream> int main(){
 /home /  picgen  /

	Free text to picture generator in to any useful format.
	You can use any picture format registered in your system.
	JPEG, BMP, GIF, TIFF, PNG
	Easy way to convert text into image.
	Pick font, font style, font size, 
	text angle, color, background color, encoder and output file name...
	Requirements: Win. XP+, GDI+ .
   
Full source code
Download executable

	Command line args :
	TextToPic.exe [text];[color];[background color];[font Name];[font Size];[font Style];[rotate angle];[quality];[image Name];[image Format]

	TextToPic.exe "1234567890\nHELLO WORLD!\n;-16777216;-1;Arial;24;1;0;65;HelloWorld;gif"

__________________________________________________________________________

#pragma comment(lib, "GdiPlus.lib")


typedef struct ImgStruct
{
	char text[1024];
	int color;
	int background;
	char fontName[MAX_PATH];
	int fontSize;
	int fontStyle;
	int angle;
	int quality;
	char imageName[MAX_PATH];
	char imageFormat[MAX_PATH];
};
//
int GetEncoderClsid(const WCHAR* format,CLSID* pClsid)
{  
	UINT num = 0;          
	UINT size = 0;      

	ImageCodecInfo* pImageCodecInfo;
	GetImageEncodersSize(&num, &size);
	if(size == 0) return -1;  

	pImageCodecInfo = (ImageCodecInfo*) LocalAlloc(LPTR, 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;
			LocalFree(pImageCodecInfo);
			return j;  
		}
	}
	LocalFree(pImageCodecInfo);
	return -1; 
}

//
int CreateImage(ImgStruct myImage)
{
	int ret;
	int maxLn =1;
	int maxCnt =1;
	unsigned int pos1 = 0;
	unsigned int pos2 = 0;
	int simplestring=1;

	string text = myImage.text;
	while(1)
	{
		pos2 = text.find("\n",pos1+1);
		if (pos2!=string::npos)
		{
			maxCnt++;
			string tmp = text.substr(pos1,pos2-pos1);
			if (tmp.length() > maxLn) maxLn = tmp.length()-2;
			pos1=pos2;
			simplestring=0;
		}
		else break;

	}

	if(simplestring)
	{
		maxLn = text.length();
		maxCnt= 2;
	}
	int angOffset ;
	if(myImage.angle <0)
	{
		angOffset = myImage.angle*15*(-1);
	}
	else
	{
		angOffset = myImage.angle*15;
	}

	int offsetOrigin = myImage.angle*15;

	double fontOffset=0.89;
	if(myImage.fontName == "Verdana") fontOffset =0.99;

	int Wi = maxLn * (myImage.fontSize*fontOffset)+angOffset;
	int He = (maxCnt * (myImage.fontSize*1.55))+angOffset;

	////////////////////

	HDC scrdc, memdc;
	HBITMAP membit;

	scrdc  = GetDC(0);

	memdc  = CreateCompatibleDC(scrdc);
	membit = CreateCompatibleBitmap(scrdc, Wi, He);
	SelectObject(memdc, membit);

	Bitmap * bmp ;
	bmp = Bitmap::FromHBITMAP(membit,  NULL);

	Image  * img = bmp->GetThumbnailImage(Wi,He);

	Gdiplus::Graphics graphics(img);

	Gdiplus::RectF rf;
	rf.X=0;
	rf.Y=0;
	rf.Width=Wi;
	rf.Height=He;


	Gdiplus::Color backgroundColor = Gdiplus::Color((Gdiplus::ARGB)myImage.background);
	Gdiplus::SolidBrush backgroundBrush(backgroundColor);

	graphics.FillRectangle(&backgroundBrush, rf);
	graphics.RotateTransform (myImage.angle);

	graphics.SetTextContrast(120);
	//graphics.SetCompositingQuality(Gdiplus::CompositingQualityHighQuality);
	graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQuality);
	graphics.SetSmoothingMode(Gdiplus::SmoothingModeHighSpeed);
	graphics.SetTextRenderingHint(Gdiplus::TextRenderingHintAntiAliasGridFit);

	Gdiplus::StringFormat format;
	//format.SetAlignment(Gdiplus::StringAlignmentCenter);
	//format.SetLineAlignment(Gdiplus::StringAlignmentNear);

	WCHAR wFont[MAX_PATH];
	MultiByteToWideChar(CP_ACP, 0, myImage.fontName,-1, wFont, MAX_PATH);

	Gdiplus::Font myFont(wFont, myImage.fontSize, myImage.fontStyle);
	int itmp=0;
	if(myImage.angle<0)
	{
		itmp = 5 - offsetOrigin;
	}
	else
	{
		itmp = 5;
	}

	Gdiplus::PointF origin(5,itmp);
	Gdiplus::Color fontColor = Gdiplus::Color((Gdiplus::ARGB)myImage.color); 
	Gdiplus::SolidBrush brush(fontColor);

	WCHAR wStr[MAX_PATH*100];
	MultiByteToWideChar(CP_ACP, 0, myImage.text, -1, wStr, MAX_PATH*100);


	graphics.DrawString(wStr, -1, &myFont, origin, &brush);
	/////////////////////////////

	CLSID             imgClsid;
	EncoderParameters encoderParams;

	encoderParams.Count							= 1;
	encoderParams.Parameter[0].Guid				= EncoderQuality;
	encoderParams.Parameter[0].Type				= EncoderParameterValueTypeLong;
	encoderParams.Parameter[0].NumberOfValues	= 1;

	// normal quality = 65;
	encoderParams.Parameter[0].Value			= &myImage.quality;

	//image/bmp 
	//image/jpeg 
	//image/gif 
	//image/tiff 
	//image/png

	wchar_t wFormat[MAX_PATH]=L"image/";
	wchar_t wTmp[MAX_PATH];
	MultiByteToWideChar(CP_ACP, 0, myImage.imageFormat, -1, wTmp, MAX_PATH);
	wcscat(wFormat,wTmp);
	GetEncoderClsid(wFormat, &imgClsid);

	lstrcatA(myImage.imageName,".");
	lstrcatA(myImage.imageName, myImage.imageFormat);

	WCHAR wf[MAX_PATH];
	MultiByteToWideChar(CP_ACP, 0, myImage.imageName, -1, wf, MAX_PATH);

	if(img->Save(wf,&imgClsid,&encoderParams))
	{
		DeleteObject(membit);
		ReleaseDC(0, scrdc);
		ReleaseDC(0, memdc);

	}

	return 1;

}

Copyright © 2008 | Contacts