欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-09 18:04:40
在c#中      图片到byte[]再到base64string的转换: bitmap bmp = ne...

在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的转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。