【问题标题】:Using C# how can I resize a jpeg image?使用 C# 如何调整 jpeg 图像的大小?
【发布时间】:2011-03-05 18:58:48
【问题描述】:

使用 C# 如何调整 jpeg 图像的大小?一个代码示例会很棒。

【问题讨论】:

标签: c#


【解决方案1】:

ImageMagick 应该是最好的方法。简单可靠。

using (var image = new MagickImage(imgfilebuf))
{
    image.Resize(len, len);
    image.Strip();
    using MemoryStream ms = new MemoryStream();
    image.Write(ms);
    return ms.ToArray();
}

【讨论】:

    【解决方案2】:

    我正在使用这个:

     public static void ResizeJpg(string path, int nWidth, int nHeight)
        {
            using (var result = new Bitmap(nWidth, nHeight))
            {
                using (var input = new Bitmap(path))
                {
                    using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
                    {
                        g.DrawImage(input, 0, 0, nWidth, nHeight);
                    }
                }
    
                var ici = ImageCodecInfo.GetImageEncoders().FirstOrDefault(ie => ie.MimeType == "image/jpeg");
                var eps = new EncoderParameters(1);
                eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
                result.Save(path, ici, eps);
            }
        }
    

    【讨论】:

    • 这可行,但要小心 - 它会将我的 1920x1080 图像缩小到 960x540...但较小版本的文件大小是完整版本的 3 倍。正在调查解决方案...
    • @Morvael 那是因为接近尾声的“100L”:图像以 100% 的质量保存(70-80% 的范围更正常)。即使将其减少到 90% 也应该会显着减小尺寸 - 最后几个百分比确实会使尺寸膨胀。
    • 微软推荐通过FormatId获取JPEG Codec:ImageCodecInfo _jpegCodecInfo = ImageCodecInfo.GetImageEncoders().First(v => v.FormatID == ImageFormat.Jpeg.Guid)
    【解决方案3】:

    很好的例子。

    public static Image ResizeImage(Image sourceImage, int maxWidth, int maxHeight)
    {
        // Determine which ratio is greater, the width or height, and use
        // this to calculate the new width and height. Effectually constrains
        // the proportions of the resized image to the proportions of the original.
        double xRatio = (double)sourceImage.Width / maxWidth;
        double yRatio = (double)sourceImage.Height / maxHeight;
        double ratioToResizeImage = Math.Max(xRatio, yRatio);
        int newWidth = (int)Math.Floor(sourceImage.Width / ratioToResizeImage);
        int newHeight = (int)Math.Floor(sourceImage.Height / ratioToResizeImage);
    
        // Create new image canvas -- use maxWidth and maxHeight in this function call if you wish
        // to set the exact dimensions of the output image.
        Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format32bppArgb);
    
        // Render the new image, using a graphic object
        using (Graphics newGraphic = Graphics.FromImage(newImage))
        {
            using (var wrapMode = new ImageAttributes())
            {
                wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                newGraphic.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
            }
    
            // Set the background color to be transparent (can change this to any color)
            newGraphic.Clear(Color.Transparent);
    
            // Set the method of scaling to use -- HighQualityBicubic is said to have the best quality
            newGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
    
            // Apply the transformation onto the new graphic
            Rectangle sourceDimensions = new Rectangle(0, 0, sourceImage.Width, sourceImage.Height);
            Rectangle destinationDimensions = new Rectangle(0, 0, newWidth, newHeight);
            newGraphic.DrawImage(sourceImage, destinationDimensions, sourceDimensions, GraphicsUnit.Pixel);
        }
    
        // Image has been modified by all the references to it's related graphic above. Return changes.
        return newImage;
    }
    

    来源:http://mattmeisinger.com/resize-image-c-sharp

    【讨论】:

    • 在 LucidObscurity 的答案中指定 WrapMode 会给您带来更好的结果。您可能希望将其添加到您的代码中。
    【解决方案4】:

    很好的免费调整大小过滤器和示例代码。

    http://code.google.com/p/zrlabs-yael/

        private void MakeResizedImage(string fromFile, string toFile, int maxWidth, int maxHeight)
        {
            int width;
            int height;
    
            using (System.Drawing.Image image = System.Drawing.Image.FromFile(fromFile))
            {
                DetermineResizeRatio(maxWidth, maxHeight, image.Width, image.Height, out width, out height);
    
                using (System.Drawing.Image thumbnailImage = image.GetThumbnailImage(width, height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
                {
                    if (image.Width < thumbnailImage.Width && image.Height < thumbnailImage.Height)
                        File.Copy(fromFile, toFile);
                    else
                    {
                        ImageCodecInfo ec = GetCodecInfo();
                        EncoderParameters parms = new EncoderParameters(1);
                        parms.Param[0] = new EncoderParameter(Encoder.Compression, 40);
    
                        ZRLabs.Yael.BasicFilters.ResizeFilter rf = new ZRLabs.Yael.BasicFilters.ResizeFilter();
                        //rf.KeepAspectRatio = true;
                        rf.Height = height;
                        rf.Width = width;
    
                        System.Drawing.Image img = rf.ExecuteFilter(System.Drawing.Image.FromFile(fromFile));
                        img.Save(toFile, ec, parms);
                    }
                }
            }
        }
    

    【讨论】:

    • 注意,这段代码是不够的,你必须添加对项目的引用(最后更改是2006年11月6日)
    【解决方案5】:

    C#(或者更确切地说:.NET 框架)本身不提供这种功能,但它确实为您提供来自 System.Drawing 的位图,以便轻松访问各种图片格式的原始像素数据。其余的见http://en.wikipedia.org/wiki/Image_scaling

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 2012-06-23
      • 2010-12-27
      • 2011-11-17
      • 1970-01-01
      • 2016-02-11
      相关资源
      最近更新 更多