zoukankan      html  css  js  c++  java
  • Base64编码的字符串与图片的转换 C#

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.ComponentModel;  
    4. using System.Data;  
    5. using System.Drawing;  
    6. using System.Text;  
    7. using System.Windows.Forms;  
    8. using System.IO;  
    9. using System.Drawing.Imaging;  
    10.   
    11. namespace base64_img  
    12. {  
    13.     public partial class Form1 : Form  
    14.     {  
    15.         public Form1()  
    16.         {  
    17.             InitializeComponent();  
    18.         }  
    19.   
    20.         //图片 转为    base64编码的文本  
    21.         private void button1_Click(object sender, EventArgs e)  
    22.         {  
    23.             OpenFileDialog dlg = new OpenFileDialog();  
    24.             dlg.Title = "选择要转换的图片";  
    25.             dlg.Filter = "Image files (*.jpg;*.bmp;*.gif)|*.jpg*.jpeg;*.gif;*.bmp|AllFiles (*.*)|*.*";  
    26.             if (DialogResult.OK == dlg.ShowDialog())  
    27.             {  
    28.                 ImgToBase64String(dlg.FileName);  
    29.             }  
    30.         }  
    31.         //图片 转为    base64编码的文本  
    32.         private void ImgToBase64String(string Imagefilename)  
    33.         {  
    34.             try  
    35.             {  
    36.                 Bitmap bmp = new Bitmap(Imagefilename);  
    37.                 this.pictureBox1.Image = bmp;  
    38.                 FileStream fs = new FileStream(Imagefilename + ".txt", FileMode.Create);  
    39.                 StreamWriter sw = new StreamWriter(fs);  
    40.   
    41.                 MemoryStream ms = new MemoryStream();  
    42.                 bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);  
    43.                 byte[] arr = new byte[ms.Length];  
    44.                 ms.Position = 0;  
    45.                 ms.Read(arr, 0, (int)ms.Length);  
    46.                 ms.Close();  
    47.                 String strbaser64 = Convert.ToBase64String(arr);  
    48.                 sw.Write(strbaser64);  
    49.   
    50.                 sw.Close();  
    51.                 fs.Close();  
    52.                 MessageBox.Show("转换成功!");  
    53.             }  
    54.             catch (Exception ex)  
    55.             {  
    56.                 MessageBox.Show("ImgToBase64String 转换失败/nException:" + ex.Message);  
    57.             }  
    58.         }  
    59.   
    60.         //base64编码的文本 转为    图片  
    61.         private void button2_Click(object sender, EventArgs e)  
    62.         {  
    63.             OpenFileDialog dlg = new OpenFileDialog();  
    64.             dlg.Title = "选择要转换的base64编码的文本";  
    65.             dlg.Filter = "txt files|*.txt";  
    66.             if (DialogResult.OK == dlg.ShowDialog())  
    67.             {  
    68.                 Base64StringToImage(dlg.FileName);  
    69.             }  
    70.         }  
    71.         //base64编码的文本 转为    图片  
    72.         private void Base64StringToImage(string txtFileName)  
    73.         {  
    74.             try  
    75.             {  
    76.                 FileStream ifs = new FileStream(txtFileName, FileMode.Open, FileAccess.Read);  
    77.                 StreamReader sr = new StreamReader(ifs);  
    78.   
    79.                 String inputStr = sr.ReadToEnd();  
    80.                 byte[] arr = Convert.FromBase64String(inputStr);  
    81.                 MemoryStream ms = new MemoryStream(arr);  
    82.                 Bitmap bmp = new Bitmap(ms);  
    83.   
    84.                 bmp.Save(txtFileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);  
    85.                 //bmp.Save(txtFileName + ".bmp", ImageFormat.Bmp);  
    86.                 //bmp.Save(txtFileName + ".gif", ImageFormat.Gif);  
    87.                 //bmp.Save(txtFileName + ".png", ImageFormat.Png);  
    88.                 ms.Close();  
    89.                 sr.Close();  
    90.                 ifs.Close();  
    91.                 this.pictureBox1.Image = bmp;  
    92.                 MessageBox.Show("转换成功!");  
    93.             }  
    94.             catch (Exception ex)  
    95.             {  
    96.                 MessageBox.Show("Base64StringToImage 转换失败/nException:"+ex.Message);  
    97.             }  
    98.         }  
    99.     }  
    100. }  
    101. <pre></pre>  
  • 相关阅读:
    ibatis 批量更新(一)
    eclipse tomcat路径更改后启动报错
    xftp Initialize Flexnet Service failed / Error code: 50003
    百度网盘 文件名中(文件)含有敏感词
    一人之下第二季百度云高清下载
    React Native Mac配置指南
    Androd自己定义控件(三)飞翔的小火箭
    关于职位规划
    SSH框架之Struts(3)——Struts的执行流程之核心方法
    HDU 4891 The Great Pan (字符串处理)
  • 原文地址:https://www.cnblogs.com/cuiguanghe/p/3051933.html
Copyright © 2011-2022 走看看