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

使用ASP.NET创建缩略图的"最佳"方法是什么?

如何解决《使用ASP.NET创建缩略图的"最佳"方法是什么?》经验,为你挑选了3个好方法。

故事:用户上传将添加到照片库的图像.作为上传过程的一部分,我们需要A)将图像存储在Web服务器的硬盘上,B)将图像的缩略图存储在Web服务器的硬盘上.

这里的"最佳"定义为

相对容易实现,理解和维护

产生合理质量的缩略图

性能和高质量的缩略图是次要的.



1> Sean Chamber..:

GetThumbnailImage可以工作,但如果你想要更好的质量,你可以为BitMap类指定你的图像选项,并将你加载的图像保存到那里.以下是一些示例代码:

Image photo; // your uploaded image

Bitmap bmp = new Bitmap(resizeToWidth, resizeToHeight);
graphic = Graphics.FromImage(bmp);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.DrawImage(photo, 0, 0, resizeToWidth, resizeToHeight);
imageToSave = bmp;

这提供了比开箱即用的GetImageThumbnail更好的质量



2> Huppie..:

我想你最好的解决方案是使用.NET Image类中的GetThumbnailImage.

// Example in C#, should be quite alike in ASP.NET
// Assuming filename as the uploaded file
using ( Image bigImage = new Bitmap( filename ) )
{
   // Algorithm simplified for purpose of example.
   int height = bigImage.Height / 10;
   int width = bigImage.Width / 10;

   // Now create a thumbnail
   using ( Image smallImage = image.GetThumbnailImage( width, 
                                                       height,
                                                       new Image.GetThumbnailImageAbort(Abort), IntPtr.Zero) )
   {
      smallImage.Save("thumbnail.jpg", ImageFormat.Jpeg);
   }
}


GetThumbnailImage仅适用于60x60或更小的缩略图 - 它使用相机生成的缩略图.还有[其他29个需要处理的边缘情况](http://nathanaeljones.com/163/20-image-resizing-pitfalls/),确保你了解它们......顺便说一下,[ imageresizing.net](http://imageresizing.net)项目现在是免费的,开源的,支持的...并正确处理29个陷阱.

3> 小智..:

使用上面的例子和其他几个地方的例子,这里有一个简单的功能(感谢Nathanael Jones和其他人在这里).

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

public static void ResizeImage(string FileNameInput, string FileNameOutput, double ResizeHeight, double ResizeWidth, ImageFormat OutputFormat)
{
    using (System.Drawing.Image photo = new Bitmap(FileNameInput))
    {
        double aspectRatio = (double)photo.Width / photo.Height;
        double boxRatio = ResizeWidth / ResizeHeight;
        double scaleFactor = 0;

        if (photo.Width < ResizeWidth && photo.Height < ResizeHeight)
        {
            // keep the image the same size since it is already smaller than our max width/height
            scaleFactor = 1.0;
        }
        else
        {
            if (boxRatio > aspectRatio)
                scaleFactor = ResizeHeight / photo.Height;
            else
                scaleFactor = ResizeWidth / photo.Width;
        }

        int newWidth = (int)(photo.Width * scaleFactor);
        int newHeight = (int)(photo.Height * scaleFactor);

        using (Bitmap bmp = new Bitmap(newWidth, newHeight))
        {
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;

                g.DrawImage(photo, 0, 0, newWidth, newHeight);

                if (ImageFormat.Png.Equals(OutputFormat))
                {
                    bmp.Save(FileNameOutput, OutputFormat);
                }
                else if (ImageFormat.Jpeg.Equals(OutputFormat))
                {
                    ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
                    EncoderParameters encoderParameters;
                    using (encoderParameters = new System.Drawing.Imaging.EncoderParameters(1))
                    {
                        // use jpeg info[1] and set quality to 90
                        encoderParameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 90L);
                        bmp.Save(FileNameOutput, info[1], encoderParameters);
                    }
                }
            }
        }
    }
}

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