生成图像缩略图ASP.NET? [英] Generating image thumbnails in ASP.NET?

查看:168
本文介绍了生成图像缩略图ASP.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是.NET生成缩略图的速度最快,更可靠的方法是什么?我需要得到任何图像,C​​OM preSS它JPEG和调整其大小。

我已经看到了使用GDI +的几个例子,一些非自由的组件,我记得WPF有大约成像一些好东西。 GDI +是pretty的旧的和WPF的东西也许对服务器环境没有好处,但。

这有一个上完全信任运行ASP.NET MVC应用程序的工作,如果可能的话,同步进行。

你有什么建议?

更新:

根据<α

href="http://stackoverflow.com/questions/5882307/generating-image-thumbnails-in-asp-net/5882623#5882623">Mantorok's回答我已经制定了这个例子,但它仍然是GDI +,它崩溃,如果我尝试用一​​个大的图像:

 公共无效GenerateThumbnail(字符串文件名,的Int32?desiredWidth,
    INT32? desiredHeight,Int64的质量,流S)
{
    使用(形象画像= Image.FromFile(文件名))
    {
        的Int32宽度= 0,高度= 0;

        如果((desiredHeight.HasValue&安培;!&安培;!desiredWidth.HasValue)||
            (desiredHeight.HasValue&安培;&安培; desiredWidth.HasValue))
            抛出新的ArgumentException(
                你必须指定所需的宽度或所需的高度);

        如果(desiredHeight.HasValue)
        {
            宽度=(desiredHeight.Value * image.Width)/ image.Height;
            身高= desiredHeight.Value;
        }
        其他
        {
            身高=(desiredWidth.Value * image.Height)/ image.Width;
            宽度= desiredWidth.Value;
        }

        使用(VAR newImage =新位图(宽,高))
        使用(VAR图形= Graphics.FromImage(newImage))
        采用(恩coderParameter qualityParam =
            新恩coderParameter(System.Drawing.Imaging.En coder.Quality,
                质量))
        采用(恩coderParameters EN coderParams =新恩coderParameters(1))
        {
            graphics.DrawImage(图像,0,0,宽度,高度);
            图片codecInfo JPEG codeC =图片codecInfo.GetImageEn codeRS()。
                单(E =&GT; e.MimeType.Equals(图像/ JPEG,
                    StringComparison.Ordinal));
            EN coderParams.Param [0] = qualityParam;
            newImage.Save(S,JPEG codeC,EN coderParams);
        }
    }
}
 

解决方案

有关密集型服务器端code,我建议你使用其他技术相比,还没有被设计为通过块来处理图像块GDI +(在流式方式)。

您可以使用 Windows图像处理组件或WPF完成这个任务。这里是如何做到这一点在一个快速和一个非常好的例子 - 更重要的 - 可伸缩的方式在这里:

<一个href="http://weblogs.asp.net/bleroy/archive/2010/05/03/the-fastest-way-to-resize-images-from-asp-net-and-it-s-more-supported-ish.aspx">The最快的方式从ASP.NET调整图像。而且它(更多)支持十岁上下。

What is the fastest and more reliable way of generating thumbnails in .NET? I need to get any image, compress it in JPEG and resize it.

I've seen several examples with GDI+, some non-free components and I remember WPF has some good stuff about imaging. GDI+ is pretty old and the WPF stuff maybe has no benefits on a server environment though.

This has to work in a ASP.NET MVC application that runs on full trust, and if possible, synchronously.

What would you recommend?

UPDATE:

Based on Mantorok's answer I have worked out this example, but it's still GDI+, and it crashes if I try with a large image:

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);
        }
    }
}

解决方案

For intensive server-side code, I suggest you use other techniques than GDI+ that has not been designed to handle images chunk by chunk (in a streaming manner).

You can use Windows Imaging Component or WPF for this task. There is a very good example on how to do this in a fast and - more important - scalable manner here:

The fastest way to resize images from ASP.NET. And it’s (more) supported-ish.

这篇关于生成图像缩略图ASP.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆