当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net mvc 从数据库中读取图片的实现代码

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

仙乐旅行社,有点色的小游戏,江陵肃爷爷是谁

首先是创建一个类,继承于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");
}

这三种都可以显示已经存在的图片并且我认为第三种方法可以修改为从数据库中读取图片显示。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网