The relationship between Image/bitmap and Base64 in C # programming

Source: Internet
Author: User

Recently used base64 encoding to pass the picture encountered some problems, summarized under.

First, summarize the logic of Base64 encoding from the network: https://www.cnblogs.com/zhangchengye/p/5432276.html

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.

Example: ABC will be BASE64 encoded:


1, first take the ASCII code value of the ABC corresponding. A (+) B (+) C (67);
2, then take the binary value A (01000001) B (01000010) C (01000011);
3, then the three bytes of the binary code to connect (010000010100001001000011);
4, and then 6 bits of the unit into 4 data blocks, and the highest bit filled two 0 after the formation of 4 bytes of the encoded value, (00010000) (00010100

) (00001001) (00000011), where the blue part is the real data;
5, then the four bytes of data into 10 binary numbers (16) (20) (9) (3);
6. Finally, according to the 64 basic character BASE64, the corresponding ASCII code character (Q) (U) (J) (D) is found, and the value is actually

The index of the data in the character table.

Note: BASE64 character: abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/

Image or Bitmap and base64: (many online, for reference only)

public static Bitmap GetImageFromBase64 (String base64string) {byte[] b = convert.frombase64string (base6            4string);            MemoryStream ms = new MemoryStream (b);            Bitmap Bitmap = new Bitmap (MS);        return bitmap; public static string Getbase64fromimage (Image imagefile) {try {//bi                TMap bmp = new Bitmap (imagefile);                MemoryStream ms = new MemoryStream (); Bmp.                Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] arr = new Byte[ms.                Length]; Ms.                Position = 0; Ms.                Seek (0, Seekorigin.begin); Ms. Read (arr, 0, (int) Ms.                Length); Ms.                Close ();                return convert.tobase64string (arr); using (MemoryStream ms = new MemoryStream ()) {ImageFile.                    Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg); Ms. Seek (0, seeKorigin.begin); Byte[] arr = Ms.                     GetBuffer (); Byte[] arr = Ms.                    ToArray ();                return convert.tobase64string (arr);            }} catch (Exception) {return ""; }        }

  

Here are some of the problems that are encountered:

Convert.frombase64string (base64string); Occasional error

Unhandled exception of type "System.FormatException" occurs in mscorlib.dll

Additional information: Base-64 character array or string length is invalid.

As we all know, this conversion is super simple, and I can't imagine why.

By the above coding method, the length of the Base64 encoding must be a multiple of 4, not enough to be used = padded

And according to the other side, it is really not a multiple of 4, and my side of the record is a multiple of 4.

Then look at the parser code and know why:

He received the XML first replaced UTF8 into GBK, so that if the Base64 string contains UTF8 will be replaced, after the replacement is not a multiple of 4 error.

Strxml = Strxml.replace ("UTF-8", "GBK"). Replace ("UTF8", "GBK");

It was a simple mistake, and it took a long time.

This kind of error, the general query whether any special characters are replaced, such as URL parameters. Do not consider that the conversion process may be problematic and basically does not exist.

The relationship between Image/bitmap and Base64 in C # programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.