/// <summary> /// 获取指定图片的缩略(指定大小) /// </summary> /// <remarks>@FrancisTan 2016-05-17</remarks> /// <param name="imgStream">图片二进制流</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图宽度</param> /// <param name="quality">图片质量百分比,它必须是0~100之间的整数</param> /// <exception cref="ArgumentNullException">图片文件流为空</exception> /// <returns>缩略图二进制流</returns> public static Stream GetThumbnailStream(Stream imgStream, int width, int height, int quality) { if (imgStream == null || imgStream.Length == 0) { throw new ArgumentNullException("imgStream"); } if (width <= 0) { throw new ArgumentOutOfRangeException("width", "图片宽度必须大于0"); } if (height <= 0) { throw new ArgumentOutOfRangeException("height", "图片高度必须大于0"); } if (quality <= 0 || quality > 100) { throw new ArgumentOutOfRangeException("quality", "图片质量百分比须在0~100之间"); } // Format is automatically detected though can be changed. ISupportedImageFormat format = new PngFormat(); Size size = new Size(width, height); using (ImageFactory factory = new ImageFactory(preserveExifData: true)) { factory.Load(imgStream) .Format(format); var scaling = GetScaling(factory.Image.Size, size); var scaleSize = GetScaleSize(factory.Image.Size, scaling); factory.Constrain(scaleSize); return(CropAroundCenterPoint(factory, size)); } }
private static Stream CompressImage(ImageFactory imgfactory, string imageFile) { MemoryStream memStream; var quality = Options.Quality; imgfactory.Load(imageFile); if (Options.Scale != 1) { imgfactory.Constrain(new Size((int)(imgfactory.Image.Size.Height * Options.Scale), (int)(imgfactory.Image.Size.Width * Options.Scale))); } do { memStream = new MemoryStream(); imgfactory .Quality(quality--) .Save(memStream); } while (((memStream.Length > Options.MaxImageSize * 1024) && quality > 0) && Options.MaxImageSize != 0); if (quality == 0) { imgfactory .Quality(Options.Quality) .Save(memStream); } return(memStream); }
/// <summary> /// Performs the Constrain effect from the ImageProcessor library. /// </summary> /// <param name="image">The image with which to apply the effect.</param> /// <param name="size">The size with which the Constrain effect will be applied.</param> /// <returns>A new image with the Constrain effect applied.</returns> public Image Constrain(Image image, Size size) { using var factory = new ImageFactory(); factory.Load(image); factory.Constrain(size); var result = factory.Image; return(new Bitmap(result)); }
// GET: Media public ActionResult Cache(String slug, String format, String filename) { String cacheFilename = Server.MapPath(String.Format("~/App_Data/media/{0}/{1}/{2}", slug, format, filename)); String remoteFilename = String.Format("https://robertsspaceindustries.com/media/{0}/{1}/{2}", slug, "source", filename); if (!System.IO.File.Exists(cacheFilename)) { if (!System.IO.Directory.Exists(Path.GetDirectoryName(cacheFilename))) { System.IO.Directory.CreateDirectory(Path.GetDirectoryName(cacheFilename)); } using (WebClient client = new WebClient()) { using (Stream stream = client.OpenRead(remoteFilename)) { using (MemoryStream inStream = new MemoryStream()) { stream.CopyTo(inStream); using (ImageFactory factory = new ImageFactory()) { factory.Load(inStream); String[] parts = format.Split('x'); if (parts.Length != 2) { parts = new String[] { "800", "600" }; } factory.Constrain(new System.Drawing.Size { Height = parts[1].ToInt32(600), Width = parts[0].ToInt32(800) }); // , new System.Drawing.Point // { // X = factory.Image.Width / 2, // Y = factory.Image.Height / 2 // }); if (Path.GetExtension(cacheFilename) == "jpg") { using (FileStream outStream = new FileStream(cacheFilename, FileMode.Create)) { factory.Optimize(outStream); } } else { factory.Save(cacheFilename); } } } } } } String contentType = MimeMapping.GetMimeMapping(cacheFilename); this.Response.Cache.SetCacheability(HttpCacheability.Public); this.Response.Cache.SetExpires(DateTime.Today.AddDays(7)); this.Response.Cache.SetMaxAge(TimeSpan.FromHours(168)); return(File(cacheFilename, contentType)); }