<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答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
        主站蜘蛛池模板: 亚洲爆乳无码专区www| 亚洲综合丁香婷婷六月香| 无人视频免费观看免费视频| 国产真实伦在线视频免费观看| 亚洲大香人伊一本线| 久草福利资源网站免费| 久久久亚洲欧洲日产国码aⅴ| 久久国产乱子伦精品免费看| 亚洲国产综合专区在线电影 | 美女被免费视频网站| www.亚洲精品| 国产日韩久久免费影院 | 久久久久亚洲精品影视| 免费观看久久精彩视频| 久久精品国产精品亚洲毛片| 无码日韩人妻av一区免费| 亚洲AV无码精品国产成人| 五月婷婷亚洲综合| 老司机69精品成免费视频| 亚洲黄色在线观看视频| 女人毛片a级大学毛片免费| 美女裸免费观看网站| 久久精品国产亚洲夜色AV网站| 国产成人精品免费视频大| 亚洲精品欧美综合四区| 亚洲精品和日本精品| 秋霞人成在线观看免费视频 | 免费一本色道久久一区| 337P日本欧洲亚洲大胆精品| 亚洲一区二区女搞男| 免费看男女下面日出水来| MM1313亚洲精品无码久久| 亚洲国产精品特色大片观看完整版 | 亚洲成A人片在线观看WWW| 国产精品视频永久免费播放| 免费精品国自产拍在线播放 | 国产一区二区三区无码免费| 好男人资源在线WWW免费| 亚洲国产综合自在线另类| vvvv99日韩精品亚洲| 69天堂人成无码麻豆免费视频|