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");
}
这三种都可以显示已经存在的图片并且我认为第三种方法可以修改为从数据库中读取图片显示。
0
投稿
猜你喜欢
- context 有什么作用context 主要用来在goroutine 之间传递上下文信息,包括:取消信号、超时时间、截止时间、k-v 等。
- 本文实例讲述了Python计算程序运行时间的方法。分享给大家供大家参考。具体实现方法如下:import timedef start_slee
- 使用诸如Lock、RLock、Semphore之类的锁原语时,必须多加小心,锁的错误使用很容易导致死锁或相互竞争。依赖锁的代码应该保证当出现
- 在oracle数据库的开发中,常因为时间的问题大费周章,所以特地将ORACLE数据的日期函数收藏致此。乃供他日所查也。 add_months
- queue分类python3 queue分三类:先进先出队列后进先出的栈优先级队列他们的导入方式分别是:from queue import
- 写一个循环删除的过程。 create or replace procedure delBigTab(p_TableName in varch
- 楔子上一篇文章我们探讨了 GIL 的原理,以及如何释放 GIL 实现并行,做法是将函数声明为 nogil,然后使用 with nogil 上
- 本文实例为大家分享了python实现五子棋游戏的具体代码,供大家参考,具体内容如下checkerboard.pyfrom collectio
- QUICKSORT(A, p, r)是快速排序的子程序,调用划分程序对数组进行划分,然后递归地调用QUICKSORT(A, p, r),以完
- 问题最近,在用SSH框架完成一个实践项目时,碰到了一个莫名其妙的Bug困扰了我好久,最后终于解决,记录如下。问题:同学在测试系统的时候突然发
- [项目]数据库中users表,包含u_name,u_pwd两个字段,其中u_name存在重复项,现在要实现把重复的项删除![分析]1、生成一
- 背景大家知道现在python主要有两个大的版本,一个是python2另一个是python3,那么不同的人可能会习惯不同的版本,而python
- 目录一、Python 中的语句1.1什么是声明?1.2什么是表达式1.3简单的赋值语句1.4增强赋值语句二、Python 中的多行语句2.1
- 目录1. 理解 * 和 ** 2.Python函数的参数 3. 支持任意参数的函数
- 两种方法,一种是为表空间增加数据文件: alter tablespace users add datafile '/opt/orac
- 为了组织search线的设计师交流会,特地去准备了一些资料。《SERP 2010》是其中一个,但是由于时间关系没有进行讨论。原著是英文报告,
- 在之前的文章中,我们介绍了PyQt5和PySide2中主窗口控件MainWindow的使用、窗口控件的4中基础布局管理。从本篇开始,我们来了
- 准备工作首先是准备工作,导入需要使用的库,读取并创建数据表取名为loandata。import numpy as npimport pand
- 这篇文章主要介绍了pycharm运行scrapy过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 今天出于需要,要将爬虫爬取的一些数据整理成二维数组,再编码成json字符串传入数据库那么问题就来了,在php中这个过程很简便 ,类似这样: