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

时间:2012-01-19 11:58:51

标签: c# image

我使用以下代码创建“自动调整大小”的图片:

    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中存储的文本变得更大,那么由于我设置的宽度,我在图像中看不到全文。

有没有选择让它变得动态?

4 个答案:

答案 0 :(得分:3)

你必须使用:

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

这将使用选择的字体准确地为您提供您的身份尺寸 请参阅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

答案 1 :(得分:1)

我有一个方法,使用一个字体迭代减少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

答案 2 :(得分:0)

您可以尝试此方法。我根据图像宽度和文本长度计算了字体大小。

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();
            }
        }

答案 3 :(得分:0)

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