SoFunction
Updated on 2025-03-06

Example of Base64 encoding conversion operation for C# implementing strings and pictures

This article describes the Base64 encoding conversion operation of C# implementing strings and pictures. Share it for your reference, as follows:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
namespace base64_img
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    //Picture converted to base64 encoded text    private void button1_Click(object sender, EventArgs e)
    {
      OpenFileDialog dlg = new OpenFileDialog();
       = "Select the picture to convert";
       = "Image files (*.jpg;*.bmp;*.gif)|*.jpg*.jpeg;*.gif;*.bmp|AllFiles (*.*)|*.*";
      if ( == ())
      {
        ImgToBase64String();
      }
    }
    //Picture converted to base64 encoded text    private void ImgToBase64String(string Imagefilename)
    {
      try
      {
        Bitmap bmp = new Bitmap(Imagefilename);
        this. = bmp;
        FileStream fs = new FileStream(Imagefilename + ".txt", );
        StreamWriter sw = new StreamWriter(fs);
        MemoryStream ms = new MemoryStream();
        (ms, );
        byte[] arr = new byte[];
         = 0;
        (arr, 0, (int));
        ();
        String strbaser64 = Convert.ToBase64String(arr);
        (strbaser64);
        ();
        ();
        ("The conversion was successful!");
      }
      catch (Exception ex)
      {
        ("ImgToBase64String conversion failed/nException:" + );
      }
    }
    //Base64 encoded text to picture    private void button2_Click(object sender, EventArgs e)
    {
      OpenFileDialog dlg = new OpenFileDialog();
       = "Select the base64 encoded text to convert";
       = "txt files|*.txt";
      if ( == ())
      {
        Base64StringToImage();
      }
    }
    //Base64 encoded text to picture    private void Base64StringToImage(string txtFileName)
    {
      try
      {
        FileStream ifs = new FileStream(txtFileName, , );
        StreamReader sr = new StreamReader(ifs);
        String inputStr = ();
        byte[] arr = Convert.FromBase64String(inputStr);
        MemoryStream ms = new MemoryStream(arr);
        Bitmap bmp = new Bitmap(ms);
        (txtFileName + ".jpg", );
        //(txtFileName + ".bmp", );
        //(txtFileName + ".gif", );
        //(txtFileName + ".png", );
        ();
        ();
        ();
        this. = bmp;
        ("The conversion was successful!");
      }
      catch (Exception ex)
      {
        ("Base64StringToImage conversion failed/nException:"+);
      }
    }
  }
}

PS: Here are a few more practical base64 online encoding and decoding tools for everyone to use:

BASE64 encoding and decoding tools:
http://tools./transcoding/base64

Online picture conversion BASE64 tool:
http://tools./transcoding/img2base64

Base64 online encoding and decoding UTF-8 version:
http://tools./tools/base64_decode

Base64 online encoding and decoding gb2312 version:
http://tools./tools/base64_decode

For more information about C# related content, please check out the topic of this site:Summary of C# coding operation skills》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

I hope this article will be helpful to everyone's C# programming.