C # byte stream converted to picture code by BASE64 encoding

Source: Internet
Author: User
Tags readfile

C # byte stream converted to picture code by BASE64 encoding

You need to load the following namespaces

Using System.IO;
Using System.Drawing;
Using System.Runtime.Serialization.Formatters.Binary;
protected void Page_Load (object sender, EventArgs e)
{
byte[] buffer = ReadFile (Server.MapPath (@ "\sex.txt"));
char[] ch = new ASCIIEncoding (). GetChars (buffer);//or: char[] ch = Encoding.UTF8.GetChars (buffer)
String base64 = ToBase64 ();
Turn into pictures and save
Toimage (base64);
System.Drawing.Image img = toimage (base64);
Img. Save (Server.MapPath (@ "\imgbase64.jpg"));
}

//<summary>
//Get byte[]
//</summary>
//<param name= "FileName" ></param>
//<returns></returns>
Private byte[] ReadFile (string fileName)
{
FileStream fs = null;
byte[] byt = new Byte[0];
fs = new FileStream (FileName, FileMode.Open, FileAccess.Read);
BinaryReader read = new BinaryReader (FS);
Read. Basestream.seek (0, Seekorigin.begin); Set the file pointer to the file open
Byt = read. Readbytes ((int) read. Basestream.length);
FS. Close ();
return byt;
}

//<summary>
//Convert picture data to Base64 string
//</summary>
//<param name= "Sender" ></ Param>
//<param name= "E" ></PARAM>
private String ToBase64 ()
{
System.Drawing.Image img = System.Drawing.Image.FromFile (Server.MapPath (@ "\sex.jpg"));
BinaryFormatter binformatter = new BinaryFormatter ();
MemoryStream memstream = new MemoryStream ();
Binformatter.serialize (Memstream, IMG);
byte[] bytes = Memstream.getbuffer ();
String base64 = convert.tobase64string (bytes);
Memstream.close ();
Return base64;
}

<summary>
Convert a Base64 string to a picture
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
Private System.Drawing.Image Toimage (string value)
{
string base64 = value;
byte[] bytes = convert.frombase64string (base64);
MemoryStream memstream = new MemoryStream (bytes);
BinaryFormatter Binformatter = new BinaryFormatter ();
System.Drawing.Image img = (System.Drawing.Image) binformatter.deserialize (Memstream);
Memstream.close ();
return img;
}

C # byte stream converted to picture code by BASE64 encoding

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.