c# - 使用 C# System.Drawing/Drawstring 将图像大小调整为图像中的文本

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

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

我让下面的代码正常工作,但它在执行其他操作之前调整了框的大小。 有没有办法调整图像大小以适应文本(我可能需要添加几个像素的小边框)。

此外,我不太确定如果我希望字符串居中,为什么需要传递 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();
    }

}

最佳答案

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

关于c# - 使用 C# System.Drawing/Drawstring 将图像大小调整为图像中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3998646/

相关文章:

python - 如何找到图像中最常见的像素值?

java - Jpeg DCT 和 IDCT 计算不正确

c# - 在 C# 中遍历 XML 树

c# - 返回的数据类型因表中的数据而异

java - 根据 X 和 Y 坐标绘制线条

c# - 在图片框中平移图像后,如何仅保存可见图像

c# - 使用虚假数据进行 wcf 服务测试

c# - 需要确定屏幕分辨率以在 ASP.NET 网页上显示必要的项目

c - 在 OpenGL 中使用两个纹理

matlab - 使用 MATLAB 进行图像去模糊