图片裁剪后图片大小变大
https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.imaging.encoder.quality?view=netframework-4.8
public ActionResult SaveVideoThumbnailFile(string keyValue,string strEntity) { Encoder myEncoder; EncoderParameter myEncoderParameter; EncoderParameters myEncoderParameters; ImageCodecInfo myImageCodecInfo; myEncoder = Encoder.Quality; myEncoderParameters = new EncoderParameters(1); myEncoderParameter = new EncoderParameter(myEncoder, 100L); myEncoderParameters.Param[0] = myEncoderParameter; //int width= int height, int offsetX, int offsetY var PostData = strEntity.ToJObject(); int width = (int)PostData["width"]; int height = (int)PostData["height"]; int offsetX = (int)PostData["offsetX"]; int offsetY = (int)PostData["offsetY"]; string filePath = "/Resource/VideoThumbnailFile/" + keyValue; //创建路径 string firstfile = System.Web.HttpContext.Current.Server.MapPath("~" + ModelList[0].VideoUrl); myImageCodecInfo = GetEncoderInfo(MimeMapping.GetMimeMapping(firstfile)); yfile = System.Web.HttpContext.Current.Server.MapPath("~" + model.VideoUrl); file = yfile.Replace("VideoFile", "VideoThumbnailFile"); Bitmap refile = GetPartOfImageRec(yfile, width, height, offsetX, offsetY); refile.Save(file,myImageCodecInfo, myEncoderParameters); }
/// <summary> /// 截取一张图片的指定部分 /// </summary> /// <param name="bitmapPathAndName">原始图片路径名称</param> /// <param name="width">截取图片的宽度</param> /// <param name="height">截取图片的高度</param> /// <param name="offsetX">开始截取图片的X坐标</param> /// <param name="offsetY">开始截取图片的Y坐标</param> /// <returns></returns> public Bitmap GetPartOfImageRec(string bitmapPathAndName, int width, int height, int offsetX, int offsetY) { Bitmap sourceBitmap = new Bitmap(bitmapPathAndName); Bitmap resultBitmap = new Bitmap(width, height); using (Graphics g = Graphics.FromImage(resultBitmap)) { Rectangle resultRectangle = new Rectangle(0, 0, width, height); Rectangle sourceRectangle = new Rectangle(0 + offsetX, 0 + offsetY, width, height); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.White); g.DrawImage(sourceBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel); } return resultBitmap; }
/// <summary> /// 截取一张图片的指定部分 /// </summary> /// <param name="bitmapPathAndName">原始图片路径名称</param> /// <param name="width">截取图片的宽度</param> /// <param name="height">截取图片的高度</param> /// <param name="offsetX">开始截取图片的X坐标</param> /// <param name="offsetY">开始截取图片的Y坐标</param> /// <returns></returns> public Bitmap GetPartOfImageRec(string bitmapPathAndName, int width, int height, int offsetX, int offsetY) { Bitmap sourceBitmap = new Bitmap(bitmapPathAndName); Bitmap resultBitmap = new Bitmap(width, height); using (Graphics g = Graphics.FromImage(resultBitmap)) { Rectangle resultRectangle = new Rectangle(0, 0, width, height); Rectangle sourceRectangle = new Rectangle(0 + offsetX, 0 + offsetY, width, height); g.InterpolationMode =System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode =System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.White); g.DrawImage(sourceBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel); } return resultBitmap; }