当前位置: 移动技术网 > IT编程>开发语言>c# > C#中图片.BYTE[]和base64string的转换方法

C#中图片.BYTE[]和base64string的转换方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

在c#中     

图片到byte[]再到base64string的转换:

bitmap bmp = new bitmap(filepath);
  memorystream ms = new memorystream();
  bmp.save(ms, system.drawing.imaging.imageformat.gif);
  byte[] arr = new byte[ms.length];
  ms.position = 0;
  ms.read(arr, 0, (int)ms.length);
  ms.close();
string   pic = convert.tobase64string(arr);

base64string到byte[]再到图片的转换:

byte[] imagebytes = convert.frombase64string(pic);
//读入memorystream对象
memorystream memorystream = new memorystream(imagebytes, 0, imagebytes.length);
memorystream.write(imagebytes, 0, imagebytes.length);
//转成图片
image image = image.fromstream(memorystream);

现在的数据库开发中:图片的存放方式一般有clob:存放base64string

blob:存放byte[]

一般推荐使用byte[]。因为图片可以直接转换为byte[]存放到数据库中

若使用base64string 还需要从byte[]转换成base64string 。更浪费性能。

以上这篇c#中图片.byte[]和base64string的转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网