[转]asp.net mvc 从数据库中读取图片
本文转自:http://www.cnblogs.com/mayt/archive/2010/05/20/1740358.html
- 首先是创建一个类,继承于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 - ].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");
- }
- 这三种都可以显示已经存在的图片并且我认为第三种方法可以修改为从数据库中读取图片显示。
[转]asp.net mvc 从数据库中读取图片的更多相关文章
- asp.net mvc 从数据库中读取图片的实现代码
首先是创建一个类,继承于ActionResult,记住要引用System.Web.Mvc命名空间,如下: public class ImageResult : ActionResult { publi ...
- 用JSP从数据库中读取图片并显示在网页上
<1>先在mysql下建立如下的table. 并insert图像. mysql.sql文件如下: CREATE TABLE photo ( photo_no int(6) unsigned ...
- [转] 从数据库中读取图片并导入Excel文件,C#方式
原文地址, 作者 Lvyou1980 直接源码吧. using System; using System.IO; using System.Data; using System.Drawing; us ...
- Asp.net从文件夹中读取图片,随机背景图
第一步:配置文件web.config里添加 <system.web><connectionStrings> <!--name 是自定义的,connectionString ...
- ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK
看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, 加入一个表10W数据,另一个表也是10万数据,当你用linq建立一个连接查询 ...
- ASP.NET MVC + EF 利用存储过程读取大数据
ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK 看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, ...
- 在ASP.NET MVC应用程序中实现Server.Transfer()类似的功能
在ASP.NET MVC应用程序中,如果使用Server.Transfer()方法希望将请求转发到其它路径或者Http处理程序进行处理,都会引发“为xxx执行子请求时出错”的HttpException ...
- ASP.NET MVC 3: Razor中的@:和语法
原文 ASP.NET MVC 3: Razor中的@:和语法 [原文发表地址] ASP.NET MVC 3: Razor’s @: and <text> syntax[原文发表时间] De ...
- C#从SQL server数据库中读取l图片和存入图片
原文:C#从SQL server数据库中读取l图片和存入图片 本实例主要介绍如何将图片存入数据库.将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStr ...
随机推荐
- 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 ...
- codeforces 474D.Flowers 解题报告
题目链接:http://codeforces.com/problemset/problem/474/D 题目意思:Marmot 吃两种类型的花(实在难以置信呀--):red 或者 white,如果要吃 ...
- 分类and分类延展
1.Category简介 Category,又称为类别&类目&分类,是OC特有语法,在不修改原有类的基础上增加新的方法,一个庞大的类可以多人来分模块开发,有助于团队合作,或者对当前类方 ...
- ImageView中XML属性src和background的区别
background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸. src是图片内容(前景),bg是背景,可以同时使用. 此外:scaleType只对sr ...
- android 拨号
public class CallActivity extends Activity { @Override public void onCreate(Bundle savedInstanceStat ...
- Effective C++笔记:实现
条款26:尽可能延后变量定义式的出现时间 博客地址:http://www.cnblogs.com/ronny/ 转载请注明出处! 有些对象,你可能过早的定义它,而在代码执行的过程中发生了导常,造成了开 ...
- Codeforces Gym 100513G G. FacePalm Accounting
G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...
- pagefile.sys and heberfil.sys
dub 删除heberfil.sys大文件的方法 方法1:Windows/system32中的cmd.exe 输入 powercfg -h off,即可关闭休眠功能,同时 Hiberfil.sys ...
- WinForm窗体间传值
1.通过构造函数 特点:传值是单向的(不可以互相传值),实现简单 实现代码如下: 在窗体Form2中 int value1; string value2; public Form2 ( int val ...
- 玩转Android Camera开发(一):Surfaceview预览Camera,基础拍照功能完整demo
杂家前文是在2012年的除夕之夜仓促完成,后来很多人指出了一些问题,琐事缠身一直没有进行升级.后来随着我自己的使用,越来越发现不出个升级版的demo是不行了.有时候就连我自己用这个demo测一些性能. ...