如何在C#中剪切一部分图像

我不知道如何从其他大图像切割矩形图像。

假设有300 x 600 image.png。

我只想用X:10 Y 20,200,高度100切割一个矩形,并将其保存到其他文件中。

我怎么能在C#中做到这一点?

谢谢!!!

查看MSDN上的Graphics Class 。

这是一个将您指向正确方向的示例(请注意Rectangle对象):

 public Bitmap CropImage(Bitmap source, Rectangle section) { // An empty bitmap which will hold the cropped image Bitmap bmp = new Bitmap(section.Width, section.Height); Graphics g = Graphics.FromImage(bmp); // Draw the given area (section) of the source image // at location 0,0 on the empty bitmap (bmp) g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel); return bmp; } // 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); 

另一种制作图像的方法是克隆具有特定起点和大小的图像。

 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);