Convert a picture to a binary string in a C # program and convert a binary string to a picture

Source: Internet
Author: User

<summary>
To add a picture to a binary stream
</summary>
<param name= "Path" ></param>
<returns></returns>
public string SaveImage (string path)
{
FileStream fs = new FileStream (path, FileMode.Open, FileAccess.Read); Save a picture as a file stream
BinaryReader br = new BinaryReader (FS);
byte[] Imgbytesin = br. Readbytes ((int) fs. Length); Reads a stream into a byte array
Encoding myencoding = encoding.getencoding ("Utf-8");

String stimagebyte = Convert.tobase64string (Imgbytesin);
return stimagebyte;
}


<summary>
Picture of realistic binary stream representation
</summary>
<param name= "Imgbytesin" ></param>
public void Showimgbybyte (string stimagebyte)
{
String newimagename = "AAAA";//imagename (Centerid);//Get the name of the picture
String ImagePath = @ "f:/aqpximageurl/" + newimagename.tostring () + ". jpg";

byte[] Imgbytesin = convert.frombase64string (stimagebyte);
MemoryStream ms = new MemoryStream (imgbytesin);

Bitmap bmp = new Bitmap (MS);
Bmp. Save (ImagePath, imageformat.bmp);
Ms. Close ();
return newimagename;

pictureBox1.Image = Image.fromstream (ms);
}

<summary>
This is for testing
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected void Btn_jituan_click (object sender, EventArgs e)
{
String bys = SaveImage ("e:/lyx/safetrainall_aqpx/safetrainall_aqpx/defaultmodule/exam/examimage/2015-08-10#2/ 00000052x1.jpg ");
Showimgbybyte (bys);
}

Convert a picture to a binary string in a C # program and convert a binary string to a picture

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.