C# 如何在ASP.NET MVC中从数据库加载图像?

C# 如何在ASP.NET MVC中从数据库加载图像?,c#,.net,asp.net,sql,asp.net-mvc,C#,.net,Asp.net,Sql,Asp.net Mvc,我已在数据库中保存了一个图像,并希望将其显示给用户 存储图像的表如下所示: Images ------ ImageData Byte ImageName String ContentType String 如何在视图中加载和显示它?在Image controller类中: public ActionResult ProfileImage(string userName) { var imageByteArray = // get

我已在数据库中保存了一个图像,并希望将其显示给用户

存储图像的表如下所示:

        Images
        ------
ImageData      Byte
ImageName      String
ContentType    String

如何在视图中加载和显示它?

在Image controller类中:

public ActionResult ProfileImage(string userName)
{
   var imageByteArray = // get image bytes from DB corresponding to userName
   string contentType = // get image content type from DB for example "image/jpg"
   string fileName = // get image file name from DB

   return File(imageByteArray, contentType, fileName);
}
鉴于:

<img src='/Image/ProfileImage/yourUserName' alt='Profile image' />

您还可以在位图中加载图像,并应用诸如大小调整、旋转等更改。如果您这样做,请考虑将图像保存为PNG,因为GDI+(System .DRAP)可以保持这种格式的最佳质量和透明度。缓存动态图像也是一种很好的做法

在图像控制器类中:

public ActionResult ProfileImage(string userName)
{
   var imageByteArray = // get image bytes from DB corresponding to userName
   string contentType = // get image content type from DB for example "image/jpg"
   string fileName = // get image file name from DB

   return File(imageByteArray, contentType, fileName);
}
鉴于:

<img src='/Image/ProfileImage/yourUserName' alt='Profile image' />

您还可以在位图中加载图像,并应用诸如大小调整、旋转等更改。如果您这样做,请考虑将图像保存为PNG,因为GDI+(System .DRAP)可以保持这种格式的最佳质量和透明度。缓存动态图像也是一种很好的做法

ImageData=Convert.ToByte[](读卡器[“@ImageData]”);如何读取字节类型的信息?在第一个“]”之后我有点不对劲。这是什么意思15??imageName?抱歉,它应该是用户名(或图像名称,如果您决定的话)ImageData=Convert.ToByte[](reader[“@ImageData”]);如何读取字节类型的信息?在第一个“]”之后我有点不对劲。这是什么意思15??imageName?对不起,它应该是用户名(如果您决定的话,也可以是图像名)