简体   繁体   English

ASP.NET上传后处理缩略图的最佳方法

[英]ASP.NET the best way to handle thumbnail images after uploading

I've read about creating a HttpHandler to call each time I want to show a thumbnail where it will perfom the resizing for me. 我已经读过有关创建HttpHandler的信息,每次我想显示缩略图时都会调用它,以便为我调整大小。 I've also heard about some other solutions, but I was wondering which solution would be the best for a social networking website where thumbnails are shown all over the place on each page and everywhere. 我还听说过其他一些解决方案,但是我想知道哪种解决方案最适合在每个页面上各处显示缩略图的社交网站。

Would it be good to resize and save the image on the disk after the origianl file has been uploaded? 上传origianl文件后,调整大小并将图像保存在磁盘上会很好吗? What's the best way to reference these images? 引用这些图像的最佳方法是什么?

Does anyone have any advice for me? 有人对我有什么建议吗? Thank you. 谢谢。

Would it be good to resize and save the image on the disk after the origianl file has been uploaded? 上传origianl文件后,调整大小并将图像保存在磁盘上会很好吗? What's the best way to reference these images? 引用这些图像的最佳方法是什么?

Definitely, that's what Twitter does, for example, and most websites when thumbnails need to be displayed. 绝对是这样,例如,Twitter和大多数需要显示缩略图的网站都这样做。 This is time consuming. 这很费时间。 You don't want your user to sit idle while you do this on every image every time. 您不希望您的用户每次在每个图像上都闲着时都闲着。

Store the thumbnails on disk and keep a reference to the thumbnails on the database. 将缩略图存储在磁盘上,并保留对数据库上缩略图的引用。 Or store them on the db itself. 或将它们存储在数据库本身上。 I don't want to get into that debate about disk vs DB but just don't resize them every time. 我不想卷入有关磁盘与数据库的争论,只是不想每次都调整它们的大小。 It should be done ONCE. 应该一次完成。

Here is some resize Code if you need it, the first you set a max height and width and it created a thumbnail with the same aspect retio as the original that do now violate either max. 如果需要的话,这是一些调整大小的代码,首先设置最大高度和宽度,然后创建一个缩略图,该缩略图的纵横比与原始纵横比相同,但现在违反了其中一个最大值。

The second, if you know the actual size of the final image and don't need to worry about aspect ratios is fatr simpler. 第二,如果您知道最终图像的实际大小,而不必担心纵横比会更简单。

    public Image Thumbnail(Image FullsizeImage, int MaxHeight, int MaxWidth)
    {

        try
        {
            // This has to be here or for some reason this resize code will
            // resize an internal Thumbnail and wil stretch it instead of shrinking
            // the fullsized image and give horrible results
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);


            System.Drawing.Image NewImage;


            if (!((MaxWidth < FullsizeImage.Width) || (MaxHeight < FullsizeImage.Height)))
                NewImage = FullsizeImage;
            else
            {
                float HeightRatio = 1;
                float WidthRatio = 1;
                HeightRatio = (float)FullsizeImage.Width / FullsizeImage.Height;
                WidthRatio = (float)FullsizeImage.Height / FullsizeImage.Width;


                float DrawHeight = (float)FullsizeImage.Height;
                float DrawWidth = (float)FullsizeImage.Width;

                if (MaxHeight < FullsizeImage.Height)
                {
                    DrawHeight = (float)MaxHeight;
                    DrawWidth = MaxHeight * HeightRatio;
                }
                if (MaxWidth < DrawWidth)
                {
                    DrawWidth = MaxWidth;
                    DrawHeight = DrawWidth * WidthRatio;
                }

                NewImage = FullsizeImage.GetThumbnailImage((int)(DrawWidth),
                       (int)(DrawHeight), null, IntPtr.Zero);
            }

            return NewImage;

            // To return a byte array for saving in a db 
            //ms = new MemoryStream();

            //NewImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

            //NewImage.Dispose();
            //FullsizeImage.Dispose();

            //return ms.ToArray();
        }
        catch
        {
            return null;
        }
        finally
        {

        }
    }

    public Image Resize(Image OrigImage, int NewHeight, int NewWidth)
    {
        if (OrigImage != null)
        {                
            Bitmap bmp = new Bitmap(OrigImage, new Size(NewWidth, NewHeight));
            bmp.SetResolution(this.ImageResolution, this.ImageResolution);
            Graphics g = Graphics.FromImage(bmp);
            return bmp;
        }
        else
        {
            return null;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM