截取一张图片中的部分区域存成新文件

代码如下:

// 载入源图片

Image oldImage= System.Drawing.Image.FromFile("d;//old.jpg");

//用指定的大小和格式初始化 Bitmap 类的新实例
Bitmap bitmap = new Bitmap(cutWidth, cutHeight, PixelFormat.Format32bppArgb);

//从指定的 Image 对象创建新 Graphics 对象
Graphics graphics = Graphics.FromImage(bitmap);

// 将源图片画到bitmap的graphics// oldImage是原图片
// 0,0 是graphics的起始位置,也就是从原点开始画
// new Rectangle是截取源图片的目标区域,用户只需要改变其中四个值即可
graphics.DrawImage(oldImage, 0, 0, new Rectangle(startPoint.X, startPoint.Y, cutWidth, cutHeight), GraphicsUnit.Pixel);

// 将Bitmap转化成Image
Image image= Image.FromHbitmap(bitmap.GetHbitmap());

// Image存到文件去
image.Save(""); 

好的,您可以使用C#的图像处理库来实现这个功能。以下是一个简单的代码示例: ```csharp using System.Drawing; public static void CropIDCard(string originalImagePath, string croppedImagePath) { // 身份证图片的宽度和高度 int idCardWidth = 850; int idCardHeight = 540; // 加载原始图片 Image originalImage = Image.FromFile(originalImagePath); // 创建一个的身份证大小的图片 Image croppedImage = new Bitmap(idCardWidth, idCardHeight); // 创建一个 Graphics 对象,用于在图片上绘制原始图片一部分 Graphics graphics = Graphics.FromImage(croppedImage); // 定义一个矩形,表示要截取图片区域 Rectangle sourceRectangle = new Rectangle(0, 0, idCardWidth, idCardHeight); // 定义一个矩形,表示要绘制的目标区域 Rectangle destinationRectangle = new Rectangle(0, 0, idCardWidth, idCardHeight); // 绘制原始图片一部分图片上 graphics.DrawImage(originalImage, destinationRectangle, sourceRectangle, GraphicsUnit.Pixel); // 保存图片 croppedImage.Save(croppedImagePath, ImageFormat.Jpeg); // 释放资源 graphics.Dispose(); croppedImage.Dispose(); originalImage.Dispose(); } ``` 在这个示例,`originalImagePath` 参数是原始图片的路径,`croppedImagePath` 参数是要保存的图片的路径。`idCardWidth` 和 `idCardHeight` 变量定义了身份证图片应该具有的宽度和高度。您可以根据需要修改这些变量的值。 您可以调用 `CropIDCard` 方法来截取图片。例如: ```csharp string originalImagePath = "path/to/original/image.jpg"; string croppedImagePath = "path/to/cropped/image.jpg"; CropIDCard(originalImagePath, croppedImagePath); ``` 这个方法会读取原始图片截取出身份证图片部分,并将结果保存到图片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值