I am generating images from specified text but there is a problem I\'m facing: I cannot remove the top and bottom padding of the drawn Text inside the image I generate.
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:
GraphicsPath
objectGraphicsPath
bounding rectangle of the text objectY
position and the Pen Size, using their negative value: we need to move backwards that measure.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 (var path = new GraphicsPath()) {
path.AddString(text, font.FontFamily, (int)font.Style, font.Size, Point.Empty, StringFormat.GenericTypographic);
RectangleF textBounds = path.GetBounds();
using (var bitmap = new Bitmap((int)textBounds.Width, (int)textBounds.Height, PixelFormat.Format32bppArgb))
using (var g = Graphics.FromImage(bitmap))
using (var brush = new SolidBrush(Color.LightGreen)) {
g.SmoothingMode = SmoothingMode.AntiAlias;
// When rendering without a GraphicsPath object
//g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
g.Clear(Color.Black);
g.TranslateTransform(-(textBounds.X + penSize), -(textBounds.Y + penSize));
g.FillPath(brush, path);
bitmap.Save("[Image Path]", ImageFormat.Png);
// Or: return (Bitmap)bitmap.Clone();
// Or: return bitmap; declaring the bitmap without a using statement
}
}