zoukankan      html  css  js  c++  java
  • C#中图片与BASE64码互相转换

    1.图表转换成BASE64码

            private string buffer1;
            private string buffer2;

           //读取文件...

            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog openDlg = new OpenFileDialog();
              openDlg.Filter = "图形文件(*.txt)|*.gif";
               openDlg.FileName ="";
                openDlg.DefaultExt = ".gif";
                openDlg.CheckFileExists = true;
                openDlg.CheckPathExists = true;
       
                DialogResult res = openDlg.ShowDialog();
                if (res == DialogResult.OK)
                {
                    for (int i = 0; i < openDlg.FileNames.Length; i++)
                    {
                        buffer1 = openDlg.FileNames[i].ToString();
                    }
                    button2.Enabled = true;
                }
            }

           //开始转换...

            public void EncodeWithString()
            {
                System.IO.FileStream inFile;
                byte[] binaryData;
                try
                {
                    inFile = new System.IO.FileStream(buffer1,System.IO.FileMode.Open,System.IO.FileAccess.Read);
                    binaryData = new Byte[inFile.Length];
                    long bytesRead = inFile.Read(binaryData, 0,(int)inFile.Length);
                    inFile.Close();
                }
                catch (System.Exception exp)
                {
                    System.Console.WriteLine("{0}", exp.Message);
                    return;
                }
                try
                {
                    buffer2 = System.Convert.ToBase64String(binaryData,0,binaryData.Length);
                }
                catch (System.ArgumentNullException)
                {
                    System.Console.WriteLine("Binary data array is null.");
                    return;
                }
                System.IO.StreamWriter outFile;
                try
                {
                    outFile = new System.IO.StreamWriter(buffer1,false,System.Text.Encoding.ASCII);
                    outFile.Write(buffer2);
                    outFile.Close();
                }
                catch (System.Exception exp)
                {
                    System.Console.WriteLine("{0}", exp.Message);
                }
            }

           //转换并保存文件

            private void button2_Click(object sender, EventArgs e)
            {

                EncodeWithString();
                SaveFileDialog saveDlg = new SaveFileDialog();
                saveDlg.Filter = "文本文件(*.txt)|*.txt";
                saveDlg.DefaultExt = ".txt";
               saveDlg.FileName = "";
               DialogResult res = saveDlg.ShowDialog();
                if (res == DialogResult.OK)
                {
                        System.IO.FileStream s = new System.IO.FileStream (saveDlg.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                        System.IO.StreamWriter w = new System.IO.StreamWriter(s);
          w.WriteLine(buffer2);
          w.Close();
          s.Close();
                        MessageBox.Show("转换成功!");
                        buffer1 = "";
                        buffer2 = "";
                }
            }

    2.BASE64码转换成图片

            private string buffer1;
            private string buffer2;
            private string buffer3;

           //读取文件...

                   private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog openDlg = new OpenFileDialog();
        openDlg.Filter = "文本文件(*.txt)|*.txt";
        openDlg.FileName ="";
        openDlg.DefaultExt = ".txt";
        openDlg.CheckFileExists = true;
        openDlg.CheckPathExists = true;
       
        DialogResult res = openDlg.ShowDialog();
                if (res == DialogResult.OK)
                {
                    for (int i = 0; i < openDlg.FileNames.Length; i++)
                    {
                        buffer1 = openDlg.FileNames[i].ToString();
                        System.IO.StreamReader srRead;
                        srRead = new System.IO.StreamReader((System.IO.Stream)System.IO.File.OpenRead(openDlg.FileName), System.Text.Encoding.Default);
                        srRead.BaseStream.Seek(0, System.IO.SeekOrigin.Begin);
                        while (srRead.Peek() > -1)
                        {
                            buffer2 += srRead.ReadLine();
                        }
                        srRead.Close();
                    }
                }
            }

           //开始转换...

            public void DecodeWithString()
            {
                System.IO.StreamReader inFile;
                string base64String;
                try
                {
                    char[] base64CharArray;
                    inFile = new System.IO.StreamReader(buffer1,System.Text.Encoding.ASCII);
                    base64CharArray = new char[inFile.BaseStream.Length];
                    inFile.Read(base64CharArray, 0, (int)inFile.BaseStream.Length);
                    base64String = new string(base64CharArray);
                }
                catch (System.Exception exp)
                {
                    System.Console.WriteLine("{0}", exp.Message);
                    return;
                }
                try
                {
                    binaryData = System.Convert.FromBase64String(base64String);
                }
                catch (System.ArgumentNullException)
                {
                    System.Console.WriteLine("Base 64 string is null.");
                    return;
                }
                catch (System.FormatException)
                {
                    System.Console.WriteLine("Base 64 string length is not " +"4 or is not an even multiple of 4.");
                    return;
                }
                try
                {
                    System.IO.FileStream outFile;
                    outFile = new System.IO.FileStream(buffer3,System.IO.FileMode.Create,System.IO.FileAccess.Write);
                    outFile.Write(binaryData, 0, binaryData.Length);
                    outFile.Close();
                }
                catch (System.Exception exp)
                {
                    System.Console.WriteLine("{0}", exp.Message);
                }
            }

           //转换并保存文件

            private void button2_Click(object sender, EventArgs e)
            {

                DecodeWithString();
                SaveFileDialog saveDlg = new SaveFileDialog();
                saveDlg.Filter = "图形文件(*.gif)|*.gif";
                saveDlg.DefaultExt = ".gif";
               saveDlg.FileName = "";
                DialogResult res = saveDlg.ShowDialog();
                if (res == DialogResult.OK)
                {
                        buffer3 = saveDlg.FileName;
                        //panel1.BackgroundImage = Image.FromStream(binaryData);
                }
            }

  • 相关阅读:
    android29
    android28
    android27
    android26
    Dynamics CRM2011 MspInstallAction failed when installing an Update Rollup
    Dynamics CRM Import Solution Attribute Display Name description is null or empty
    The service cannot be activated because it does not support ASP.NET compatibility
    IIS部署WCF报 无法读取配置节“protocolMapping”,因为它缺少节声明
    Unable to access the IIS metabase.You do not have sufficient privilege
    LM算法与非线性最小二乘问题
  • 原文地址:https://www.cnblogs.com/wujy/p/2277235.html
Copyright © 2011-2022 走看看