栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

高质量图像缩放库

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

高质量图像缩放库

这是一个经过很好评论的“图像处理”帮助器类,您可以查看和使用。我将其作为如何在C#中执行某些图像处理任务的示例进行了编写。您将对
ResizeImage 函数感兴趣,该函数采用System.Drawing.Image,宽度和高度作为参数。

using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Imaging;namespace DoctaJonez.Drawing.Imaging{    /// <summary>    /// Provides various image untilities, such as high quality resizing and the ability to save a JPEG.    /// </summary>    public static class ImageUtilities    { /// <summary>        /// A quick lookup for getting image enprers        /// </summary>        private static Dictionary<string, ImageCodecInfo> enprers = null;        /// <summary>        /// A lock to prevent concurrency issues loading the enprers.        /// </summary>        private static object enprersLock = new object();        /// <summary>        /// A quick lookup for getting image enprers        /// </summary>        public static Dictionary<string, ImageCodecInfo> Enprers        { //get accessor that creates the dictionary on demand get {     //if the quick lookup isn't initialised, initialise it     if (enprers == null)     {         //protect against concurrency issues         lock (enprersLock)         {  //check again, we might not have been the first person to acquire the lock (see the double checked lock pattern)  if (enprers == null)  {      enprers = new Dictionary<string, ImageCodecInfo>();      //get all the precs      foreach (ImageCodecInfo prec in ImageCodecInfo.GetImageEnprers())      {          //add each prec to the quick lookup          enprers.Add(prec.MimeType.ToLower(), prec);      }  }         }     }     //return the lookup     return enprers; }        }        /// <summary>        /// Resize the image to the specified width and height.        /// </summary>        /// <param name="image">The image to resize.</param>        /// <param name="width">The width to resize to.</param>        /// <param name="height">The height to resize to.</param>        /// <returns>The resized image.</returns>        public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height)        { //a holder for the result Bitmap result = new Bitmap(width, height); //set the resolutions the same to avoid cropping due to resolution differences result.SetResolution(image.HorizontalResolution, image.VerticalResolution); //use a graphics object to draw the resized image into the bitmap using (Graphics graphics = Graphics.FromImage(result)) {     //set the resize quality modes to high quality     graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;     graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;     graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;     //draw the image into the target bitmap     graphics.DrawImage(image, 0, 0, result.Width, result.Height); } //return the resulting bitmap return result;        }        /// <summary>         /// Saves an image as a jpeg image, with the given quality         /// </summary>         /// <param name="path">Path to which the image would be saved.</param>         /// <param name="quality">An integer from 0 to 100, with 100 being the         /// highest quality</param>         /// <exception cref="ArgumentOutOfRangeException">        /// An invalid value was entered for image quality.        /// </exception>        public static void SaveJpeg(string path, Image image, int quality)        { //ensure the quality is within the correct range if ((quality < 0) || (quality > 100)) {     //create the error message     string error = string.Format("Jpeg image quality must be between 0 and 100, with 100 being the highest quality.  A value of {0} was specified.", quality);     //throw a helpful exception     throw new ArgumentOutOfRangeException(error); } //create an enprer parameter for the image quality EnprerParameter qualityParam = new EnprerParameter(System.Drawing.Imaging.Enprer.Quality, quality); //get the jpeg prec ImageCodecInfo jpegCodec = GetEnprerInfo("image/jpeg"); //create a collection of all parameters that we will pass to the enprer EnprerParameters enprerParams = new EnprerParameters(1); //set the quality parameter for the prec enprerParams.Param[0] = qualityParam; //save the image using the prec and the parameters image.Save(path, jpegCodec, enprerParams);        }        /// <summary>         /// Returns the image prec with the given mime type         /// </summary>         public static ImageCodecInfo GetEnprerInfo(string mimeType)        { //do a case insensitive search for the mime type string lookupKey = mimeType.ToLower(); //the prec to return, default to null ImageCodecInfo foundCodec = null; //if we have the enprer, get it to return if (Enprers.ContainsKey(lookupKey)) {     //pull the prec from the lookup     foundCodec = Enprers[lookupKey]; } return foundCodec;        }     }}

更新资料

一些人一直在评论中询问如何使用ImageUtilities类的示例,因此您可以开始使用。

//resize the image to the specified height and widthusing (var resized = ImageUtilities.ResizeImage(image, 50, 100)){    //save the resized image as a jpeg with a quality of 90    ImageUtilities.SaveJpeg(@"C:myimage.jpeg", resized, 90);}

注意

请记住,图像是一次性的,因此您需要将调整大小的结果分配给using声明(或者您可以尝试使用try并确保您在finally中调用dispose)。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/433130.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号