Conversion of picture byte[] and base64string in C #

Source: Internet
Author: User

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 #

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.