English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Image.BYTE[] and base64String Conversion Method

In C#     

Image to byte[] to base64String conversion:

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);

base64Conversion from string to byte[] to image:

byte[] imageBytes = Convert.FromBase64String(pic);
//Read into the MemoryStream object
MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
memoryStream.Write(imageBytes, 0, imageBytes.Length);
//Convert to image
Image image = Image.FromStream(memoryStream);

In current database development: the storage methods of images generally have CLOB: stores base64string

BLOB: stores byte[]

It is generally recommended to use byte[] because images can be directly converted to byte[] and stored in the database

If using base64string also needs to be converted from byte[] to base64string is more wasteful of performance.

The above article on C# image.BYTE[] and base64That's all the conversion methods of string that the editor shares with everyone. I hope it can be a reference for everyone, and I also hope everyone will support the Shouting Tutorial.

You May Also Like