"景先生毕设|www.jxszl.com

PDF转base64,图片转base64,base64转PDF,base64转图片C#源代码

2023-12-15 11:09编辑: www.jxszl.com景先生毕设
PDF转base64,图片转base64,base64转PDF,base64转图片C#源代码
一.效果图:

二.源代码:

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

namespace base64Convert
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// [功能描述: <br>PDF转base64,图片转base64,base64转PDF,base64转图片C#源代码</br>
        /// [创 建 者: 51jrft.com]<br>C#   framwork 3.5 </br>
        /// [创建时间: 2023-12-15]<br> </br>
        /// </summary>
        #region 方法
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// pdf转base64
        /// </summary>
        private void pdfConvertbase64()
        {
            string strBase64 = string.Empty;

            using (FileStream fs = new FileStream(this.textBox1.Text, FileMode.Open))
            {
                byte[] bt = new byte[fs.Length];
                fs.Read(bt, 0, bt.Length);
                fs.Close();
                strBase64 = Convert.ToBase64String(bt);
            }

            Logs(strBase64, textBox2.Text, false);
            //  richTextBox1.Text = strBase64;
        }
        /// <summary>
        /// base64转pdf
        /// </summary>
        private void base64Convertpdf()
        {

            string strBase = string.Empty;
            using (StreamReader read = new StreamReader(this.textBox1.Text, Encoding.Default))
            {
                strBase = read.ReadToEnd();
                read.Close();
            }
            byte[] bytes = Convert.FromBase64String(strBase);//richTextBox1.Text.Trim()
            //保存数据到磁盘
            string location = this.textBox2.Text;
            System.IO.FileStream stream = new System.IO.FileStream(location, System.IO.FileMode.CreateNew);
            System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
            writer.Write(bytes, 0, bytes.Length);
            writer.Close();
        }
        /// <summary>
        /// base64转Pic
        /// </summary>
        private void base64ConvertPic()
        {
            string base64 = string.Empty;
            using (StreamReader read = new StreamReader(this.textBox1.Text, Encoding.Default))
            {
                base64 = read.ReadToEnd();
                read.Close();
            }

            //  base64 = richTextBox1.Text.Trim();
            base64 = base64.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");//将base64头部信息替换
            byte[] bytes = Convert.FromBase64String(base64);
            MemoryStream memStream = new MemoryStream(bytes);
            Image mImage = Image.FromStream(memStream);


            Bitmap bp = new Bitmap(mImage);
            MemoryStream ms = new MemoryStream();
            bp.Save(this.textBox2.Text, System.Drawing.Imaging.ImageFormat.Jpeg);//注意保存路径
        }
        /// <summary>
        /// Pic转base64
        /// </summary>
        private void PicConvertbase64()
        {

            FileInfo file = new FileInfo(this.textBox1.Text);
            var stream = file.OpenRead();
            byte[] buffer = new byte[file.Length];
            //读取图片字节流
            stream.Read(buffer, 0, Convert.ToInt32(file.Length));
            //将base64字符串保存到base64.txt文件中
            StreamWriter sw = new StreamWriter("base64.txt", false, Encoding.UTF8);
            //将字节流转化成base64字符串
            //sw.Write(Convert.ToBase64String(buffer));
            sw.Close();
            Logs(Convert.ToBase64String(buffer), textBox2.Text, false);
        }
        /// <summary>
        /// 校验
        /// </summary>
        private void valid()
        {
            if (comboBox1.Text.Trim() == "PDF转BASE64" && !textBox1.Text.ToUpper().EndsWith(".PDF"))
            {
                label3.Text = "请选择PDF 文件";
                return;

            }
            else if (comboBox1.Text.Trim() == "BASE64转PDF" && !textBox1.Text.ToUpper().EndsWith(".TXT"))
            {
                label3.Text = "请选择TXT 文件";
                return;
            }
            else if (comboBox1.Text.Trim() == "BASE64转图片" && !textBox1.Text.ToUpper().EndsWith(".PDF"))
            {
                label3.Text = "请选择TXT 文件";
                return;
            }
            else if (comboBox1.Text.Trim() == "图片转BASE64" && !textBox1.Text.ToUpper().EndsWith(".JPG") && textBox1.Text.ToUpper().EndsWith(".PNG"))
            {
                label3.Text = "请选择图片【jpg|png】 文件";
                return;
            }

        }
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="str"> 文件内容</param>
        /// <param name="FileName">文件名</param>
        /// <param name="ifAdd">是否增量</param>
        public static void Logs(string str, string FileName, bool ifAdd)
        {
            System.IO.TextWriter output;



            System.IO.FileInfo errLogFs = new System.IO.FileInfo(FileName);
            try
            {
                if (ifAdd == true)
                {
                    output = File.AppendText(FileName);
                    output.WriteLine(str + "\r\n");
                    output.Close();
                }
                else
                {
                    File.Delete(FileName);
                    output = File.AppendText(FileName);
                    output.WriteLine(str + "\r\n");
                    output.Close();
                }

            }
            catch (Exception ex)
            {

            }
        }
        #endregion

        #region 变量
        #endregion

        #region 事件
        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Text = "BASE64转PDF";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            label3.Text = "-";
            string fullFileName = textBox1.Text;
            if (!string.IsNullOrEmpty(fullFileName))
            {
                if (comboBox1.Text.Trim() == "PDF转BASE64")
                {
                    textBox2.Text = fullFileName.Substring(0, fullFileName.IndexOf(".")) + ".txt";

                }
                else if (comboBox1.Text.Trim() == "BASE64转PDF")
                {
                    textBox2.Text = fullFileName.Substring(0, fullFileName.IndexOf(".")) + ".pdf";
                }
                else if (comboBox1.Text.Trim() == "BASE64转图片")
                {
                    textBox2.Text = fullFileName.Substring(0, fullFileName.IndexOf(".")) + ".jpg";
                }
                else if (comboBox1.Text.Trim() == "图片转BASE64")
                {
                    textBox2.Text = fullFileName.Substring(0, fullFileName.IndexOf(".")) + ".txt";
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            if (string.IsNullOrEmpty(this.comboBox1.Text.Trim()))
            {
                textBox1.Text = "请选择生成类别";
            }

            OpenFileDialog dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                string fullFileName = dialog.FileName;
                textBox1.Text = fullFileName;
                if (comboBox1.Text.Trim() == "PDF转BASE64")
                {
                    textBox2.Text = fullFileName.Substring(0, fullFileName.IndexOf(".")) + ".txt";
                }
                else if (comboBox1.Text.Trim() == "BASE64转PDF")
                {
                    textBox2.Text = fullFileName.Substring(0, fullFileName.IndexOf(".")) + ".pdf";
                }
                else if (comboBox1.Text.Trim() == "BASE64转图片")
                {
                    textBox2.Text = fullFileName.Substring(0, fullFileName.IndexOf(".")) + ".jpg";
                }
                else if (comboBox1.Text.Trim() == "图片转BASE64")
                {
                    textBox2.Text = fullFileName.Substring(0, fullFileName.IndexOf(".")) + ".txt";
                }

            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text.Trim() == "PDF转BASE64")
            {
                valid();
                pdfConvertbase64();

            }
            else if (comboBox1.Text.Trim() == "BASE64转PDF")
            {
                valid();
                base64Convertpdf();
            }
            else if (comboBox1.Text.Trim() == "BASE64转图片")
            {
                valid();
                base64ConvertPic();
            }
            else if (comboBox1.Text.Trim() == "图片转BASE64")
            {
                valid();
                PicConvertbase64();
            }
            label3.Text = "转换完成";

        }
        #endregion

    }
}



原文链接:http://www.jxszl.com/biancheng/C/556419.html