图片文件转换为base64编码

用c#对文件进行base64编解码似乎更方便,因为有现成的方法可以调用

编码的过程:首先将文件转换成二进制,再 转换成base64

<pre name="code" class="csharp">Image img = new Bitmap("E:\\ftptest\\192.168.1.229\\2014-05-21\\192.168.1.229_0_2014-05-21_09-45-38.jpg");
            BinaryFormatter binFormatter = new BinaryFormatter();
            MemoryStream memStream = new MemoryStream();
            binFormatter.Serialize(memStream , img);
            byte[] bytes = memStream.GetBuffer();
            base64 = Convert.ToBase64String(bytes);


 



base64解码的过程和编码的过程相反,逆序进行就行

<pre name="code" class="csharp">string tmpbase64 = base64;
            byte[] bytes = Convert.FromBase64String(tmpbase64 );
            MemoryStream memStream = new MemoryStream(bytes);
            BinaryFormatter binFormatter = new BinaryFormatter();
            Image img = (Image)binFormatter.Deserialize(memStream);


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值