国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

asp.net mvc 從數據庫中讀取圖片的實現代碼

來源:懂視網 責編:小采 時間:2020-11-27 22:43:13
文檔

asp.net mvc 從數據庫中讀取圖片的實現代碼

asp.net mvc 從數據庫中讀取圖片的實現代碼:首先是創建一個類,繼承于ActionResult,記住要引用System.Web.Mvc命名空間,如下: 代碼如下:public class ImageResult : ActionResult { public ImageFormat ContentType { get; set; } public Image image
推薦度:
導讀asp.net mvc 從數據庫中讀取圖片的實現代碼:首先是創建一個類,繼承于ActionResult,記住要引用System.Web.Mvc命名空間,如下: 代碼如下:public class ImageResult : ActionResult { public ImageFormat ContentType { get; set; } public Image image

首先是創建一個類,繼承于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");
}

這三種都可以顯示已經存在的圖片并且我認為第三種方法可以修改為從數據庫中讀取圖片顯示。

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

asp.net mvc 從數據庫中讀取圖片的實現代碼

asp.net mvc 從數據庫中讀取圖片的實現代碼:首先是創建一個類,繼承于ActionResult,記住要引用System.Web.Mvc命名空間,如下: 代碼如下:public class ImageResult : ActionResult { public ImageFormat ContentType { get; set; } public Image image
推薦度:
標簽: 圖片 實現 代碼
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 精品国产乱码久久久久久一区二区 | 国产精品久久久久久永久牛牛 | 亚洲精品二三区伊人久久 | 欧美日韩精品在线播放 | 国产精品合集一区二区三区 | 日韩免费高清视频网站 | 最新亚洲| 久久福利网| 国产美女视频黄a视频全免费网站 | 国产精品99| 精品一区二区三区四区五区六区 | 欧美 韩国 精品 另类 综合 | 3a毛片 | 国产三级在线 | 久久精品国产亚洲a不卡 | 一区二区三区精品牛牛 | 国产精品高清视亚洲一区二区 | 国产精品…在线观看 | 国产一区二区三区久久 | 欧美亚洲综合图区在线 | 一二三高清区线路1 | 欧美激情国产日韩精品一区18 | 欧美日韩精品 | 欧美国产日韩综合 | 中文字幕在线播放第一页 | 国产一区二区福利久久 | 久操视频免费在线观看 | 亚洲欧美在线观看 | 国产精品不卡 | 亚洲国产精品二区久久 | 国产一区亚洲二区三区 | 国产精品v一区二区三区 | 久久综合精品国产一区二区三区无 | 香蕉久久夜色精品国产小说 | 亚洲欧美日韩综合在线 | 99久久免费国产精精品 | 国产一区二区三区欧美精品 | 91精品一区二区三区久久久久 | 欧美性一区二区三区五区 | 国产精品视频一区二区三区 | 色综合91久久精品中文字幕 |