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);
                }
            }

  • 相关阅读:
    SDK Hello world(直接使用SDK封装)
    一个类有多个基类的内存布局
    写个测试程序看看磁盘映像文件中哪个扇区可以使用?
    在服务中以当前用户身份启动一个程序
    在类有成员变量的场景下, 按照虚表原理, 模拟虚函数实现
    pipe----管道
    Qt序列化格式分析(qint,QString)(非常简单好用)
    模拟QQ系统设置面板实现功能
    Qt持久性对象进行序列化(同时比较了MFC与Java的方法)
    使用srvany.exe将任何程序作为Windows服务运行
  • 原文地址:https://www.cnblogs.com/wujy/p/2277235.html
Copyright © 2011-2022 走看看