我不知道如何从其他大图像切割矩形图像.
假设有300 x 600 image.png.
我只想用X:10 Y 20,200,高度100切割一个矩形,并将其保存到其他文件中.
我怎么能在C#中做到这一点?
谢谢!!!
Jam*_*ill 31
查看MSDN上的Graphics Class.
这是一个将指向正确方向的示例(注意Rectangle
对象):
public Bitmap CropImage(Bitmap source, Rectangle section)
{
var bitmap = new Bitmap(section.Width, section.Height);
using (var g = Graphics.FromImage(bitmap))
{
g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
return bitmap;
}
}
// Example use:
Bitmap source = new Bitmap(@"C:\tulips.jpg");
Rectangle section = new Rectangle(new Point(12, 50), new Size(150, 150));
Bitmap CroppedImage = CropImage(source, section);
Run Code Online (Sandbox Code Playgroud)
Abh*_*min 24
另一种制作图像的方法是克隆具有特定起点和大小的图像.
int x= 10, y=20, width=200, height=100;
Bitmap source = new Bitmap(@"C:\tulips.jpg");
Bitmap CroppedImage = source.Clone(new System.Drawing.Rectangle(x, y, width, height), source.PixelFormat);
Run Code Online (Sandbox Code Playgroud)