在C#中创建自动调整大小的图像

本文关键字:图像 调整 创建 | 更新日期: 2023-09-27 18:20:41

我正在使用以下代码创建一个"自动调整大小"的图像:

    String id="foo bar";
    Color BackColor = Color.White;
    String FontName = "Times New Roman";
    int FontSize = 25;
    int Height = 50;
    int Width = 200;
    Bitmap bitmap = new Bitmap(Width,Height);
    Graphics graphics = Graphics.FromImage(bitmap);
    Color color = Color.Gray; ;
    Font font = new Font(FontName, FontSize);
    SolidBrush BrushBackColor = new SolidBrush(BackColor);
    Pen BorderPen = new Pen(color);
    Rectangle displayRectangle = new Rectangle(new Point(0, 0), new Size());
    graphics.FillRectangle(BrushBackColor, displayRectangle);
    graphics.DrawRectangle(BorderPen, displayRectangle);
    graphics.DrawString(id, font, Brushes.Black, 0, 0);
    FileContentResult result;
    using (var memStream = new System.IO.MemoryStream())
    {
        bitmap.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        result = this.File(memStream.GetBuffer(), "image/jpeg");
    }

它运行正常并创建了图像,但如果我存储在id中的文本变大了一点,那么由于我设置的宽度,我在图像中看不到整个文本。

是否有任何选项可以使其动态?

在C#中创建自动调整大小的图像

您必须使用:

SizeF size = graphics.MeasureString(id, font);

这将使用选择的字体为您提供准确的id维度
请参阅MeasureString语法。

所以你可以做

Bitmap bmp = new Bitmap(1, 1);
Graphics graphics = Graphics.FromImage(bmp);
Font font = new Font(FontName, FontSize);
SizeF size = graphics.MeasureString("", font);
int width=1 + (int)size.Width;
int height= 1 + (int)size.Height;
bmp = new Bitmap(width,height);
Rectangle displayRectangle = new Rectangle(0, 0, width, height);
// All your previous code here

我有一个小方法,使用一个Font迭代,将字体大小减少1点,直到文本适合给定的宽度。

Public Function GetBestFitFont(ByVal pText As String, ByVal pFontInitial As Font, ByVal pWidth As Integer, ByVal g As Graphics) As Font
        Dim mfont As Font = pFontInitial 
        Dim sizeW As Integer = g.MeasureString( pText, mfont ).Width
        Do While sizeW >= pWidth
            If (mfont.Size - 1) > 1 Then
                mfont = New Font(mfont.FontFamily, mfont.Size - 1, mfont.Style, mfont.Unit)
            Else
                Exit Do
            End If
            sizeW = g.MeasureString( pText, mfont ).Width
        Loop
        Return mfont
    End Function

您可以尝试这种方法。我已经根据图像宽度和文本长度计算出字体大小。

private void WriteWatermarkText(Image image)
        {
            string watermark = config.Watermark.Text;
            if (string.IsNullOrEmpty(watermark))
                return;
            // Pick an appropriate font size depending on image size
            int fontsize = (image.Width / watermark.Length);//get the font size with respect to length of the string;
            if (fontsize > 24)
                fontsize = 18;
            // Set up the font
            using (Graphics graphics = Graphics.FromImage(image))
            {
                graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
                Font font = new Font("Arial Black", fontsize, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
                // Determine size of watermark to write background
                SizeF watermarkSize = graphics.MeasureString(watermark, font);
                int xPosition = (image.Width / 2) - ((int)watermarkSize.Width / 2);
                int yPosition = (image.Height / 2) - ((int)watermarkSize.Height / 2);
                // Write watermark
                graphics.DrawString(
                watermark,
                font,
                new SolidBrush(Color.FromArgb(config.Watermark.Opacity, Color.WhiteSmoke)),
                xPosition,
                yPosition
                );
                graphics.Dispose();
            }
        }

查看Measurestring和GetTextExtent GDI方法以获得创建的文本的大小,然后可以重新调整位图大小。