优化Web图像(C#和ASP.NET MVC)

问题描述 投票:0回答:1

我正在寻找一种方法来有效地为.NET中的网站准备大量图像

图像通常是大型的,未经编辑的,来自手机摄像头的2-5MB 4000x8000px怪物图像。

我想快速生成缩略图,并尽可能高效。 (不是为了减慢CPU)或用户的性能。

我还必须考虑缓存。

现代CMS系统正在使用您可以通过前端调用的Image预处理器。所以我也希望这样做,但我自己。在我的情况下,我不能在这里使用CMS。

这是我的代码:我有一个来自Helper类的静态辅助方法。每次我需要渲染图像时,我都会用剃刀调用它。

public static string GetImgThumbnail(string web_path, int _width = 0, int _height = 0)
{
        //Default parameters
        Image image;
        Image thumbnail;
        string thumb_url = "/img/noimg.png";

        try
        {
            // web_path example input "/images/helloworld.jpg"
            //system_path returns system path of Ogirinal image in the system f.e.: "C:\projects\websites\mysite\images\helloworld.jpg"
            string system_path = HttpContext.Current.Server.MapPath(web_path);
            image = Image.FromFile(system_path);

            //Original image dimensions
            int width = image.Width;
            int height = image.Height;

            //Get New image dimensions
            if(_height == 0)
            {
                if(_width == 0)
                {
                    _width = 700;
                }
                _height = (_width * height) / width;
            }

            if (_width == 0)
            {
                if (_height == 0)
                {
                    _height = 700;
                }
                _width = (_height * width) / height;
            }

            //Generate Thumbnail, passing in new dimensions
            thumbnail = image.GetThumbnailImage(_width, _height, null, IntPtr.Zero);

            //Find out how to call newly created thumbnail.
            //Original image example name = "/images/helloworld.jpg" thumbnail would be "/images/helloworld_thumb_700x250.jpg" or analogical with .png or JPEG etc...
            //Suffix should be "_thumb_{width}x{height}"
            string suffix = string.Format("_thumb_{0}x{1}", _width.ToString(),_height.ToString());

            var bigImgFilename = String.Format("{0}{1}",
                     Path.GetFileNameWithoutExtension(system_path), Path.GetExtension(system_path));

            var newImgFilename = String.Format("{0}{1}{2}",
                     Path.GetFileNameWithoutExtension(system_path), suffix, Path.GetExtension(system_path));

            //New system path of new Thumbnail example: "C:\projects\websites\mysite\images\helloworld_thumb_700x250.jpg"
            var newpath = system_path.Replace(bigImgFilename, newImgFilename);

            //Set new web path, expect this to be: "/images/helloworld_thumb_700x250.jpg"
            thumb_url = web_path.Replace(bigImgFilename, newImgFilename);

            //Check if file exists, no need to overrite if file exists.
            if (!File.Exists(newpath)) 
            { 
                thumbnail.Save(newpath);
            }

        }
        catch (Exception exc)
        {
            // If something goes wrong, just return backup image.
            thumb_url = "/img/noimg.png";
        }

        // return thumbnail 
        return thumb_url;
}

很想听到一些提示/建议,或者我是否走在正确的道路上,或者我应该以不同的方式做到这一点?

c# asp.net-mvc image
1个回答
2
投票

因此,在您的代码中,您首先计算缩略图。然后计算文件名并检查是否需要缩略图计算。

我会以不同的顺序做:

  1. 加载图像(因为你需要它来计算新的文件名)
  2. 计算新文件名
  3. 检查文件是否已存在
  4. 如果文件已经存在,请使用它
  5. 如果没有,请生成缩略图。

在代码中,这将看起来像下面这样:

public static string GetImgThumbnail(string web_path, int _width = 0, int _height = 0)
{

    [...]
    string system_path = HttpContext.Current.Server.MapPath(web_path);
    image = Image.FromFile(system_path);

    // calculate new width and height
    [...]

    // calculate new filename
    [...]
        //New system path of new Thumbnail example: "C:\projects\websites\mysite\images\helloworld_thumb_700x250.jpg"
        var newpath = system_path.Replace(bigImgFilename, newImgFilename);

        //Set new web path, expect this to be: "/images/helloworld_thumb_700x250.jpg"
        thumb_url = web_path.Replace(bigImgFilename, newImgFilename);

        //Check if file exists, no need to overrite if file exists.
        if (!File.Exists(newpath)) 
        { 
            //Generate Thumbnail, passing in new dimensions
            thumbnail = image.GetThumbnailImage(_width, _height, null, IntPtr.Zero);
            thumbnail.Save(newpath);
        }

我的观点是:加载图像并计算大小不会占用太多资源。调整大小部分是CPU的重量级。因此,如果您只在必要时转换图像,您将获得更快的响应。


您可以考虑以下几点:

每次我需要渲染图像时,我都会用剃刀调用它。

这将在每个图像视图上生成缩略图。这非常重,很可能不是你想要的。考虑仅创建一次缩略图,将其保存到磁盘并开始使用预渲染版本。

下一期:

//Check if file exists, no need to overrite if file exists.
if (!File.Exists(newpath)) 
{ 
    thumbnail.Save(newpath);
}

首先计算缩略图,然后检查计算是否已完成。它应该是反过来的。

© www.soinside.com 2019 - 2024. All rights reserved.