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