码迷,mamicode.com
首页 > 其他好文 > 详细

请求一个action,将图片的二进制字节字符串在视图页面以图片形式输出

时间:2016-07-13 13:46:36      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

有些时候需要将二进制图片字节在发送浏览器以图片形式显示:

下面是一些示例代码:

控制器:

 1     /// <summary>
 2     /// 将图片的二进制字节字符串在视图页面以图片形式输出
 3     /// </summary>
 4     public class HomeController : Controller
 5     {
 6 
 7         public ActionResult Test()
 8         {
 9             return View();
10         }
11 
12         //方法一:
13         public FileResult TestFileResult_1()
14         {
15             byte[] mybyte;
16             using (WebClient client = new WebClient())
17             {
18                 mybyte = client.DownloadData("http://img.baidu.com/video/img/video_logo_new.gif");
19                 MemoryStream ms = new MemoryStream(mybyte);
20                 //System.Drawing.Image img;
21                 //img = System.Drawing.Image.FromStream(ms); 
22             }
23             return File(mybyte, "image/gif");
24         }
25 
26         //方法二:
27         public FileResult TestFileResult()
28         {
29             byte[] mybyte;
30             using (WebClient client = new WebClient())
31             {
32                 mybyte = client.DownloadData("http://img.baidu.com/video/img/video_logo_new.gif");
33                 MemoryStream ms = new MemoryStream(mybyte);
34                 //System.Drawing.Image img;
35                 //img = System.Drawing.Image.FromStream(ms); 
36             }
37             return new FileContentResult(mybyte, "image/gif");
38         }
39 
40         //方法三:
41         public ActionResult TestFileContentResult()
42         {
43             byte[] mybyte;
44             using (WebClient client = new WebClient())
45             {
46                 mybyte = client.DownloadData("http://img.baidu.com/video/img/video_logo_new.gif");
47                 MemoryStream ms = new MemoryStream(mybyte);
48             }
49             return new FileContentResult(mybyte, "image/gif");
50         }
51 
52         //方法四:
53         public ActionResult TestFile()
54         {
55             byte[] mybyte;
56             using (WebClient client = new WebClient())
57             {
58                 mybyte = client.DownloadData("http://img.baidu.com/video/img/video_logo_new.gif");
59                 //MemoryStream ms = new MemoryStream(mybyte);
60                 //System.Drawing.Image img;
61                 // img = System.Drawing.Image.FromStream(ms);
62             }
63             return File(mybyte, "image/gif");
64         }
65 
66 
67 
68 
69         public ActionResult Index()
70         {
71             return View();
72         }
73     }

视图(view):

 1 @{
 2     ViewBag.Title = "Test";
 3     Layout = "~/Views/Shared/_Layout.cshtml";
 4 }
 5 
 6 TestFile:<img src="@(Url.Action("TestFile", "Home"))"/>
 7 <br/>
 8 <br/>
 9 TestFileContentResult:<img src="@(Url.Action("TestFileContentResult", "Home"))"/>
10 
11 <br/>
12 <br/>
13 TestFileResult:<img src="@(Url.Action("TestFileResult", "Home"))"/>
14 
15 <br/>
16 <br/>
17 TestFileResult:<img src="@(Url.Action("TestFileResult_1", "Home"))"/>

运行结果如下图所示:

技术分享

请求一个action,将图片的二进制字节字符串在视图页面以图片形式输出

标签:

原文地址:http://www.cnblogs.com/linJie1930906722/p/5666503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!