Hello all. I am working on a small hobby project which is supposed to turn text into handwriting. By that I mean it will take any text you enter and add flaws to it, in addition to small flaws it will also vary spacing and whitespace between the words.
I am having a bit of trouble already.
I figure since I need to vary spacing and whitespace, I should iterate through the inputted string and carry out modifications on each individual word.
Here is my code:
The trouble I am having is adding the correct height to each line after it reaches the length of the image. ( When the length of the characters/string reaches the length of the image, I want to move down to the next line) Otherwise the text will run off the edge of the image and not be visible.
In addition to that, I am having trouble getting the spacing between words to be correct, and im sort of lost on why that is. Any help would be appreciated! Thank you.
I am having a bit of trouble already.
I figure since I need to vary spacing and whitespace, I should iterate through the inputted string and carry out modifications on each individual word.
Here is my code:
C#:
public Image DrawText(String text, Font font, Color textColor, Color backColor)
{
//first, create a dummy bitmap just to get a graphics object
Image img = new Bitmap(1, 1);
Graphics drawing = Graphics.FromImage(img);
//measure the string to see how big the image needs to be
SizeF textSize = drawing.MeasureString(text, font);
//free up the dummy image and old graphics object
img.Dispose();
drawing.Dispose();
//create a new image of the right size, currently adding width and height bc the method is not finished.
img = new Bitmap((int)textSize.Width + 600, (int)textSize.Height + 600);
drawing = Graphics.FromImage(img);
//paint the background
drawing.Clear(backColor);
//create a brush for the text
Brush textBrush = new SolidBrush(textColor);
//set render mode
drawing.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
drawing.InterpolationMode = InterpolationMode.HighQualityBicubic;
drawing.PixelOffsetMode = PixelOffsetMode.HighQuality;
drawing.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
StringFormat format = new StringFormat();
// splitting the input string delimited by spaces
string[] xx1 = text.Split(' ');
// iterating through each word, and placing each word on to the image in sentences, paragraphs, etc.
float totalwidth = 0;
float totalheight = 0;
foreach (string bee in xx1)
{
totalwidth = totalwidth + drawing.MeasureString(" " + bee + " ", font).Width;
drawing.DrawString(bee, font, textBrush, totalwidth, totalheight);
if ( (int)totalwidth > (int)img.Width )
{
totalheight = totalheight + drawing.MeasureString(bee, font).Height;
}
}
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
return img;
}
The trouble I am having is adding the correct height to each line after it reaches the length of the image. ( When the length of the characters/string reaches the length of the image, I want to move down to the next line) Otherwise the text will run off the edge of the image and not be visible.
In addition to that, I am having trouble getting the spacing between words to be correct, and im sort of lost on why that is. Any help would be appreciated! Thank you.