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> /// taken an image portion specified /// </ Summary> /// <param name = "bitmapPathAndName"> Original image path name </ param> /// <param name = " width "> taken image width </ param> /// <param name =" height "> taken picture height </ param> /// <param name =" offsetX "> start X coordinate image taken </ param > /// <param name = "offsetY"> start capturing images of the Y coordinate </ 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> /// specified portion of an image taken /// </ summary> /// <param name = "bitmapPathAndName"> Original image path name </ param> /// <param name = " width "> width of the image taken </ param> /// <param name =" height "> taken picture height </ param> /// <param name =" offsetX "> X coordinate of the image taken beginning </ param > /// <param name = "offsetY"> Y coordinate of the start image taken </ 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; }Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.White); g.DrawImage(sourceBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel); } return resultBitmap; }Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.White); g.DrawImage(sourceBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel); } return resultBitmap; }