I want to remove padding from top and bottom from generated Image

旧时模样 提交于 2020-01-06 04:00:06

问题


I am generate image from specified text but there is some problem i facing with remove padding from top and bottom of generated image.

I tried by changing string format while using DrawString but somehow i achieved to remove from left and right.

private void button1_Click(object sender, EventArgs e)
    {
        Font font = new Font("Arial", 52, FontStyle.Regular);
        Image i = GetTextAsImage(textBox1.Text,400, font, Color.Black, Color.LightGray);

        i.Save("myImage.jpeg", ImageFormat.Jpeg);

    }
    private Image GetTextAsImage(String text, int widthInPixel, Font textFont, Color textColor, Color backColor)
    {
        //first, create a dummy bitmap just to get a graphics object
        Image img = new Bitmap(1, 1);
        Graphics drawing = Graphics.FromImage(img);

        //measure the string to see how big the image needs to be
        SizeF textSize = drawing.MeasureString(text, textFont);

        //free up the dummy image and old graphics object
        img.Dispose();
        drawing.Dispose();

        //create a new image of the right size
        img = new Bitmap((int)textSize.Width, textFont.Height);

        drawing = Graphics.FromImage(img);
        drawing.SmoothingMode = SmoothingMode.AntiAlias;
        drawing.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        //paint the background
        drawing.Clear(backColor);

        //create a brush for the text
        Brush textBrush = new SolidBrush(textColor);


        drawing.DrawString(text, textFont, textBrush, 0, 0,StringFormat.GenericTypographic);

        drawing.Save();

        textBrush.Dispose();
        drawing.Dispose();

        return img;

    }

Expected output is to achieve to remove padding from top and bottom. Output i get This is the expected output


回答1:


I propose you a slightly different method, using the GraphicsPath class to both measure and draw the text on a Bitmap object.

The advantage is that the GraphicsPath class reports the actual coordinates where the object that it references will be drawn and also the size of the text in relation to a specific Font.
These measures are returned in a RectagleF structure using the GraphicsPath.GetBounds() method.
The base constructor assumes a Pen size of 1 pixel.

There's only one (small) detail to take care of: the GDI+ Bitmap object accepts dimensions expressed in integer values only, while all the other measures are expressed in floating point values.
We need to compensate for the rounding, but it's usually just ± 1 pixel.

Sample of the results:

A description of the procedure:

  • Define a Font Family and Size
  • Add the Text string to the GraphicsPath object
  • Get the GraphicsPath bounding rectangle of the text object
  • Build a Bitmap object using the bounding rectangle Size
  • Move the World coordinates, with Graphics.TranslateTransform, to the coordinates defined by the bounding rectangle Y position and the Pen Size, using their negative value: we need to move backwards that measure.
  • Draw the Text

See also these notes about GraphicsPath and Fonts:
Properly draw text using Graphics Path

Sample code:

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;


string text = "This is my Text";
Font font = new Font("Arial", 52, FontStyle.Regular, GraphicsUnit.Point);
float penSize = 1f;

using (GraphicsPath path = new GraphicsPath())
{
    path.AddString(text, font.FontFamily, (int)font.Style, font.Size, Point.Empty, StringFormat.GenericTypographic);

    RectangleF textBounds = path.GetBounds();
    using (Bitmap bitmap = new Bitmap((int)textBounds.Width, (int)textBounds.Height, PixelFormat.Format32bppArgb))
    using (Graphics g = Graphics.FromImage(bitmap))
    {
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
        g.Clear(Color.Black);
        g.TranslateTransform(-penSize, -(textBounds.Y + penSize));
        using (SolidBrush brush = new SolidBrush(Color.LightGreen))
        {
            g.FillPath(brush, path);
        }
        bitmap.Save("[Image Path]", ImageFormat.Png);
        //Or: return (Bitmap)bitmap.Clone();
    }
}


来源:https://stackoverflow.com/questions/54380214/i-want-to-remove-padding-from-top-and-bottom-from-generated-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!