我有一个asp.net mvc 5应用程序,其中包含一个存储照片的数据库。我正在尝试阅读照片并调整其大小以便在员工个人资料中显示。
我对asp.net mvc和c#
都很新我设置了以下控制器,但是当我在img标签中使用控制器的链接时,我没有显示图像。
任何帮助都将不胜感激。
public ActionResult Index(int id)
{
Staff staff = db.StaffList.Find(id);
if (staff.Photo != null)
{
var img = new WebImage(staff.Photo);
img.Resize(100, 100, true, true);
var imgBytes = img.GetBytes();
return File(imgBytes, "image/" + img.ImageFormat);
}
else
{
return null;
}
}
答案 0 :(得分:0)
环顾四周似乎对WebImage类有很多不满,并且它有一些突出的错误。我决定使用名为ImageProcessor的nuget包,而不是尝试编写自己的。这对我来说似乎效率很低,但我现在没有更好的答案,这并没有大量使用,所以我要继续这样做,继续前进。
在此处发布,以防其他任何人在与类似的东西挣扎。
public ActionResult Index(int id, int? height, int? width)
{
int h = (height ?? 325);
int w = (width ?? 325);
Staff staff = db.StaffList.Find(id);
if (staff == null)
{
return new HttpNotFoundResult();
}
if (staff.Photo != null)
{
Size size = new Size(w, h);
byte[] rawImg;
using (MemoryStream inStream = new MemoryStream(staff.Photo))
{
using (MemoryStream outStream = new MemoryStream())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(inStream)
.Constrain(size)
.Format(format)
.Save(outStream);
}
rawImg = outStream.ToArray();
}
}
return new FileContentResult(rawImg, "image/jpeg");
}
else
{
return null;
}
}
答案 1 :(得分:0)
我将使用ImageProcessor来回答这个问题,因为您自己的答案使用了库。免责声明。我也是图书馆的作者。
你真的不是最好使用ActionResult来处理图像,因为它会非常低效。它在管道中运行得太晚了,你将没有缓存。
您最好安装Imageprocessor.Web软件包并实现自己版本的IImageService
接口,以便从数据库中提供图像。 (在那个说明中,将图像存储在数据库中,除非你使用blob存储也不是明智的)
实现IImageService
接口允许您利用具有特定前缀的库中的url api来标识要使用的图像服务。例如,远程图像请求以remote.axd
为前缀,告诉IageProcessor.Web执行RemoteImageService
实现。
这样可以缓存您的图像,以便从缓存中返回后续请求,而不是在每次请求时重新处理。
以下是LocalFileImageService
的完整实现,它是库的默认服务。这应该可以作为如何实施自己的服务的指南。
namespace ImageProcessor.Web.Services
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Web;
using ImageProcessor.Web.Helpers;
/// <summary>
/// The local file image service for retrieving images from the
/// file system.
/// </summary>
public class LocalFileImageService : IImageService
{
/// <summary>
/// The prefix for the given implementation.
/// </summary>
private string prefix = string.Empty;
/// <summary>
/// Gets or sets the prefix for the given implementation.
/// <remarks>
/// This value is used as a prefix for any image requests
/// that should use this service.
/// </remarks>
/// </summary>
public string Prefix
{
get
{
return this.prefix;
}
set
{
this.prefix = value;
}
}
/// <summary>
/// Gets a value indicating whether the image service
/// requests files from
/// the locally based file system.
/// </summary>
public bool IsFileLocalService
{
get
{
return true;
}
}
/// <summary>
/// Gets or sets any additional settings required by the service.
/// </summary>
public Dictionary<string, string> Settings { get; set; }
/// <summary>
/// Gets or sets the white list of <see cref="System.Uri"/>.
/// </summary>
public Uri[] WhiteList { get; set; }
/// <summary>
/// Gets a value indicating whether the current request
/// passes sanitizing rules.
/// </summary>
/// <param name="path">
/// The image path.
/// </param>
/// <returns>
/// <c>True</c> if the request is valid; otherwise, <c>False</c>.
/// </returns>
public bool IsValidRequest(string path)
{
return ImageHelpers.IsValidImageExtension(path);
}
/// <summary>
/// Gets the image using the given identifier.
/// </summary>
/// <param name="id">
/// The value identifying the image to fetch.
/// </param>
/// <returns>
/// The <see cref="System.Byte"/> array containing the image data.
/// </returns>
public async Task<byte[]> GetImage(object id)
{
string path = id.ToString();
byte[] buffer;
// Check to see if the file exists.
// ReSharper disable once AssignNullToNotNullAttribute
FileInfo fileInfo = new FileInfo(path);
if (!fileInfo.Exists)
{
throw new HttpException(404, "No image exists at " + path);
}
using (FileStream file = new FileStream(path,
FileMode.Open,
FileAccess.Read,
FileShare.Read,
4096,
true))
{
buffer = new byte[file.Length];
await file.ReadAsync(buffer, 0, (int)file.Length);
}
return buffer;
}
}
}