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

    运行结果如下图所示:

  • 相关阅读:
    http 性能测试. Apache ab 使用.
    JavaScript , js 上下文(this 的指代)
    nodejs --- querystring模块.
    NodeJs -- URL 模块.
    Node.js 回调函数 1) 阻塞 ,同步 2) 非阻塞 ,异步.
    NPM 使用介绍
    xampp 忘记密码的处理方式.
    css 伪类: 1)a:link , a:visited, a:hover, a:active 2):first-child
    background 的一些 小的细节: 1, 背景色覆盖范围: border+ width+ padding ;背景图覆盖范围: width + padding ; 2设置多个背景图片 ; 3) background-position定位百分比的计算方式: 4)background-clip 和 background-origin 的区别
    webstrom 里面使用github
  • 原文地址:https://www.cnblogs.com/linJie1930906722/p/5666503.html
Copyright © 2011-2022 走看看