优化web映像(C#和ASP.NET MVC)
我正在寻找一种方法,以有效地准备大量的图片网站在.NET 图像通常是大型的、未经编辑的、2-5MB 4000x8000px的手机摄像头拍摄的怪物图像 我想快速生成缩略图,并尽可能高效。(不降低CPU速度)或用户的性能优化web映像(C#和ASP.NET MVC),c#,asp.net-mvc,image,C#,Asp.net Mvc,Image,我正在寻找一种方法,以有效地准备大量的图片网站在.NET 图像通常是大型的、未经编辑的、2-5MB 4000x8000px的手机摄像头拍摄的怪物图像 我想快速生成缩略图,并尽可能高效。(不降低CPU速度)或用户的性能 我也必须考虑缓存。 现代CMS系统使用图像预处理器,您可以通过前端调用。所以我也想做这样的东西,但是我自己的。因为在我的情况下,我不能在这里使用CMS 这是我的代码:我有一个来自helper类的静态helper方法。 每次需要渲染图像时,我都会在razor中调用它 public s
我也必须考虑缓存。
现代CMS系统使用图像预处理器,您可以通过前端调用。所以我也想做这样的东西,但是我自己的。因为在我的情况下,我不能在这里使用CMS 这是我的代码:我有一个来自helper
类的静态helper方法。
每次需要渲染图像时,我都会在razor中调用它
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;
}
希望听到一些提示/建议,或者我是否走上了正确的道路,或者我应该以不同的方式进行操作?因此,在您的代码中,您首先计算缩略图。然后计算文件名并检查是否需要进行缩略图计算 我会按照不同的顺序做:
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来说是一个沉重的负担。因此,如果只在必要时变换图像,您将获得更快的响应
您可以考虑以下几点: 每次需要渲染图像时,我都会在razor中调用它
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;
}
这将在每个图像视图上生成缩略图。这非常占用cpu,很可能不是您想要的。考虑只创建缩略图一次,将其保存到磁盘并开始使用预渲染的版本。
下一期:
首先计算缩略图,然后检查计算是否已经完成。它应该是另一种方式。因此在代码中,您首先计算缩略图。然后计算文件名并检查是否需要进行缩略图计算 我会按照不同的顺序做:
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来说是一个沉重的负担。因此,如果只在必要时变换图像,您将获得更快的响应
您可以考虑以下几点: 每次需要渲染图像时,我都会在razor中调用它
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;
}
这将在每个图像视图上生成缩略图。这非常占用cpu,很可能不是您想要的。考虑只创建缩略图一次,将其保存到磁盘并开始使用预渲染的版本。
下一期:
首先计算缩略图,然后检查计算是否已经完成。反之亦然。看看ImageResizer项目,我不明白你的代码在计算宽度和高度时应该做什么,但第二组ifs不可能是正确的,因为它检查宽度是否为0 twiceThanks,这是一个打字错误,我纠正了我的代码示例,但有一些实际的想法:你可以考虑一个云服务。一旦请求,他们将从您的位置下载图像,调整大小(其他可能的操作,如裁剪、应用过滤器、压缩)并返回缓存副本(从现在开始),所有操作都只需更改URL即可完成。我知道,人们可以编写所有程序,但这些服务有时非常有用。看看ImageResizer项目,我不明白你的代码在计算宽度和高度时应该做什么,但第二组ifs不可能是正确的,因为它检查宽度是否为0 twiceThanks,这是一个打字错误,我纠正了我的代码示例,但有一些实际的想法:你可以考虑一个云服务。一旦请求,他们将从您的位置下载图像,调整大小(其他可能的操作,如裁剪、应用过滤器、压缩)并返回缓存副本(从现在开始),所有操作都只需更改URL即可完成。我知道,人们可以对一切进行编程,但这些服务有时非常有用。虽然这些观点可能有效,但这并不试图回答OP的问题。这应该是一个评论,而不是一个回答。嗨,罗尔斯图法赫勒,你有一些好的观点,完全同意。但首先调用File.Exists()的邮件问题是,我需要获取新路径,只有在通过OldPath(也称为web_Path)后才能获取新路径。所以我需要先分解它,然后我知道我需要查找(或创建)什么文件,它有什么扩展名等等。。。因为用户可能会添加TIFF文件格式,这是我完全没有想到的。做了一些修改。“我越来越近了吗?”罗尔施陶法赫你的回答对这个案子很有帮助,它确实解决了我的案子。对此表示衷心的感谢!:)虽然这些观点可能有效,但这并不试图回答OP的问题。这应该是一个评论,而不是一个回答。嗨,罗尔斯图法赫勒,你有一些好的观点,完全同意。但首先调用File.Exists()的邮件问题是,我需要获取新路径,只有在通过OldPath(也称为web_Path)后才能获取新路径。所以我需要先分解它,然后我知道我需要查找(或创建)什么文件,它有什么扩展名等等。。。因为用户可能会添加TIFF文件格式,这是我完全没有想到的。做了一些修改。“我越来越近了吗?”罗尔施陶法赫你的回答对这个案子很有帮助,它确实解决了我的案子。对此表示衷心的感谢!:)