When I use DrawString, the length of the string is dynamic, I have no idea how long the string could be. If the string gets too long, and is truncated, is there any way that I can tell that this happens so I can inject a new line?
following code will dynamically create image with fixed 300px width and appropriate height to fit text.
Run sample code
Test Page
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>How do I calculate dynamic image height to fit text.</title>
</head>
<body>
<form id="form1" action="DynamicImageHeightToFitText.ashx" >
<div>
<h2>Enter text and click "submit" to view image.</h2>
<textarea id="longText" name="longText" rows="10"
style="width:400px;" >
In the attitude of silence the soul finds the path in a clearer
light, and what is elusive and deceptive resolves itself into
crystal clearness. Our life is a long and arduous quest after
Truth.
Gandhi.
</textarea>
<p>
<input type="submit"value="submit" />
</p>
</div>
</form>
</body>
</html>
Image Generator
<%@ WebHandler Language="C#" Class="DynamicImageHeightToFitText" %>
using System;
using System.Web;
using System.Drawing;
using System.Web.SessionState;
public class DynamicImageHeightToFitText :
IHttpHandler,IRequiresSessionState
{
public void ProcessRequest (HttpContext context) {
string longText = "test for long string on Image";
int imageWidth = 300;
int margin = 10;
Font font = new Font(FontFamily.GenericMonospace, 10);
StringFormat stringFormat = new StringFormat();
if (!string.IsNullOrEmpty(context.Request.Params["longText"]))
{
longText = context.Request.Params["longText"];
}
int stringHeight = MeasureDisplayStringHeight(longText,
font, imageWidth - 2 * margin, stringFormat);
int imageHeight = stringHeight + margin * 2;
// create image and graphics object.
Bitmap bitmap = new Bitmap(imageWidth, imageHeight);
Graphics g = Graphics.FromImage(bitmap);
// put white background.
g.Clear(Color.White);
// set AntiAlias for smooth curves.
g.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawString(longText, font, Brushes.Black,
new RectangleF(margin, margin, imageWidth - 2 *
margin, stringHeight),stringFormat);
g.DrawRectangle(Pens.Black, new Rectangle(0, 0,
imageWidth - 1, imageHeight - 1));
g.Dispose();
context.Response.ContentType = "image/pjpeg";
bitmap.Save(context.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
}
public static int MeasureDisplayStringHeight(string text, Font font,
int maxWidth, System.Drawing.StringFormat format)
{
System.Drawing.Graphics g =
Graphics.FromImage(new Bitmap(maxWidth, 1000));
System.Drawing.RectangleF rect =
new System.Drawing.RectangleF(0, 0, maxWidth, 1000);
System.Drawing.CharacterRange[] ranges =
{ new System.Drawing.CharacterRange(0, text.Length) };
System.Drawing.Region[] regions = new System.Drawing.Region[1];
format.SetMeasurableCharacterRanges(ranges);
regions = g.MeasureCharacterRanges(text, font, rect, format);
rect = regions[0].GetBounds(g);
return (int)(rect.Bottom + 1.0f);
}
public bool IsReusable {
get {
returnfalse;
}
}
}