user1145150
user1145150

Reputation: 11

Creating Auto Sized Image in C#

I am using the following code to create an 'auto-sized' 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");
    }

It's running properly and creating the image, but if my text which I store in id gets a little bit bigger then I don't see whole text in the image because of width that I set.

Is there any option to make it dynamic?

Upvotes: 0

Views: 1414

Answers (4)

Romias
Romias

Reputation: 14133

I have a little method that using one Font it iterates decresing the font size in 1 point until the text fits in the given width.

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

Upvotes: 1

Marco
Marco

Reputation: 57573

You have to use:

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

This will give you exactly your id dimension using choosen font.
See MeasureString syntax.

So you could do

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

Upvotes: 3

Laird Streak
Laird Streak

Reputation: 399

Have a look at Measurestring and GetTextExtent GDI methods to get the size of the text you create then you can re-size the Bitmap size.

Upvotes: 0

Shoaib Shaikh
Shoaib Shaikh

Reputation: 4585

You can try this method. i have calculated fontsize based on image width and length of text.

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

Upvotes: 0

Related Questions