如何调整以编程方式创建的位图的大小来匹配在其上绘制的文本?

我有以下.ashx页面,它接受一些查询字符串参数并返回一个位图,上面写有指定的文本。 我遇到的问题是,我目前只是手动设置位图的初始大小为100 X 100,而我真正想要的是让位图足够大以包含写入它的所有文本。 我怎样才能做到这一点?

public void ProcessRequest (HttpContext context) { context.Response.ContentType = "image/png"; string text = context.Request.QueryString["Text"]; //set FontName string fontName; if (context.Request.QueryString["FontName"] != null) { fontName = context.Request.QueryString["FontName"]; } else { fontName = "Arial"; } //Set FontSize int fontEms; if (context.Request.QueryString["FontSize"] != null) { string fontSize = context.Request.QueryString["FontSize"]; fontEms = Int32.Parse(fontSize); } else { fontEms = 12; } //Set Font Color System.Drawing.Color color; if (context.Request.QueryString["FontColor"] != null) { string fontColor = context.Request.QueryString["FontColor"]; color = System.Drawing.ColorTranslator.FromHtml(fontColor); context.Response.Write(color.ToString()); } else { color = System.Drawing.Color.Red; } using (System.Drawing.Text.PrivateFontCollection fnts = new System.Drawing.Text.PrivateFontCollection()) using (System.Drawing.FontFamily fntfam = new System.Drawing.FontFamily(fontName)) using (System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(color)) using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(100, 100)) { using (System.Drawing.Font fnt = new System.Drawing.Font(fntfam, fontEms)) { fnts.AddFontFile(System.IO.Path.Combine(@"C:\Development\Fonts\", fontName)); System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp); graph.DrawString(text, fnt, brush, new System.Drawing.Point(0, 0)); string imgPath = System.IO.Path.Combine(@"C:\Development\MyPath\Images\Text", System.IO.Path.GetRandomFileName()); bmp.Save(imgPath); context.Response.WriteFile(imgPath); } } } 

更新:我最终创建了比我需要的更大的位图,从该位图获取图形并使用MeasureString方法获取我要写入的文本的大小,然后将位图的该部分裁剪为新的位图所以。

  using (System.Drawing.Text.PrivateFontCollection fnts = new System.Drawing.Text.PrivateFontCollection()) using (System.Drawing.FontFamily fntfam = new System.Drawing.FontFamily(fontName)) using (System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(color)) using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(500, 500)) { using (System.Drawing.Font fnt = new System.Drawing.Font(fntfam, fontEms)) { fnts.AddFontFile(System.IO.Path.Combine(@"C:\Development\Fonts\", fontName)); System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp); System.Drawing.SizeF bmpSize = graph.MeasureString(text, fnt); System.Drawing.Rectangle rect = new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), bmpSize.ToSize()); System.Drawing.Bitmap croppedBmp = bmp.Clone(rect, bmp.PixelFormat); graph = System.Drawing.Graphics.FromImage(croppedBmp); graph.DrawString(text, fnt, brush, new System.Drawing.Point(0, 0)); string imgPath = System.IO.Path.Combine(@"C:\Development\MyPath\Images\Text", System.IO.Path.GetRandomFileName()); croppedBmp.Save(imgPath); context.Response.WriteFile(imgPath); } } 

我猜有一个System.Drawing.Graphics.MeasureString方法,它将使用指定的字体返回指定文本的大小。

然后,您可以相应地调整图像大小,或相应地调整文本大小以使它们完全匹配。

也许关注这个问题和答案会以某种方式指导你,因为问题是关于调整文本大小以使其适合某个区域。 你似乎希望做出类似的东西,所以算法可能会激发你的灵感。