#region 从大图中截取一部分图片
/// <summary>
/// 从大图中截取一部分图片
/// </summary>
/// <param name="fromImagePath">来源图片地址</param>
/// <param name="offsetX">从偏移X坐标位置开始截取</param>
/// <param name="offsetY">从偏移Y坐标位置开始截取</param>
/// <param name="toImagePath">保存图片地址</param>
/// <param name="width">保存图片的宽度</param>
/// <param name="height">保存图片的高度</param>
/// <returns></returns>
public void CaptureImage(string fromImagePath, int offsetX,int offsetY, string toImagePath, int width, int height)
{
//原图片文件
Image fromImage = Image.FromFile(fromImagePath);
//创建新图位图
Bitmap bitmap = new Bitmap(width, height);
//创建作图区域
Graphics graphic = Graphics.FromImage(bitmap);
//截取原图相应区域写入作图区
graphic.DrawImage(fromImage, 0, 0, new Rectangle(offsetX, offsetY, width, height), GraphicsUnit.Pixel);
//从作图区生成新图
Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());
//保存图片
saveImage.Save(toImagePath, ImageFormat.Png);
//释放资源
saveImage.Dispose();
graphic.Dispose();
bitmap.Dispose();
}
#endregion
附加转换形式
public static void CaptureImage(Image fromImage, int offsetX, int offsetY, string toImagePath, int width, int height)
{
Bitmap bitmap = new Bitmap(width, height);
Graphics graphic = Graphics.FromImage(bitmap);
graphic.DrawImage(fromImage, 0, 0, new Rectangle(offsetX, offsetY, width, height), GraphicsUnit.Pixel);
Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());
saveImage.Save(toImagePath, System.Drawing.Imaging.ImageFormat.Png);
saveImage.Dispose();
graphic.Dispose();
bitmap.Dispose();
}
如果需要byte转换为image,可查看上一篇文章进行转换。
System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
[参考](https://www.cnblogs.com/kehaocheng/p/7126690.html)