在图像c#的底部填充/追加矩形
本文关键字:追加 填充 底部 图像 | 更新日期: 2023-09-27 18:02:08
我需要在图像的底部填充一个矩形,但不是在图像上方,所以它应该是在图像的底部添加一个矩形。
我现在有什么:
private void DrawRectangle()
{
string imageFilePath = @"c:'Test.jpg";
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
using (Image img = Image.FromFile(imageFilePath))
{
SolidBrush brush = new SolidBrush(Color.Black);
int width = img.Width;
int height = img.Height - 350;
graphics.FillRectangle(brush, 0, height, width, 350);
}
}
bitmap.Save(@"c:'Test1.jpg");
}
但这是在图像上。
知道的吗?
谢谢。
您需要知道原始图像的尺寸,以便设置新位图的大小,必须更大以容纳矩形。
private void DrawRectangle()
{
string imageFilePath = @"c:'Test.jpg";
int rectHeight = 100;
using (Image img = Image.FromFile(imageFilePath)) // load original image
using (Bitmap bitmap = new Bitmap(img.Width, img.Height + rectHeight)) // create blank bitmap of desired size
using (Graphics graphics = Graphics.FromImage(bitmap))
{
// draw existing image onto new blank bitmap
graphics.DrawImage(img, 0, 0, img.Width, img.Height);
SolidBrush brush = new SolidBrush(Color.Black);
// draw your rectangle below the original image
graphics.FillRectangle(brush, 0, img.Height, img.Width, rectHeight);
bitmap.Save(@"c:'Test1.bmp");
}
}
看看FillRectangle()
的方法重载。它有一个定义如下的:FillRectangle(Brush brush, int PositionX, int PositionY, int Width, int Height)