我可以使用哪个免费图像大小调整库来调整大小并可能提供图像?

Bur*_*jua 10 c# asp.net image image-resizing

我使用过Umbraco,那里有非常好的ImageGen库,可以"动态"调整图像大小并兑现已处理的图像.

有没有类似的东西我可以在Umbraco外面使用?

(我以为我可以在没有Umbraco的情况下使用ImageGen,但看起来它不是免费的)

谢谢

Pau*_*ean 11

我意识到这个问题已经很老了,但是因为我偶然发现它,我想我应该发布我的发现.

http://imageprocessor.org/似乎是一个相对较新的免费开源库,并且具有相当多的功能!


Lil*_*ver 5

我编写了 ImageResizer HttpModule,它是免费的,开源的,并通过StackOverflow imageresizer标签自由支持.

它有39个插件,其中一些需要许可证,但所有的所有源代码都可以在http://imageresizing.net/download上找到.

适用于所有.NET CMS,包括Umbraco.

与ImageGen不同,它可以扩展到数百万张图像.

  • 更新 - ImageResizer 不再免费,每月 170 美元 + (3认同)

Ric*_*kNZ 3

.NET Framework 包含对图像大小调整的支持。

下面是我的书《Ultra-Fast ASP.NET》中的一些示例代码:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

namespace Samples
{
    public class ImageResizer
    {
        private static ImageCodecInfo jpgEncoder;

        public static void ResizeImage(string inFile, string outFile, double maxDimension, long level)
        {
            //
            // Load via stream rather than Image.FromFile to release the file
            // handle immediately
            //
            using (Stream stream = new FileStream(inFile, FileMode.Open))
            {
                using (Image inImage = Image.FromStream(stream))
                {
                    double width;
                    double height;

                    if (inImage.Height < inImage.Width)
                    {
                        width = maxDimension;
                        height = (maxDimension / (double)inImage.Width) * inImage.Height;
                    }
                    else
                    {
                        height = maxDimension;
                        width = (maxDimension / (double)inImage.Height) * inImage.Width;
                    }
                    using (Bitmap bitmap = new Bitmap((int)width, (int)height))
                    {
                        using (Graphics graphics = Graphics.FromImage(bitmap))
                        {
                            graphics.SmoothingMode = SmoothingMode.HighQuality;
                            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            graphics.DrawImage(inImage, 0, 0, bitmap.Width, bitmap.Height);
                            if (inImage.RawFormat.Guid == ImageFormat.Jpeg.Guid)
                            {
                                if (jpgEncoder == null)
                                {
                                    ImageCodecInfo[] ici = ImageCodecInfo.GetImageDecoders();
                                    foreach (ImageCodecInfo info in ici)
                                    {
                                        if (info.FormatID == ImageFormat.Jpeg.Guid)
                                        {
                                            jpgEncoder = info;
                                            break;
                                        }
                                    }
                                }
                                if (jpgEncoder != null)
                                {
                                    EncoderParameters ep = new EncoderParameters(1);
                                    ep.Param[0] = new EncoderParameter(Encoder.Quality, level);
                                    bitmap.Save(outFile, jpgEncoder, ep);
                                }
                                else
                                    bitmap.Save(outFile, inImage.RawFormat);
                            }
                            else
                            {
                                //
                                // Fill with white for transparent GIFs
                                //
                                graphics.FillRectangle(Brushes.White, 0, 0, bitmap.Width, bitmap.Height);
                                bitmap.Save(outFile, inImage.RawFormat);
                            }
                        }
                    }
                }
            }
        }

        public static void GetImageSize(string inFile, out int width, out int height)
        {
            using (Stream stream = new FileStream(inFile, FileMode.Open))
            {
                using (Image src_image = Image.FromStream(stream))
                {
                    width = src_image.Width;
                    height = src_image.Height;
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)