[WPF]Image 與 Base64互相轉換

摘要:[WPF]Image 與 Base64互相轉換

因為專案的需要接觸到了Base64編碼這種資料呈現方式,在一些專案上面,有時為了快速或某些特殊需求需要將圖片轉成Base64後呈現。

小弟這邊本來就有asp寫好的一段function code,今天因為在開發app時也需要用到就乾脆將此段程式碼做成一個簡單的exe檔,這邊提供圖片(bitmap、jpg、gif、png)與base64互相轉換之功能。

ImgToBase64.exe下載

P.S.順便提供包成exe時找到的相關程式碼給有興趣的人參考(來源:http://blog.csdn.net/marquess/article/details/2732629)

小弟程式碼內的版本:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Drawing.Imaging;
using Microsoft.Win32;
namespace ImgToBase64
{
    /// 
    /// MainWindow.xaml 的互動邏輯
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        //圖片轉成base64編碼的文件檔
        private void ImgToBase64String(string Imagefilename)
        {
            try
            {
                Bitmap bmp = new Bitmap(Imagefilename);
                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("轉換成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("ImgToBase64String 轉換失敗/nException:" + ex.Message);
            }
        }
        //base64編碼轉為圖片
        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);
                switch(((ComboBoxItem)cb_ImgType.Items[cb_ImgType.SelectedIndex]).Content.ToString())
                    {
                        case "jpg":
                            bmp.Save(txtFileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);break;
                        case "bmp":
                            bmp.Save(txtFileName + ".bmp", ImageFormat.Bmp); break;
                        case "gif": 
                            bmp.Save(txtFileName + ".gif", ImageFormat.Gif);break;
                        case "png": 
                            bmp.Save(txtFileName + ".png", ImageFormat.Png);break;
                    }
                ms.Close();
                sr.Close();
                ifs.Close();
                MessageBox.Show("轉換成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Base64StringToImage 轉換失敗/nException:" + ex.Message);
            }
        }
        private void btnTo64_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "選擇要轉換的圖片";
            dlg.Filter = "Image files (*.jpg;*.bmp;*.gif;*.png)|*.jpg*.jpeg;*.gif;*.png;*.bmp|AllFiles (*.*)|*.*";
            if ((bool)dlg.ShowDialog())
            {
                ImgToBase64String(dlg.FileName);
            }
        }

        private void btnToImg_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "選擇要轉換的base64文件檔案";
            dlg.Filter = "txt files|*.txt";
            if ((bool)dlg.ShowDialog())
            {
               Base64StringToImage(dlg.FileName);
                
            }
        }
    }
}

 


如果覺得文章還不錯麻煩請在文章最上面給予推薦,你的支持是小弟繼續努力產出的動力!