簡體   English   中英

使用C#System.Drawing / Drawstring將圖像調整為圖像中的文本

[英]Sizing image to text in the image with C# System.Drawing / Drawstring

我想要一個字符串和一個指定的字體,並創建一個在框中包含該文本的圖形圖像,其中該框將調整為文本大小。

我在下面的代碼中正常工作,但是在執行其他任何操作之前先確定了大小。 是否有某種方法可以調整圖像大小以適合文本(我可能需要添加幾個像素的小邊框)。

另外,我不確定如果要將字符串居中,為什么需要傳遞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來測量給定字體和大小的字符串的大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM