I am seeking to create an image from user input text. ultimately, it would be used to send to twitter along with a status update.
I cannot control the length of the user text - it will be variable.
Originally, I was able to draw a png from the text using System.Drawing DrawString method, but the output is a single line png with a potentially enormous width - this is obviously not a viable solution.
I had an idea to split the string into multiple lengths, and draw them individually in the png, this is working, but doesn't feel elegant as it wraps mid-word, and I can't guarantee it will work with long strings (if 25% of the sting length is still too wide to fit in the png). All in all it is better than my original solution, but still doesn't feel viable long term.
This is the code I'm using for creating the png:
string quoteText = someString; //my user's input text
int strLength = quoteText.Length;
int i25 = strLength / 4;
string st25 = quoteText.Substring(0, i25);
string st50 = quoteText.Substring(i25 + 1, i25);
string st75 = quoteText.Substring((i25 * 2) + 1, i25);
string st100 = quoteText.Substring((i25 * 3) + 1);
Bitmap objBmpImage = new Bitmap(500, 600);
int intWidth = 600;
int intHeight = 700;
// Create the Font object for the image text drawing.
Font objFont = new Font("Roboto Condensed", 14, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
Font brandFont = new Font("IM Fell Double Pica", 14, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Pixel);
// Create a graphics object to measure the text's width and height.
Graphics objGraphics = Graphics.FromImage(objBmpImage);
// This is where the bitmap size is determined.
intWidth = 500;
intHeight = 600;
// Create the bmpImage again with the correct size for the text and font.
objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
// Add the colors to the new bitmap.
objGraphics = Graphics.FromImage(objBmpImage);
// Set Background color
objGraphics.Clear(Color.White);
objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
objGraphics.DrawString(st25, objFont, new SolidBrush(Color.FromArgb(0, 126, 58)), 0, 0);
objGraphics.DrawString(st50, objFont, new SolidBrush(Color.FromArgb(0, 126, 58)), 0, 30);
objGraphics.DrawString(st75, objFont, new SolidBrush(Color.FromArgb(0, 126, 58)), 0, 60);
objGraphics.DrawString(st100, objFont, new SolidBrush(Color.FromArgb(0, 126, 58)), 0, 90);
objGraphics.DrawString("some site branding", brandFont, new SolidBrush(Color.FromArgb(0, 126, 58)), 100, 120);
objGraphics.Flush();
//create the path for the imge to be saved
string imagePath = System.Web.HttpContext.Current.Server.MapPath("~/MyServerPath/" + sURL + ".png");
objBmpImage.Save(imagePath);
//return the imagePath
return imagePath;
Can anyone suggest a way to create the png height and width more dynamically, so that is appropriate to the string length, and wraps elegantly (at word breaks)?
I've done multiple searches but can't find anything that quite does what I'm after.