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.