zoukankan      html  css  js  c++  java
  • 请求一个action,将图片的二进制字节字符串在视图页面以图片形式输出

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

    下面是一些示例代码:

    控制器:

     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"))"/>

    运行结果如下图所示:

  • 相关阅读:
    奥巴马邻居卖房的启示,彻底改变你的思维!
    CentOS7.0安装Nginx 1.7.4
    CentOS 7 下安装 Nginx
    C# 关于线程锁lock的使用方法
    内存屏障、编译屏障:
    linux环境下安装nginx步骤
    一、为什么要学习Java虚拟机?
    Linux CentOS系统上安装Eclipse
    poj 3311 Hie with the Pie (TSP问题)
    怎样初始化一个指针数组
  • 原文地址:https://www.cnblogs.com/linJie1930906722/p/5666503.html
Copyright © 2011-2022 走看看