在ASP.NET中生成图像缩略图?

vto*_*ola 16 .net asp.net wpf gdi+ image-processing

在.NET中生成缩略图的最快,最可靠的方法是什么?我需要获取任何图像,以JPEG格式压缩并调整大小.

我已经看过GDI +的几个例子,一些非自由组件,我记得WPF有一些关于成像的好东西.GDI +已经很老了,WPF的东西可能在服务器环境中没有任何好处.

这必须在ASP.NET MVC应用程序中运行,该应用程序在完全信任的情况下运行,并且如果可能的话,同步运行.

你会推荐什么?

更新:

基于Mantorok的回答我已经得出了这个例子,但它仍然是GDI +,如果我尝试使用大图像它会崩溃:

public void GenerateThumbnail(String filename, Int32? desiredWidth, 
    Int32? desiredHeight, Int64 quality, Stream s)
{
    using (Image image = Image.FromFile(filename))
    {
        Int32 width=0, height=0;

        if ((!desiredHeight.HasValue && !desiredWidth.HasValue) ||
            (desiredHeight.HasValue && desiredWidth.HasValue))
            throw new ArgumentException(
                "You have to specify a desired width OR a desired height");

        if (desiredHeight.HasValue)
        {
            width = (desiredHeight.Value * image.Width) / image.Height;
            height = desiredHeight.Value;
        }
        else
        {
            height = (desiredWidth.Value * image.Height) / image.Width;
            width = desiredWidth.Value;
        }

        using (var newImage = new Bitmap(width, height))
        using (var graphics = Graphics.FromImage(newImage))
        using (EncoderParameter qualityParam = 
            new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 
                quality))
        using (EncoderParameters encoderParams = new EncoderParameters(1))
        {
            graphics.DrawImage(image, 0, 0, width, height);
            ImageCodecInfo jpegCodec = ImageCodecInfo.GetImageEncoders().
                Single(e => e.MimeType.Equals("image/jpeg", 
                    StringComparison.Ordinal));
            encoderParams.Param[0] = qualityParam;
            newImage.Save(s, jpegCodec, encoderParams);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*ett 6

多年来我一直很好:

public static void CreateThumbnail(string filename, int desiredWidth, int desiredHeight, string outFilename)
{
    using (System.Drawing.Image img = System.Drawing.Image.FromFile(filename))
    {
        float widthRatio = (float)img.Width / (float)desiredWidth;
        float heightRatio = (float)img.Height / (float)desiredHeight;
        // Resize to the greatest ratio
        float ratio = heightRatio > widthRatio ? heightRatio : widthRatio;
        int newWidth = Convert.ToInt32(Math.Floor((float)img.Width / ratio));
        int newHeight = Convert.ToInt32(Math.Floor((float)img.Height / ratio));
        using (System.Drawing.Image thumb = img.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailImageAbortCallback), IntPtr.Zero))
        {
            thumb.Save(outFilename, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }
}

public static bool ThumbnailImageAbortCallback()
{
    return true;
}
Run Code Online (Sandbox Code Playgroud)


Sim*_*ier 5

对于密集型服务器端代码,我建议您使用除GDI +之外的其他技术,这些技术尚未设计为按块(以流方式)处理图像块.

您可以使用Windows Imaging Component或WPF执行此任务.关于如何以一种快速且更重要的可扩展方式执行此操作有一个非常好的示例:

从ASP.NET调整图像大小的最快方法.并且(更多)支持 - ish.