asp.net mvc 从数据库中读取图片的实现代码
发布时间:2024-01-19 14:51:09
标签: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");
}
这三种都可以显示已经存在的图片并且我认为第三种方法可以修改为从数据库中读取图片显示。


猜你喜欢
- 安装数据可视化模块matplotlib:pip install matplotlib导入matplotlib模块下的pyplot1 折线图f
- Django根据已有数据库表反向生成models类一. 创建一个Django项目django-admin startproject ‘xxx
- 最近在处理Qzone黄钻图标更新时,想起近期对业务图标进行优化所遇到的一些问题,把思绪收拾起来和大家一共探讨,欢迎多方声音。在实际工作中,图
- 本文实例讲述了Python 面向对象之类class和对象基本用法。分享给大家供大家参考,具体如下:类(class):定义一件事物的抽象特点,
- 1、安装 python3sudo apt install python32、卸载 python2.7 (可选)sudo apt remove
- 字段是逗号分隔开的数组如何查询匹配数据方式一:CHARINDEX***()*****SELECT *&n
- 我不知道有多少人在使用浏览器的书签,这东东有时候实在是很有用的,比如现在说到的jQuerify书签。jQuerify书签的功能很简单,那就是
- 1.我的MySQL中的start_time存储的是2018-03-21 10:55:32格式的时间,我需要按照YYYY-MM-DD格式来查询
- 本文实例为大家分享了python脚本筛选出两个文件中重复的行数,供大家参考,具体内容如下'''查找A文件中,与B文件
- 最近迷上了高效处理数据的pandas,其实这个是用来做数据分析的,如果你是做大数据分析和测试的,那么这个是非常的有用的!!但是其实我们平时在
- 1. dbm UNIX键-值数据库dbm是面向DBM数据库的一个前端,DBM数据库使用简单的字符串值作为键来访问包含字符串的记录。dbm使用
- 1 存储过程1.1 什么是存储过程存储过程是一组为了完成某项特定功能的sql语句集,其实质上就是一段存储在数据库中的代码,他可以由声明式的s
- 在我们进行注册码的有效期验证时,通常使用获取网络时间的方式来进行比对。以下为获取网络时间的几种方式。方法一需要的时间会比较长,个别电脑上可能
- mysql_result定义和用法mysql_result() 函数返回结果集中一个字段的值。mysql_result() 返回 MySQL
- 首先,建立一个Conn的连接对象,然后连接到数据库data.mdb中,取得连接句柄后,把它保存在session("conn&quo
- 本文为大家分享了pygame游戏之旅的第3篇,供大家参考,具体内容如下载入car图片(我自己画的),需要用到pygame.image模块,定
- Python的版本有很多,很多第三方库也有很多不同的版本,不同的版本也可能是互不兼容的,在本机运行不同的项目,可能需要不同的环境。为了不和本
- php ffmpeg获取视频缩略图1.环境centos 7ffmpeg version 2.8.15 Copyright2.centos7安
- 【问题原因】 这个应该是 jquery.datatable 控件本身的一个缺陷。 该控件中的checkbox小插件的id是写死的,所以当有多
- 其实这里的静态页面并不是真正意义上的静态,但可以达到了静态页面的解析效率,还未经项目测试,拿来分享。代码如下:<% Cons