ASP.NET MVC 动态加载图像

时间:2014-05-06 10:01:43   收藏:0   阅读:313


        private ImageInfo CreateImageFile(string fileName)
        {
            if (!File.Exists(fileName))
                return null;
            Image image = Image.FromFile(fileName);
            MemoryStream ms = new MemoryStream();
            image.Save(ms, ImageFormat.Png);
            image.Dispose();
            ImageInfo result = new ImageInfo();
            result.contentType = "image/png";
            result.fileContents = ms.ToArray();
            return result;
        }

        public ActionResult DisplayImage(string fileName)
        {
            ImageInfo imageInfo = CreateImageFile(Server.MapPath(fileName));
            if (imageInfo == null)
                return imageInfo == null ? "" : File(imageInfo.fileContents, imageInfo.contentType);
        }

    <img src=‘/Home/DisplayImage?fileName=myimage.jpg‘>

 

ASP.NET MVC 动态加载图像,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!