Wednesday, January 2, 2008

convert text to bitmap...

using System.Drawing;
using System.Drawing.Imaging;
string FontName = "Courier New";
Color FontColor = Color.Black;
Color BackColor = Color.White;
int FontSize = 14;
int Width = 25;
//fontsize times 1.5 is just high enough
//to encase the text without spacer above or below.
float h = (FontSize * 1.5f);
int Height = int.Parse(h.ToString());
//file to save as
string FileName = "MyImage";
//fore color
SolidBrush objBrushForeColor = new SolidBrush(FontColor);
//back color
SolidBrush objBrushBackColor = new SolidBrush(BackColor);
//the point to start the text. I chose horizontal value of zero
//vertical starts at 2 pixels down.
Point objPoint = new Point(0,2);
//font object
Font objFont = new Font(FontName, FontSize);
//bitmap object
Bitmap objBitmap = new Bitmap(Width, Height);
//graphics object
Graphics objGraphics = System.Drawing.Graphics.FromImage(objBitmap);
//the following line is not needed, but is shown
//in the vb example.. dont know why.
//Color objColor;
//draw a white rectangle
objGraphics.FillRectangle(objBrushBackColor, 0, 0, Width, (FontSize*1.5f));
//draw the text
objGraphics.DrawString(character.ToString(), objFont, objBrushForeColor, objPoint);
//save the bitmap.
objBitmap.Save(FileName + ".bmp", ImageFormat.Bmp);

No comments: