The method of converting pictures, binary and string in C # _c# tutorial

Source: Internet
Author: User

The examples in this article describe the methods of converting pictures, binary, and strings in C #. Share to everyone for your reference, specific as follows:

protected void Button1_Click (object sender, EventArgs e) {//Picture into binary byte[] Imagebyte = Getpicturedata (Server.MapPath (
  "./uploadfile/111.png"));
  Binary converts to string picstr = Convert.tobase64string (imagebyte);
  Output String Response.Write (PICSTR);
  String binary byte[] imagebytes = convert.frombase64string (PICSTR);
  Read into the MemoryStream object MemoryStream MemoryStream = new MemoryStream (imagebytes, 0, imagebytes.length);
  Memorystream.write (imagebytes, 0, imagebytes.length);
  Binary turn into picture save System.Drawing.Image image = System.Drawing.Image.FromStream (MemoryStream); Image.
Save (Server.MapPath ("./uploadfile/222.png")); ///<summary>///binary flow picture///</summary>///<param name= "streambyte" > Binary flow </param>///<retu rns> Pictures </returns> public System.Drawing.Image returnphoto (byte[] streambyte) {System.IO.MemoryStream ms = new S Ystem. Io.
  MemoryStream (Streambyte);
  System.Drawing.Image img = System.Drawing.Image.FromStream (ms);
return img; }///<summary&Gt Picture to binary///</summary>///<param name= "ImagePath" > Picture address </param>///<returns> binary </ Returns> public byte[] Getpicturedata (string imagepath) {//open using file stream according to the path of the picture file and save as byte[] FileStream fs = new FILESTR EAM (ImagePath, FileMode.Open);//Can be other overloaded methods byte[] Bydata = new Byte[fs.
  Length]; Fs.
  Read (bydata, 0, bydata.length); Fs.
  Close ();
return bydata; ///<summary>///pictures to binary///</summary>///<param name= "Imgphoto" > Image objects </param>///<returns > Binary </returns> public byte[] Photoimageinsert (System.Drawing.Image imgphoto) {//convert Image to stream data and save as byte[] Mem
  Orystream mstream = new MemoryStream ();
  Imgphoto.save (Mstream, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.BMP); byte[] Bydata = new Byte[mstream.
  Length]; Mstream.
  Position = 0; Mstream.
  Read (bydata, 0, bydata.length); Mstream.
  Close ();
return bydata;

 }

PS: Here is a small series to recommend this site a picture of the BASE64 format of the online conversion tool, very practical value:

Online Image conversion BASE64 tool:
http://tools.jb51.net/transcoding/img2base64

Read more about C # Interested readers can view the site topics: "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # Data structure and algorithm tutorial", "C # object-oriented Program design Introductory Course" and "C # programming Thread Usage Skill Summary"

I hope this article will help you with C # programming.

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.