当前位置:  开发笔记 > 编程语言 > 正文

高质量图像缩放库

如何解决《高质量图像缩放库》经验,为你挑选了5个好方法。

我想在C#中缩放图像,其质量水平与Photoshop一样好.有没有可用的C#图像处理库来做这件事?



1> Reinstate Mo..:

这是一个很好评论的Image Manipulation助手类,您可以查看和使用它.我把它写成了如何在C#中执行某些图像处理任务的例子.您将对ResizeImage函数感兴趣,该函数采用System.Drawing.Image,宽度和高度作为参数.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;

namespace DoctaJonez.Drawing.Imaging
{
    /// 
    /// Provides various image untilities, such as high quality resizing and the ability to save a JPEG.
    /// 
    public static class ImageUtilities
    {    
        /// 
        /// A quick lookup for getting image encoders
        /// 
        private static Dictionary encoders = null;

        /// 
        /// A lock to prevent concurrency issues loading the encoders.
        /// 
        private static object encodersLock = new object();

        /// 
        /// A quick lookup for getting image encoders
        /// 
        public static Dictionary Encoders
        {
            //get accessor that creates the dictionary on demand
            get
            {
                //if the quick lookup isn't initialised, initialise it
                if (encoders == null)
                {
                    //protect against concurrency issues
                    lock (encodersLock)
                    {
                        //check again, we might not have been the first person to acquire the lock (see the double checked lock pattern)
                        if (encoders == null)
                        {
                            encoders = new Dictionary();

                            //get all the codecs
                            foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())
                            {
                                //add each codec to the quick lookup
                                encoders.Add(codec.MimeType.ToLower(), codec);
                            }
                        }
                    }
                }

                //return the lookup
                return encoders;
            }
        }

        /// 
        /// Resize the image to the specified width and height.
        /// 
        /// The image to resize.
        /// The width to resize to.
        /// The height to resize to.
        /// The resized image.
        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;
        }

        ///  
        /// Saves an image as a jpeg image, with the given quality 
        ///  
        /// Path to which the image would be saved. 
        /// An integer from 0 to 100, with 100 being the 
        /// highest quality 
        /// 
        /// An invalid value was entered for image quality.
        /// 
        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 encoder parameter for the image quality
            EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
            //get the jpeg codec
            ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");

            //create a collection of all parameters that we will pass to the encoder
            EncoderParameters encoderParams = new EncoderParameters(1);
            //set the quality parameter for the codec
            encoderParams.Param[0] = qualityParam;
            //save the image using the codec and the parameters
            image.Save(path, jpegCodec, encoderParams);
        }

        ///  
        /// Returns the image codec with the given mime type 
        ///  
        public static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            //do a case insensitive search for the mime type
            string lookupKey = mimeType.ToLower();

            //the codec to return, default to null
            ImageCodecInfo foundCodec = null;

            //if we have the encoder, get it to return
            if (Encoders.ContainsKey(lookupKey))
            {
                //pull the codec from the lookup
                foundCodec = Encoders[lookupKey];
            }

            return foundCodec;
        } 
    }
}

更新

一些人一直在评论中询问如何使用ImageUtilities类的示例,所以在这里你去.

//resize the image to the specified height and width
using (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);
}

注意

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


+1这个作品非常出色!您需要在此代码中更正的一个问题是在将质量变量传递给编码器参数之前将其转换为long,否则您将获得无效的参数运行时异常.
它应该读取GetEncoderInfo而不是getEncoderInfo.我修复了拼写错误,现在编译了这个类.

2> Hallgrim..:

当你使用GDI +绘制图像时,它在我看来非常好.您可以使用它来创建缩放图像.

如果您想使用GDI +扩展图像,可以执行以下操作:

Bitmap original = ...
Bitmap scaled = new Bitmap(new Size(original.Width * 4, original.Height * 4));
using (Graphics graphics = Graphics.FromImage(scaled)) {
  graphics.DrawImage(original, new Rectangle(0, 0, scaled.Width, scaled.Height));
}



3> kitsune..:

像Imagemagick和GD这样的测试库可用于.NET

您还可以阅读双三次插值等内容并编写自己的内容.



4> Robinicks..:

CodeProject文章讨论和共享缩放图像的源代码:

使用过滤器的两次通过缩放

使用.NET GDI +在C#中对图像进行矩阵变换

使用GDI + for .NET调整摄影图像的大小

Haar变换的快速二元图像缩放



5> superlogical..:

使用此库:http://imageresizing.net

请阅读图书馆作者阅读本文:20使用.NET调整图像大小

推荐阅读
路人甲
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有