BASE64 encoded string and picture conversion c#__ encoding

Source: Internet
Author: User
Tags base64

 

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using System.IO;

Using System.Drawing.Imaging; namespace Base64_img {public partial class Form1:form {public Form1 () {Initialize
        Component (); }//Picture converted to Base64 encoded text private void button1_click (object sender, EventArgs e) {Ope
            Nfiledialog dlg = new OpenFileDialog (); Dlg.
            Title = "Select the picture to convert"; Dlg. Filter = "Image files (*.jpg;*.bmp;*.gif) |*.jpg*.jpeg;*.gif;*.bmp|
            Allfiles (*.*) |*.* "; if (DialogResult.OK = = dlg. ShowDialog ()) {imgtobase64string (DLG).
            FileName);
            }//Picture converted to Base64 encoded text private void imgtobase64string (string imagefilename) {
               try {Bitmap bmp = new Bitmap (imagefilename); This.pictureBox1.Image = BMP;
                FileStream fs = new FileStream (Imagefilename + ". txt", filemode.create);

                StreamWriter sw = new StreamWriter (FS);
                MemoryStream ms = new MemoryStream (); 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 ("Conversion successful!"); The catch (Exception ex) {MessageBox.Show ("imgtobase64string conversion failed/nexception:" + ex .
            message);
            }//base64 encoded text into picture private void button2_click (object sender, EventArgs e) {
       OpenFileDialog dlg = new OpenFileDialog ();     Dlg.
            Title = "Select the Base64 encoded text to convert"; Dlg.
            Filter = "txt files|*.txt"; if (DialogResult.OK = = dlg. ShowDialog ()) {base64stringtoimage (DLG).
            FileName);
            }//base64 encoded text into picture private void Base64stringtoimage (string txtfilename) {
                try {FileStream ifs = new FileStream (Txtfilename, FileMode.Open, FileAccess.Read);

                StreamReader sr = new StreamReader (IFS); String inputstr = Sr.
                ReadToEnd ();
                byte[] arr = convert.frombase64string (INPUTSTR);
                MemoryStream ms = new MemoryStream (arr);

                Bitmap bmp = new Bitmap (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.pictureBox1.Image = BMP; MessageBox.Show ("Conversion succeeded.")
            "); The catch (Exception ex) {MessageBox.Show ("Base64stringtoimage conversion failed/nexception:" +ex .
            message);
 }
        }
    }
}
 
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.