为副标题添加图像高度

pil*_*ila 0 c# image-manipulation image

我想为图像添加一个额外的高度,以给它一个副标题.我不想"压缩"或调整原始图像的大小.我想保持它的大小,并在底部添加+40像素的高度,并添加像这个例子的文字

红色部分是原始图像.蓝色部分是我的补充.

我尝试了这段代码,但我的文字显示在"我想"的图像"外面".

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");
Run Code Online (Sandbox Code Playgroud)

cor*_*ulu 6

创建一个新的位图,从中创建一个图形,然后在底部绘制带有文本空间的旧图像.

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);
 }
Run Code Online (Sandbox Code Playgroud)