c# bitmap裁剪
时间: 2023-08-19 10:06:17 浏览: 130
你可以使用C#中的Bitmap类来裁剪图像。以下是一个简单的示例代码,演示如何裁剪一个Bitmap图像:
```csharp
using System;
using System.Drawing;
class Program
{
static void Main()
{
// 加载原始图像
Bitmap originalImage = new Bitmap("original.jpg");
// 定义裁剪的矩形区域
Rectangle cropRect = new Rectangle(50, 50, 200, 200);
// 创建一个新的Bitmap来存储裁剪后的图像
Bitmap croppedImage = new Bitmap(cropRect.Width, cropRect.Height);
using (Graphics g = Graphics.FromImage(croppedImage))
{
// 将裁剪后的图像绘制到新的Bitmap上
g.DrawImage(originalImage, new Rectangle(0, 0, croppedImage.Width, croppedImage.Height),
cropRect, GraphicsUnit.Pixel);
}
// 保存裁剪后的图像
croppedImage.Save("cropped.jpg");
// 释放资源
originalImage.Dispose();
croppedImage.Dispose();
}
}
```
在这个示例中,我们首先加载了原始图像,然后定义了要裁剪的矩形区域(在示例中是50, 50为起点,宽度和高度都是200)。然后,我们创建一个新的Bitmap对象来存储裁剪后的图像,并使用Graphics类的DrawImage方法将裁剪后的图像绘制到新的Bitmap上。最后,我们保存裁剪后的图像并释放资源。
请确保在运行代码之前,将"original.jpg"替换为你要裁剪的实际图像路径,并且有足够的权限来保存裁剪后的图像。
阅读全文
相关推荐


















