Overview
This article focuses on the conversion of image byte[] and base64string in C #
Encoding Rules
The idea of BASE64 encoding is to re-encode the data using 64 basic ASCII characters. It splits the data that needs to be encoded into bytes
Array. A group of 3 bytes. The 24-bit data is sorted sequentially, and the 24 bits of data are divided into 4 groups, 6 bits per group. Before the highest bit of each group
Complement two 0 to make a single byte. This re-encodes a 3-byte set of data into 4 bytes. When the number of bytes of data to encode is not
Integer multiples of 3, which means that the last group is less than 3 bytes at the time of grouping. At this point the last group is populated with 1 to 2 0 bytes. And after the final encoding is complete,
Add 1 to 2 "=" at the end.
Decoding rules
The decoding process is to restore 4 bytes to 3 bytes and then rearrange the byte arrays into data according to different data forms.
Picture to byte[] and the conversion 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);
Base64string to byte[] again to the conversion of the picture
byte[] imagebytes = convert.frombase64string (pic); Read in MemoryStream object MemoryStream MemoryStream = new MemoryStream (imagebytes, 0, imagebytes.length); Memorystream.write (imagebytes, 0, imagebytes.length); Turn to image image Image = Image.fromstream (MemoryStream);
BASE64 encoded encryption
http://base64.xpcha.com/
Summarize
Now in the database development: The picture is generally stored in a CLOB: storage base64string
Generally recommended for use with byte[]. Because pictures can be converted directly to byte[] stored in the database
If you use base64string you also need to convert from byte[] to base64string. More wasted performance.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Conversion of picture byte[] and base64string in C #