从字节到优化的网页演示文稿的图像
-
03-10-2019 - |
题
我将存储的图像的数据作为字节[] array在数据库上;然后,我将其转换为system.drawing.image如下所示的代码;
public System.Drawing.Image CreateImage(byte[] bytes)
{
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bytes);
System.Drawing.Image image = System.Drawing.Image.FromStream(memoryStream);
return image;
}
(*)另一方面,我计划在客户滚动下页面上显示ASP.NET页面上的图像列表。用户在他/她看到的页面上越来越少,看到的照片越多。因此,这意味着快速的页面负载和丰富的用户体验。 (您可能会在www.mashable.com上看到我的意思,只需在向下滚动时注意照片的新负载即可。)
此外,从上面的方法中返回的imgae对象,如何使用上面的(*)条件在循环中以循环表示。
关于BK
解决方案
好吧,我认为每次需要图像时,主瓶颈实际上都会击中数据库。 (特别是考虑到许多访问该网站的用户。)
我会采用以下解决方案:
- 数据库将以原始质量存储图像;
- .ASHX处理程序将以各种所需的分辨率(例如图标的32x32像素,缩略图等48x48等)在文件系统上缓存图像。 (在 这 示例显示了如何通过ASHX处理程序返回图像)
- 实际页面将指向.ASHX页面以获取图像。 (像
<img scr="GetImage.ashx?ID=324453&Size=48" />
)
更新:
因此,处理程序中的实际工作流将就像:
public void ProcessRequest (HttpContext context)
{
// Create path of cached file based on the context passed
int size = Int32.Parse(context.Request["Size"]);
// For ID Guids are possibly better
// but it can be anything, even parameter you need to pass
// to the web service in order to get those bytes
int id = Int32.Parse(context.Request["Id"]);
string imagePath = String.Format(@"images/cache/{0}/{1}.png", size, id);
// Check whether cache image exists and created less than an hour ago
// (create it if necessary)
if (!File.Exists(imagePath)
|| File.GetLastWriteTime(imagePath) < DateTime.Now.AddHours(-1))
{
// Get the file from the web service here
byte[] imageBytes = ...
// Save as a file
using (var memoryStream = new MemoryStream(imageBytes))
using (var outputStream = File.OpenWrite(imagePath))
Image.FromStream(memoryStream).Save(outputStream);
}
context.Response.ContentType = "image/png";
context.Response.WriteFile(imagePath);
}
不隶属于 StackOverflow