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

黄舟
Release: 2017-03-22 11:42:29
Original
1786 people have browsed it

下面小编就为大家带来一篇C#中图片.BYTE[]和base64string的转换方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

在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);
Copy after login

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);
Copy after login

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

BLOB:存放byte[]

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

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

The above is the detailed content of 实现C#中图片.BYTE[]和base64string的转换方法的详解. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!