C#开发,一个图片(Bitmap)需要截取其中一块区域,网上找到了比较简单的办法:

https://stackoverflow.com/questions/734930/how-to-crop-an-image-using-c

实现代码如下:

Bitmap target = crop(src);

//方法定义
private Bitmap crop(Bitmap src)
{
    Rectangle cropRect = new Rectangle(0,0,400, 400);
    Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);

    using (Graphics g = Graphics.FromImage(target))
    {
        g.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height),
              cropRect,
              GraphicsUnit.Pixel);
    }
    return target;
}

效果:

原图:

C#  Bitmap裁剪_C#编程

截取后(左上角一块):

C#  Bitmap裁剪_编程开发_02