Home >Backend Development >C#.Net Tutorial >Detailed explanation of the conversion method of image .BYTE[] and base64string in C#
The following editor will bring you an article on the conversion method of picture .BYTE[] and base64string in C#. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.
In C
#Conversion of image to byte[] and then to 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);
Conversion from base64string to byte[] and then to image:
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);
In current database development: images are generally stored in CLOB: storing base64string
BLOB: store byte[]
It is generally recommended to use byte[]. Because pictures can be directly converted to byte[] and stored in the database
If you use base64string, you need to convert from byte[] to base64string. More waste of performance.
The above is the detailed content of Detailed explanation of the conversion method of image .BYTE[] and base64string in C#. For more information, please follow other related articles on the PHP Chinese website!