. NET C # pictures to Base64 Base64 to pictures

Source: Internet
Author: User

//picture converted to Base64 encoded text        Private voidButton1_Click (Objectsender, EventArgs e) {OpenFileDialog Dlg=NewOpenFileDialog (); Dlg. MultiSelect=true; Dlg. Title="Select the picture you want to convert"; Dlg. Filter="Image Files (*.jpg;*.bmp;*.gif;*.png) |*.jpg*.jpeg;*.gif;*.bmp| Allfiles (*. *) |*.*"; if(DialogResult.OK = =dlg. ShowDialog ()) { for(inti =0; I < dlg. Filenames.length; i++) {imgtobase64string (dlg. Filenames[i].                ToString ()); }            }        }        //picture converted to Base64 encoded text        Private voidImgtobase64string (stringimagefilename) {            Try{Bitmap bmp=NewBitmap (imagefilename);  This. pictureBox1.Image =bmp; FileStream FS=NewFileStream (Imagefilename +". txt", FileMode.Create); StreamWriter SW=NewStreamWriter (FS); MemoryStream Ms=NewMemoryStream (); Bmp.                Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] arr =New byte[Ms.                Length]; Ms. Position=0; Ms. Read (arr,0, (int) Ms.                Length); Ms.                Close (); String strbaser64=convert.tobase64string (arr); Sw.                Write (STRBASER64); Sw.                Close (); Fs.               Close (); //MessageBox.Show ("Successful conversion!");            }            Catch(Exception ex) {MessageBox.Show ("imgtobase64string conversion failed \nexception:"+Ex.            Message); }        }        //Base64 encoded text to image        Private voidButton2_Click (Objectsender, EventArgs e) {OpenFileDialog Dlg=NewOpenFileDialog (); Dlg. MultiSelect=true; Dlg. Title="Select the Base64 encoded text that you want to convert"; Dlg. Filter="txt files|*.txt"; if(DialogResult.OK = =dlg. ShowDialog ()) { for(inti =0; I < dlg. Filenames.length; i++) {base64stringtoimage (dlg. Filenames[i].                ToString ()); }                            }        }        //Base64 encoded text to image        Private voidBase64stringtoimage (stringtxtfilename) {            Try{FileStream IFS=NewFileStream (Txtfilename, FileMode.Open, FileAccess.Read); StreamReader SR=NewStreamReader (IFS); String Inputstr=Sr.                ReadToEnd (); byte[] arr =convert.frombase64string (INPUTSTR); MemoryStream Ms=NewMemoryStream (arr); Bitmap BMP=NewBitmap (MS); //bmp.                Save (Txtfilename + ". jpg", System.Drawing.Imaging.ImageFormat.Jpeg); //bmp.                Save (Txtfilename + ". bmp", imageformat.bmp); //bmp.                Save (Txtfilename + ". gif", imageformat.gif); //bmp. Save (Txtfilename + ". png", imageformat.png);Ms.                Close (); Sr.                Close (); Ifs.                Close ();  This. Picturebox2.image =bmp; if(File.exists (txtfilename)) {file.delete (txtfilename); }                //MessageBox.Show ("Conversion succeeded!") ");            }            Catch(Exception ex) {MessageBox.Show ("base64stringtoimage conversion failed \nexception:"+Ex.            Message); }        }

. NET C # pictures to Base64 Base64 to pictures

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.