C Implementation String and Picture Base64 Transcoding Operation Example
- 2021-12-21 04:41:55
- OfStack
In this paper, an example of C # to achieve string and picture Base64 encoding conversion operation. Share it for your reference, as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
namespace base64_img
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Picture Convert to base64 Coded text
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = " Select the picture to convert ";
dlg.Filter = "Image files (*.jpg;*.bmp;*.gif)|*.jpg*.jpeg;*.gif;*.bmp|AllFiles (*.*)|*.*";
if (DialogResult.OK == dlg.ShowDialog())
{
ImgToBase64String(dlg.FileName);
}
}
// Picture Convert to base64 Coded text
private void ImgToBase64String(string Imagefilename)
{
try
{
Bitmap bmp = new Bitmap(Imagefilename);
this.pictureBox1.Image = bmp;
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(" Successful conversion !");
}
catch (Exception ex)
{
MessageBox.Show("ImgToBase64String Conversion failed /nException:" + ex.Message);
}
}
//base64 Coded text Convert to Picture
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = " Select the to convert base64 Coded text ";
dlg.Filter = "txt files|*.txt";
if (DialogResult.OK == dlg.ShowDialog())
{
Base64StringToImage(dlg.FileName);
}
}
//base64 Coded text Convert to Picture
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);
bmp.Save(txtFileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
//bmp.Save(txtFileName + ".bmp", ImageFormat.Bmp);
//bmp.Save(txtFileName + ".gif", ImageFormat.Gif);
//bmp.Save(txtFileName + ".png", ImageFormat.Png);
ms.Close();
sr.Close();
ifs.Close();
this.pictureBox1.Image = bmp;
MessageBox.Show(" Conversion succeeded! ");
}
catch (Exception ex)
{
MessageBox.Show("Base64StringToImage Conversion failed /nException : "+ex.Message);
}
}
}
}
PS: Here are several practical base64 online encoding and decoding tools for everyone to use:
BASE64 encoding and decoding tool:
http://tools.ofstack.com/transcoding/base64
Online image conversion BASE64 tool:
http://tools.ofstack.com/transcoding/img2base64
Base64 Online Coding and Decoding UTF-8 Version:
http://tools.ofstack.com/tools/base64_decode-utf8.php
Base64 Online Coding and Decoding gb2312 Version:
http://tools.ofstack.com/tools/base64_decode-gb2312.php
For more readers interested in C # related content, please check the topics on this site: "Summary of C # Coding Operation Skills", "Summary of XML File Operation Skills in C #", "Tutorial on Usage of C # Common Controls", "Tutorial on Usage of WinForm Controls", "Tutorial on Data Structures and Algorithms", "Introduction to C # Object-Oriented Programming" and "Summary of Thread Use Skills in C # Programming"
I hope this article is helpful to everyone's C # programming.