0

i want to add an additonal height to an Image to give it a subtitle. I do not want to "compres" or resize my original image. I want to keep it's size and add +40 px to its height on bottom and add a text like this example

The red part is the original image. The blue part is my addition.

I tried this code but my text appears "outside" the image i think.

Image image = Image.FromFile("D:\\my_sample_image.jpg");
// Create graphics from image
Graphics graphics = Graphics.FromImage(image);
// Create font
Font font = new Font("Times New Roman", 42.0f);
// Create text position
PointF point = new PointF(150, image.Height+40);
// Draw text
graphics.DrawString("Watermark", font, Brushes.Red, point);
// Save image
image.Save("D:\\my_sample_output.jpg");
MessageBox.Show("FINISHED");
// Open generated image file in default image viewer installed in Windows
Process.Start("D:\\my_sample_output.jpg");
pila
  • 928
  • 3
  • 11
  • 28

2 Answers2

6

Make a new Bitmap, create a Graphics out of it, then draw the old image with room for text at the bottom.

Bitmap image = new Bitmap("path/resource");
Bitmap newImage = new Bitmap(image.Width, image.Height + 40);
using (Graphics g = Graphics.FromImage(newImage))
{
      // Draw base image
      Rectangle rect = new Rectangle(0, 0, image.Width, image.Height); 
      g.DrawImageUnscaledAndClipped(image, rect);
      //Fill undrawn area
      g.FillRectangle(new SolidBrush(Color.Black), 0, image.Height, newImage.Width, 40);
      // Create font
      Font font = new Font("Times New Roman", 22.0f);
      // Create text position (play with positioning)
      PointF point = new PointF(0, image.Height);
      // Draw text
      g.DrawString("Watermark", font, Brushes.Red, point);
 }
corylulu
  • 3,449
  • 1
  • 19
  • 35
0
public Image addSubtitleToImage(string path, int height, string title)
{
    Bitmap image = new Bitmap(path);
    Bitmap newImage = new Bitmap(image.Width, image.Height + height);
    using (Graphics g = Graphics.FromImage(newImage))
    {
        g.FillRectangle(new SolidBrush(Color.Black), new Rectangle(0, 0,   newImage.Width, newImage.Height));
        g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height));

        Font font = new Font("Tahoma", 30.0f);
        g.DrawString(title, font, Brushes.White, new PointF(image.Width/2 , image.Height+));

    }
    return newImage;
}
danopz
  • 3,310
  • 5
  • 31
  • 42
BehroOoz
  • 1
  • 3