asp.net 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"); }
这三种都可以显示已经存在的图片并且我认为第三种方法可以修改为从数据库中读取图片显示。
转载于:https://www.cnblogs.com/hnsongbiao/p/4456999.html
asp.net mvc 从数据库中读取图片的实现代码相关推荐
- dw读取access中的图片_怎样从Access数据库中读取图片?解决办法
当前位置:我的异常网» C# » 怎样从Access数据库中读取图片?解决办法 怎样从Access数据库中读取图片?解决办法 www.myexceptions.net 网友分享于:2013-03-0 ...
- JAVA MYSQL从数据库中提取图片_java web将图片存到储数据库和从数据库中读取图片...
(Notice:以下所有经验也是我根据网上的经验整理的,如有侵权可以联系我删除,Wx:IT_Ezra,QQ 654303408. 有问题讨论也可联系我,QQ同上.) 一.分析一下基本流程 从前台页面获 ...
- 从oracle中读取图片,从oracle数据库中读取图片,在jsp?
servlet:(jsp调用servlet显示图片,servlet如下) public void doGet(HttpServletRequest request, HttpServletRespon ...
- 用JSP从数据库中读取图片并显示在网页上
环境:mysql+tomcat: <1>先在mysql下建立如下的table. 并insert图像. mysql.sql文件如下: CREATE TABLE photo ( photo_ ...
- Android 数据库中读取图片名称加载相应的资源
android的开发中会遇到这么一种需求,就是把图片存储到本地,每次加载的时候通过保存的图片的名称加载图片,或者利用请求到的/存储数据库存储的名称去加载图片 . 开始我把数据放在了assets中,开始 ...
- 使用Gridview绑定数据库中的图片
注:此系列记录在我实际开发中遇到的问题和收藏一些技巧文章. 我们都知道,在Gridview中不能直接去绑定数据库中的图片,我们可以利用HttpHandler很容易的完成这个任务,在这里我记录一下这个过 ...
- ASP.NET MVC从数据库读取、存入图片
一.DEMO效果 先选择图片,点击上传后,即可在照片预览中看到照片.如果需要更改,重新上传就可覆盖原图片. 二.代码 页面端,这里有个细节:再次上传后,如果<img>的URL不变,浏览器会 ...
- C#从SQL server数据库中读取l图片和存入图片
一.从图片中获得二进制值的基本方法:Image.Save 方法 (String, ImageFormat) 这会将保存 Image 写入指定的文件中指定的格式. 命名空间: System.Drawin ...
- c#form+mysql储存读取图片_C#从SQL server数据库中读取l图片和存入图片
本实例主要介绍如何将图片存入数据库.将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStream类.BinaryReader把图片读成字节的形式,赋给一 ...
最新文章
- Microsoft StreamInsight 构建物联网
- 【分享】在线解析微信h5网页标签跳转到手机默认浏览器的实现方式
- 旅游网站的主页代码_一个在优化的网站主页内容应该如何设计?
- LeetCode Pow(x, n)
- CF1137F Matches Are Not a Child‘s Play(树上数据结构问题、树链剖分+ODT)
- 【移植Linux 3.4.2内核之四】修改内核代码支持YAFFS文件系统
- 解决outlook无法启动
- WinForm皮肤控件(SkinEngine)
- 多线程笔记--原子操作Interlocked系列函数
- Python源码深度解析—float空闲对象缓存池
- 得寸进尺的摩根大通 步步为营
- SpringMVC、Struts1和Struts2区别
- 华东师范大学数据学院2020夏令营机试题解
- wmi 计算机应用程序,WMI 提供程序宿主程序 (Wmiprvse.exe) 已安装 NLB 功能的基于 Windows Server 2008 的计算机上可能会崩溃...
- 其实运营,就是用户的贪、嗔、痴
- 30种常用管理工具模型整理分享(上)
- 学计算机苹果电脑哪款好,学生超值电脑汇总!适合学生的5款顶级笔记本电脑,您喜欢哪款?...
- 数字连连看java实验报告,Java语言实现数字连连看源码
- yapi接口导出为word_接口文档神器YApi
- 2022-2027年中国书店连锁行业市场调研及未来发展趋势预测报告
热门文章
- ICCV2017: Unlabeled Samples Generated by GAN Improve the Person Re-Identification Baseline in Vitro
- 【数据平台】关于Hadoop集群namenode format安全事故
- (转载)机器学习知识点(十五)从最大似然到EM算法浅解
- linux下eclipse cdt主函数main参数值传递设置
- 字符串之数字子串求和
- amd的处理器能兼容idea么_250元就能买到16GB内存 这背后究竟是道德的沦丧还是人性的扭曲?...
- quirks 模式是什么?它和 standards 模式有什么区别_什么是二级分销模式
- toString()与new String ()用法区别
- Explore the Service Console
- spring基于注解的IOC以及IoC的案例——概念