根据左上角坐标、长、高,实现截图
/// <summary> /// 根据左上角坐标、长、高,实现截图 /// </summary> /// <param name="vehicleImage">图片二进制流</param> /// <param name="image1Url">图片路径</param> /// <param name="vehicleRegionLeft">左上角X轴坐标</param> /// <param name="vehicleRegionTop">左上角Y轴坐标</param> /// <param name="vehicleRegionWidth">宽度</param> /// <param name="vehicleRegionHeight">高度</param> /// <returns></returns> public ActionResult GetVehicleImageByRegion(string vehicleImage, string image1Url, int vehicleRegionLeft, int vehicleRegionTop, int vehicleRegionWidth, int vehicleRegionHeight) { if (!string.IsNullOrEmpty(vehicleImage) && FileHelper.Exists(vehicleImage)) { byte[] buffer = FileHelper.ReadAllBytes(vehicleImage); return File(buffer, "image/jpeg"); } else if (!string.IsNullOrEmpty(image1Url)) { Bitmap image1Bitmap = null; Bitmap newBit = null; if (image1Url.ToLower().Contains("http")) //http路由 { WebRequest request = WebRequest.Create(image1Url); WebResponse response = request.GetResponse(); if(response == null) return File(new byte[0], "image/jpeg"); Stream stream = response.GetResponseStream(); image1Bitmap = (Bitmap)Image.FromStream(stream); } else //ftp路径 { if (!FileHelper.Exists(image1Url)) return File(new byte[0], "image/jpeg"); image1Bitmap = (Bitmap)Image.FromFile(image1Url); } try { newBit = new Bitmap(vehicleRegionWidth, vehicleRegionHeight); using (Graphics g = Graphics.FromImage(newBit)) //实现截图 { g.DrawImage(image1Bitmap, new Rectangle(0, 0, newBit.Width, newBit.Height), new Rectangle(vehicleRegionLeft, vehicleRegionTop, newBit.Width, newBit.Height), GraphicsUnit.Pixel); using (MemoryStream ms = new MemoryStream()) { newBit.Save(ms, ImageFormat.Jpeg); //保存图片 return File(ms.ToArray(), "image/jpeg"); } } } finally { if (image1Bitmap != null) image1Bitmap.Dispose(); if (newBit != null) newBit.Dispose(); } } return File(new byte[0], "image/jpeg"); }