【问题标题】:Sizing image to text in the image with C# System.Drawing / Drawstring使用 C# System.Drawing / Drawstring 将图像大小调整为图像中的文本
【发布时间】:2010-10-25 03:00:01
【问题描述】:

我想获取一个字符串和一个指定的字体,并在框中创建一个包含该文本的图形图像,其中该框会根据文本调整大小。

我得到了下面的代码,但它在做任何其他事情之前先调整了盒子的大小。 有没有办法调整图像大小以适合文本(我可能需要添加几个像素的小边框)。

另外,如果我想让字符串居中,我不确定为什么需要传递 x,y 坐标。

  //Sample calls to function below 
        renderImage("Hello World", "Arial", 12, "test12.png");
        renderImage("Hello", "Arial", 16, "test16.png");
        renderImage("Peace Out", "Arial", 24, "test24.png");


static void renderImage(string text, string fontName, string filename, int fontsize) 
{

    {
        System.Drawing.Bitmap objBMP = null;
        System.Drawing.Graphics objGraphics = null;
        System.Drawing.Font objFont = null;

        // Create new image - bitmap
        objBMP = new Bitmap(531, 90);

        // Create a graphics object to work with from the BMP
        objGraphics = System.Drawing.Graphics.FromImage(objBMP);

        // Fill the image with background color
        objGraphics.Clear(Color.Yellow);

        // Set anti-aliasing for text to make it better looking
        objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;

        // Configure font to use for text
        objFont = new Font(fontName, fontsize, FontStyle.Bold);

        // Try StringFormat 
        StringFormat objStringFormat = new StringFormat();
        objStringFormat.Alignment = StringAlignment.Center;
        objStringFormat.LineAlignment = StringAlignment.Center;


        // Write out the text
        objGraphics.DrawString(text, objFont, Brushes.Black, 1, 1, objStringFormat);


  // Set the content type and return the image
        objBMP.Save(filename, ImageFormat.Png);

    }

更新:

根据答案让它工作,但有没有更有效的方法:

static void Main(string[] args)
{
    renderImage("Hello World", "Arial", 12, "test12.png");
    renderImage("Hello", "Arial", 16, "test16.png");
    renderImage("Peace Out", "Arial", 24, "test24.png");
}

static void renderImage(string text, string fontName, int fontSize, string filename) 
{

    {
        System.Drawing.Bitmap objBMP = null;
        System.Drawing.Graphics objGraphics = null;
        System.Drawing.Bitmap objBMPTemp = null;
        System.Drawing.Graphics objGraphicsTemp = null;
        System.Drawing.Font objFont = null;

        // Configure font to use for text
        objFont = new Font(fontName, fontSize, FontStyle.Bold);

        // Create new image - bitmap
        SizeF objSizeF = new SizeF();
        objBMPTemp = new Bitmap(100, 100); // some temp dummy size 

        // Create a graphics object to work with from the BMP
        objGraphicsTemp = System.Drawing.Graphics.FromImage(objBMPTemp);

        objSizeF = objGraphicsTemp.MeasureString(text, objFont);
        objBMP = new Bitmap((int)objSizeF.Width, (int)objSizeF.Height); 
        objGraphics = System.Drawing.Graphics.FromImage(objBMP); 



        // Fill the image with background color
        objGraphics.Clear(Color.Yellow);

        // Set anti-aliasing for text to make it better looking
        objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;


        // Try StringFormat 
        //StringFormat objStringFormat = new StringFormat();
        //objStringFormat.Alignment = StringAlignment.Center;
        //objStringFormat.LineAlignment = StringAlignment.Center;
        //objStringFormat.FormatFlags



        // Write out the text
        //objGraphics.DrawRectangle(new Pen(Color.Cyan, 1), 0.0F, 0.0F, objSizeF.Width, objSizeF.Height);
        objGraphics.DrawString(text, objFont, Brushes.Black, 1, 1);
        //objGraphics.DrawString(text, objFont, Brushes.Black, 



        // Set the content type and return the image
        objBMP.Save(filename, ImageFormat.Png);


        // Kill our objects
        objFont.Dispose();
        objGraphics.Dispose();
        objBMP.Dispose();
    }

}

【问题讨论】:

  • obj 作为所有东西的前缀是怎么回事?
  • @Joel - 只是老式的匈牙利符号 - VBScript 时代遗留下来的。但有时我仍然喜欢它,例如提醒我 objFont 是 Font 对象,而不是包含 Font 的字符串值。
  • 匈牙利语肯定应该表明类型?由于一切都是对象(包括字符串!),这似乎是对这个想法的误解。我们确实使用匈牙利符号(异端!异端!)但会写例如bmpTemp、grphTemp、fntBlah 等,前缀指示类型,其余指定特定实例。另请参阅joelonsoftware.com/articles/Wrong.html,了解有关这一切的精彩讨论。

标签: c# graphics image-processing system.drawing string-formatting


【解决方案1】:

您可以使用Graphics.MeasureString 来测量给定字体和大小的字符串的大小。

【讨论】:

  • +1 用过 Graphics.MeasureString,听起来像你想要的
  • 如何解决鸡和蛋的问题。在实例化 Graphics 对象之前我必须有一个图像对象,并且在我实例化 MeasureString 之前我必须有一个图形对象。我需要两个图形对象吗?
  • 让它工作,但它似乎令人费解。请参阅原始问题中的更新。另外,为什么 SizeF 给浮点数和创建图像需要整数?
猜你喜欢
  • 1970-01-01
  • 2012-06-23
  • 1970-01-01
  • 1970-01-01
  • 2019-05-21
  • 2019-04-18
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多