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

    运行结果如下图所示:

  • 相关阅读:
    UITableView加载显示更多内容
    UITableView  折叠效果
    40个GitHub上最受欢迎的iOS开源项目
    oc中的block使用心得
    iOS CGRectContainsPoint的用法
    ios NSComparator 三种枚举类型
    错误提示 Unsupported compiler 'com.apple.compilers.llvmgcc42' selected for architecture 'i386'
    IOS 第三方库之-MBProgressHUD的使用详解
    ios 测试工程是否内存泄漏
    单双击手势
  • 原文地址:https://www.cnblogs.com/linJie1930906722/p/5666503.html
Copyright © 2011-2022 走看看