图片与Json(String)的转换

 

将C#的读取的文件转换为Json的字符串,然后使用JS显示出来。

 

1.Img 2 Json

 1         //图片 转为    base64编码的文本
 2         private void ImgToBase64String(string Imagefilename)
 3         {
 4             try
 5             {
 6                 MemoryStream ms = new MemoryStream(this.FileUpload1.FileBytes);
 7                 byte[] arr = new byte[ms.Length];
 8                 ms.Position = 0;
 9                 ms.Read(arr, 0, (int)ms.Length);
10                 ms.Close();
11                 String strbaser64 = Convert.ToBase64String(arr);
12                 this.TextBox1.Text = strbaser64;
13             }
14             catch (Exception ex)
15             {
16                 lbl_message.Text = "ImgToBase64String 转换失败\nException:" + ex.Message;
17             }
18         }


2 Json 2 Img

 1 <script type="text/javascript">
 2         function imgfun()
 3         {
 4             var result = $("#TextBox1").val();
 5             if (result.indexOf("<img",0) < 0)
 6             {
 7                 $("#TextBox1").val('<img src="https://img-blog.csdnimg.cn/2022010708214938681.bmp' + result + '" alt="图片Base64编码" style="max-width:90%; max-height:2000px;"/>');
 8             }
 9             $("#img_area").html('' + $("#TextBox1").val() + '');
10         }
11     </script>


3 HMTL

 1 <div>
 2 
 3             <asp:FileUpload ID="FileUpload1" runat="server" Width="403px" />
 4             <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
 5             <input type="button" id="btn2" onclick="imgfun()" value="Button" />
 6 
 7         </div>
 8         <div>
 9             <asp:Label ID="lbl_message" runat="server" Text=""></asp:Label>
10         </div>
11         <div>
12             <input type="image" id="input_img" runat="server" /></div>
13         <div>
14             <asp:TextBox ID="TextBox1" runat="server" Height="200px" TextMode="MultiLine" Width="100%"></asp:TextBox></div>
15         <div>
16              <p id="img_area">  </p>
17 
18         </div>

 

posted on 2017-04-18 16:04 阿日斯兰 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/loge/p/6728308.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值