<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
        主站蜘蛛池模板: 亚洲午夜国产精品无码老牛影视| 国产精品久久久久久久久免费| 国产精品亚洲综合一区在线观看| 亚洲AV成人无码久久WWW| 国产综合激情在线亚洲第一页| 超pen个人视频国产免费观看| 亚洲美女在线国产| 亚洲免费在线视频| 亚洲AV日韩综合一区| 色www永久免费视频| 日韩欧美亚洲中文乱码| mm1313亚洲精品无码又大又粗 | 最近中文字幕mv免费高清在线| 国产精品jizz在线观看免费| 久久久久亚洲av无码尤物| 国产精品日本亚洲777| 日韩亚洲国产二区| 两个人日本WWW免费版| 免费在线观看视频网站| 亚洲综合色婷婷七月丁香| 亚洲Av永久无码精品黑人| 亚洲av无码乱码在线观看野外| ass亚洲**毛茸茸pics| 日本一区二区在线免费观看| 91免费国产精品| 亚洲欧洲无卡二区视頻| **真实毛片免费观看| 亚洲人成人无码网www电影首页| 黄网站免费在线观看| 免费一级毛片女人图片| 亚洲制服丝袜第一页| 久久免费区一区二区三波多野| 国产99视频免费精品是看6| 亚洲一本到无码av中文字幕| 1000部夫妻午夜免费| 亚洲AV色无码乱码在线观看| 日韩亚洲一区二区三区| 91精品成人免费国产| 亚洲精品亚洲人成人网| 爽爽爽爽爽爽爽成人免费观看| 亚洲卡一卡2卡三卡4麻豆|