1

I want to draw a text on image. However text is not centered on the image. What can I do? I set the coordinate x=0, y=0; But text is shifted...

Bitmap image = new Bitmap(width, height);
RectangleF rectf = new RectangleF(0, 0, image.Width, image.Height);

using (Graphics g = Graphics.FromImage(image))
{
    g.CompositingQuality = CompositingQuality.HighQuality;
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.SmoothingMode = SmoothingMode.HighQuality;
    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
    g.Clear(Color.Black);
    StringFormat sf = new StringFormat();
    sf.Alignment = StringAlignment.Near;
    sf.LineAlignment = StringAlignment.Center;
    g.DrawString(text, new Font("Times New Roman", fontSize), Brushes.Red, rectf, sf);

    g.Flush();
}

return image;  

exemple What I want... My target

Clemens
  • 123,504
  • 12
  • 155
  • 268
G3k0S
  • 37
  • 1
  • 11
  • 1
    [I want to remove padding from top and bottom from generated Image](https://stackoverflow.com/a/54383828/7444103). To calculate the Font size based on pixel measures, get the FontObject class you can find here: [Properly draw text using Graphics Path](https://stackoverflow.com/a/53074638/7444103) – Jimi Jul 06 '20 at 17:48
  • Thanks @Jimi very helpful :) – G3k0S Jul 06 '20 at 18:59
  • @Jimi, how to calculate font size, if I'm sending to method only height of image? Because, your example is working, but image size(height) will be changed, however me need specific size(height) image. – G3k0S Jul 07 '20 at 06:48

0 Answers0