asp.net mvc 从数据库中读取图片的实现代码

 更新时间:2010年05月21日 02:23:57   作者:  
今天搞了一天的MVC,在显示图片的时候老是出现问题,从网上搜索了好久,才找到解决方法。
首先是创建一个类,继承于ActionResult,记住要引用System.Web.Mvc命名空间,如下:
复制代码 代码如下:

public class ImageResult : ActionResult
{
public ImageFormat ContentType { get; set; }
public Image image { get; set; }
public string SourceName { get; set; }
public ImageResult(string _SourceName, ImageFormat _ContentType)
{
this.SourceName = _SourceName;
this.ContentType = _ContentType;
}
public ImageResult(Image _ImageBytes, ImageFormat _ContentType)
{
this.ContentType = _ContentType;
this.image = _ImageBytes;
}
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.Clear();
context.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
if (ContentType.Equals(ImageFormat.Bmp)) context.HttpContext.Response.ContentType = "image/bmp";
if (ContentType.Equals(ImageFormat.Gif)) context.HttpContext.Response.ContentType = "image/gif";
if (ContentType.Equals(ImageFormat.Icon)) context.HttpContext.Response.ContentType = "image/vnd.microsoft.icon";
if (ContentType.Equals(ImageFormat.Jpeg)) context.HttpContext.Response.ContentType = "image/jpeg";
if (ContentType.Equals(ImageFormat.Png)) context.HttpContext.Response.ContentType = "image/png";
if (ContentType.Equals(ImageFormat.Tiff)) context.HttpContext.Response.ContentType = "image/tiff";
if (ContentType.Equals(ImageFormat.Wmf)) context.HttpContext.Response.ContentType = "image/wmf";
if (image != null)
{
image.Save(context.HttpContext.Response.OutputStream, ContentType);
}
else
{
context.HttpContext.Response.TransmitFile(SourceName);
}
}
}

然后在 Controller类中创建一个Action.如下:
复制代码 代码如下:

public ActionResult GetPicture(int id)
{
ICategory server = new CategoryServer();
byte[] buffer = server.getCategoryPicture(id);
if (buffer != null)
{
MemoryStream stream = new MemoryStream(buffer);
System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
ImageResult result = new ImageResult(image, System.Drawing.Imaging.ImageFormat.Jpeg);
return result;
}
return View();
}

这样就可以显示图片了。
下面几种方法可以显示已经存在的图片
方法一:
复制代码 代码如下:

using System.IO;
public FileResult Image() {
string path = Server.MapPath("/Content/Images/Decorative/");
string filename = Request.Url.Segments[Request.Url.Segments.Length - 1].ToString();
// Uss Path.Combine from System.IO instead of StringBuilder.
string fullPath = Path.Combine(path, filename);
return(new FileResult(fullPath, "image/jpeg"));
}

方法二:
复制代码 代码如下:

public ActionResult Image(string id)
{
var dir = Server.MapPath("/Images");
var path = Path.Combine(dir, id + ".jpg");
return base.File(path, "image/jpg");
}

方法三:
复制代码 代码如下:

[AcceptVerbs(HttpVerbs.Get)]
[OutputCache(CacheProfile = "CustomerImages")]
public FileResult Show(int customerId, string imageName)
{
var path = string.Concat(ConfigData.ImagesDirectory, customerId, @"\", imageName);
return new FileStreamResult(new FileStream(path, FileMode.Open), "image/jpeg");
}

这三种都可以显示已经存在的图片并且我认为第三种方法可以修改为从数据库中读取图片显示。

相关文章

  • .NET CORE HttpClient的使用方法

    .NET CORE HttpClient的使用方法

    这篇文章主要给大家介绍了关于.NET CORE HttpClient的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者使用.NET CORE具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-07-07
  • 在ASP.NET中使用Session常见问题集锦

    在ASP.NET中使用Session常见问题集锦

    在ASP.NET中使用Session常见问题集锦...
    2007-08-08
  • ASP.NET Core全局异常处理

    ASP.NET Core全局异常处理

    这篇文章介绍了ASP.NET Core全局异常处理的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • ASP.NET MVC5网站开发之展示层架构(五)

    ASP.NET MVC5网站开发之展示层架构(五)

    这篇文章主要为大家详细介绍了ASP.NET MVC5网站开发之展示层架构,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • ASP.NET对IIS中的虚拟目录进行操作的代码

    ASP.NET对IIS中的虚拟目录进行操作的代码

    在做系统开发的过程中,我们经常会遇到用asp.net来操作IIS,如新建虚拟目录、更改虚拟目录的属性、删除虚拟目录等操作,现在分析如下
    2012-10-10
  • [Asp.Net Core] 浅谈Blazor Server Side

    [Asp.Net Core] 浅谈Blazor Server Side

    这篇文章主要介绍了[Asp.Net Core] Blazor Server Side 的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • mvc重定向方式详解

    mvc重定向方式详解

    这篇文章主要为大家详细介绍了mvc重定向的几种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • asp.net+ajax的Post请求实例

    asp.net+ajax的Post请求实例

    这篇文章主要介绍了asp.net+ajax的Post请求实现方法,实例分析了Ajax的发送post数据的原理与技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-01-01
  • ASP.NET中为TextBox中添加calendar.js示例代码

    ASP.NET中为TextBox中添加calendar.js示例代码

    为TextBox中添加calendar.js对于一些新手朋友确实有点难度,下面为大家介绍下ASP.NET中具体的实现方法
    2013-11-11
  • 为每个页面加上Session判断的小例子

    为每个页面加上Session判断的小例子

    这篇文章介绍了在每个页面加上Session判断的简单实例,有需要的朋友可以参考一下
    2013-10-10

最新评论