本文转自:http://www.cnblogs.com/mayt/archive/2010/05/20/1740358.html

  1. 首先是创建一个类,继承于ActionResult,记住要引用System.Web.Mvc命名空间,如下:
  2.  
  3. public class ImageResult : ActionResult
  4. {
  5. public ImageFormat ContentType { get; set; }
  6. public Image image { get; set; }
  7. public string SourceName { get; set; }
  8.  
  9. public ImageResult(string _SourceName, ImageFormat _ContentType)
  10. {
  11. this.SourceName = _SourceName;
  12. this.ContentType = _ContentType;
  13. }
  14.  
  15. public ImageResult(Image _ImageBytes, ImageFormat _ContentType)
  16. {
  17. this.ContentType = _ContentType;
  18. this.image = _ImageBytes;
  19. }
  20.  
  21. public override void ExecuteResult(ControllerContext context)
  22. {
  23. context.HttpContext.Response.Clear();
  24. context.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
  25. if (ContentType.Equals(ImageFormat.Bmp)) context.HttpContext.Response.ContentType = "image/bmp";
  26. if (ContentType.Equals(ImageFormat.Gif)) context.HttpContext.Response.ContentType = "image/gif";
  27. if (ContentType.Equals(ImageFormat.Icon)) context.HttpContext.Response.ContentType = "image/vnd.microsoft.icon";
  28. if (ContentType.Equals(ImageFormat.Jpeg)) context.HttpContext.Response.ContentType = "image/jpeg";
  29. if (ContentType.Equals(ImageFormat.Png)) context.HttpContext.Response.ContentType = "image/png";
  30. if (ContentType.Equals(ImageFormat.Tiff)) context.HttpContext.Response.ContentType = "image/tiff";
  31. if (ContentType.Equals(ImageFormat.Wmf)) context.HttpContext.Response.ContentType = "image/wmf";
  32. if (image != null)
  33. {
  34. image.Save(context.HttpContext.Response.OutputStream, ContentType);
  35. }
  36. else
  37. {
  38. context.HttpContext.Response.TransmitFile(SourceName);
  39. }
  40. }
  41. }
  42.  
  43. 然后在 Controller类中创建一个Action.如下:
  44.  
  45. public ActionResult GetPicture(int id)
  46. {
  47. ICategory server = new CategoryServer();
  48. byte[] buffer = server.getCategoryPicture(id);
  49. if (buffer != null)
  50. {
  51. MemoryStream stream = new MemoryStream(buffer);
  52. System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
  53. ImageResult result = new ImageResult(image, System.Drawing.Imaging.ImageFormat.Jpeg);
  54. return result;
  55. }
  56. return View();
  57. }
  58.  
  59. 这样就可以显示图片了。
  60.  
  61. 下面几种方法可以显示已经存在的图片
  62.  
  63. 方法一:
  64.  
  65. using System.IO;
  66.  
  67. public FileResult Image() {
  68. string path = Server.MapPath("/Content/Images/Decorative/");
  69. string filename = Request.Url.Segments[Request.Url.Segments.Length - ].ToString();
  70.  
  71. // Uss Path.Combine from System.IO instead of StringBuilder.
  72. string fullPath = Path.Combine(path, filename);
  73.  
  74. return(new FileResult(fullPath, "image/jpeg"));
  75.  
  76. }
  77. 方法二:
  78. public ActionResult Image(string id)
  79. {
  80. var dir = Server.MapPath("/Images");
  81. var path = Path.Combine(dir, id + ".jpg");
  82. return base.File(path, "image/jpg");
  83. }
  84. 方法三:
  85. [AcceptVerbs(HttpVerbs.Get)]
  86. [OutputCache(CacheProfile = "CustomerImages")]
  87. public FileResult Show(int customerId, string imageName)
  88. {
  89. var path = string.Concat(ConfigData.ImagesDirectory, customerId, @"\", imageName);
  90. return new FileStreamResult(new FileStream(path, FileMode.Open), "image/jpeg");
  91. }
  92. 这三种都可以显示已经存在的图片并且我认为第三种方法可以修改为从数据库中读取图片显示。

[转]asp.net mvc 从数据库中读取图片的更多相关文章

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

    首先是创建一个类,继承于ActionResult,记住要引用System.Web.Mvc命名空间,如下: public class ImageResult : ActionResult { publi ...

  2. 用JSP从数据库中读取图片并显示在网页上

    <1>先在mysql下建立如下的table. 并insert图像. mysql.sql文件如下: CREATE TABLE photo ( photo_no int(6) unsigned ...

  3. [转] 从数据库中读取图片并导入Excel文件,C#方式

    原文地址, 作者 Lvyou1980 直接源码吧. using System; using System.IO; using System.Data; using System.Drawing; us ...

  4. Asp.net从文件夹中读取图片,随机背景图

    第一步:配置文件web.config里添加 <system.web><connectionStrings> <!--name 是自定义的,connectionString ...

  5. ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK

    看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, 加入一个表10W数据,另一个表也是10万数据,当你用linq建立一个连接查询 ...

  6. ASP.NET MVC + EF 利用存储过程读取大数据

    ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK 看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, ...

  7. 在ASP.NET MVC应用程序中实现Server.Transfer()类似的功能

    在ASP.NET MVC应用程序中,如果使用Server.Transfer()方法希望将请求转发到其它路径或者Http处理程序进行处理,都会引发“为xxx执行子请求时出错”的HttpException ...

  8. ASP.NET MVC 3: Razor中的@:和语法

    原文 ASP.NET MVC 3: Razor中的@:和语法 [原文发表地址] ASP.NET MVC 3: Razor’s @: and <text> syntax[原文发表时间] De ...

  9. C#从SQL server数据库中读取l图片和存入图片

    原文:C#从SQL server数据库中读取l图片和存入图片 本实例主要介绍如何将图片存入数据库.将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStr ...

随机推荐

  1. Java for LeetCode 054 Spiral Matrix

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  2. codeforces 474D.Flowers 解题报告

    题目链接:http://codeforces.com/problemset/problem/474/D 题目意思:Marmot 吃两种类型的花(实在难以置信呀--):red 或者 white,如果要吃 ...

  3. 分类and分类延展

    1.Category简介 Category,又称为类别&类目&分类,是OC特有语法,在不修改原有类的基础上增加新的方法,一个庞大的类可以多人来分模块开发,有助于团队合作,或者对当前类方 ...

  4. ImageView中XML属性src和background的区别

    background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸. src是图片内容(前景),bg是背景,可以同时使用. 此外:scaleType只对sr ...

  5. android 拨号

    public class CallActivity extends Activity { @Override public void onCreate(Bundle savedInstanceStat ...

  6. Effective C++笔记:实现

    条款26:尽可能延后变量定义式的出现时间 博客地址:http://www.cnblogs.com/ronny/ 转载请注明出处! 有些对象,你可能过早的定义它,而在代码执行的过程中发生了导常,造成了开 ...

  7. Codeforces Gym 100513G G. FacePalm Accounting

    G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...

  8. pagefile.sys and heberfil.sys

    dub 删除heberfil.sys大文件的方法 方法1:Windows/system32中的cmd.exe  输入 powercfg -h off,即可关闭休眠功能,同时 Hiberfil.sys ...

  9. WinForm窗体间传值

    1.通过构造函数 特点:传值是单向的(不可以互相传值),实现简单 实现代码如下: 在窗体Form2中 int value1; string value2; public Form2 ( int val ...

  10. 玩转Android Camera开发(一):Surfaceview预览Camera,基础拍照功能完整demo

    杂家前文是在2012年的除夕之夜仓促完成,后来很多人指出了一些问题,琐事缠身一直没有进行升级.后来随着我自己的使用,越来越发现不出个升级版的demo是不行了.有时候就连我自己用这个demo测一些性能. ...