Build wheels: C # converts pictures into base64 strings

Source: Internet
Author: User
Tags base64

Address: http://www.cnblogs.com/yunfeifei/p/4165351.html

1. Convert the picture into a Base64 string in C #:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;namespaceconvertimgtobase64{classProgram {Static voidMain (string[] args) {FileInfo file=NewFileInfo ("1.png"); varstream =file.         OpenRead (); byte[] buffer =New byte[File.         Length]; //read picture byte streamStream. Read (Buffer,0, Convert.ToInt32 (file.         Length)); //Save the Base64 string to the Base64.txt fileStreamWriter SW =NewStreamWriter ("Base64.txt",false, Encoding.UTF8); //Convert a byte stream into a base64 stringSW.         Write (convert.tobase64string (buffer)); Sw.         Close (); Console.WriteLine ("Convert successful!");      Console.read (); }   }}

The 64-bit encoding exists in the TXT file,

2. Embed the base64 encoded image in the CSS

. main {    width:600px;    height:300px;    Background-image:url (data:image/gif;base64, R0LGODLHCAAIAJEAAONP6ETK5O7M8AAAACH5BAEAAAIALAAAAAAIAAGAAAINJAMJH2Q6DNXOVSQMLQA7);}

When the page style is applied to main, the display effect is a GIF background:

3. Place the base64 encoded image directly into the IMG tag

<img src="data:image/jpeg;base64, R0lgodlhcaaiajeaaonp6etk5o7m8aaaach5baeaaaialaaaaaaiaagaaainjamjh2q6dnxovsqmlqa7alt= " BASE64 encoded Image "

This also allows you to load a stream of bytes into a JPG image of the page

Build wheels: C # converts pictures into base64 strings

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.