SoFunction
Updated on 2025-03-06

C# base64 to string instance

Convert to Base64 form:

string a = "Base64 string and normal string"; 
 byte[] b = (a); 
 //Convert to Base64 form a = Convert.ToBase64String(b); 
 (a); 

Go back to the original one:

 byte[] c = Convert.FromBase64String(a); 
 a = (c); 
 (a);

Supplementary knowledge:Conversion of base64 string and picture in c#

c#base64 string to picture uses the bitmap class to encapsulate GDI+ bitmap, which consists of pixel data of graphic images and their characteristics. Bitmap is an object used to process images defined by pixel data.

You can query the specific bitmap class by yourself, so I won’t introduce it here.

#region base64 to picture    /// <summary>
    /// Image upload Base64 decoding    /// </summary>
    /// <param name="dataURL">Base64 data</param>    /// <param name="imgName">Picture name</param>    /// <returns>Return a relative path</returns>    public string decodeBase64ToImage(string dataURL,string imgName)
    {
 
      string filename = "";//Declare a relative path of type string 
      String base64 = ((",") + 1);   //Delete the previous extra strings of ','       bitmap = null;//Define a Bitmap object to receive the image completed by the conversion 
      try//There will be an exception thrown, try, catch      {
 
        byte[] arr = Convert.FromBase64String(base64);//Convert pure resource Base64 into an equivalent 8-bit unsigned shaping array 
         ms = new (arr);//Convert to MemoryStream object that cannot be resized        bitmap = new (ms);//Convert MemoryStream object to Bitmap object 
        filename ="Knowledge_" + imgName + ".jpg";//The relative path and name to be saved        string url =();
        string tmpRootDir = (()); //Get the program root directory        string imagesurl2 = tmpRootDir + filename; //Convert to absolute path        (imagesurl2, );//Save to server path        //(filePath + ".bmp", );
        //(filePath + ".gif", );
        //(filePath + ".png", );
        ();//Close the current flow and release all resources associated with it        ();
      }
      catch (Exception e)
      {
        string massage= ;
      }
      return filename;//Return relative path    }
    #endregion
 
    #region Picture to base64    /// &lt;summary&gt;
    /// Picture to base64    /// &lt;/summary&gt;
    /// <param name="path">Image path</param><br> /// <returns>Return a base64 string</returns>    public string decodeImageToBase64(string path) {
      path = "E:/vs 2015/newaqtcprj/WEB/UpFile/2018/12/20181229174708_7471722c425a2ec08fa513ddf4f8c76306d55fbb0fbd9d8.jpg";
      string base64str = "";
 
      //Site file directory      string fileDir = ("/");
      string[] arrfileDir = ('\\');
      fileDir = arrfileDir[0] + "\\" + arrfileDir[1] + "\\" + arrfileDir[2];
      try
      {
        //Read the picture and convert it to Base64String         bmp = new ((fileDir, "WEB\\UpFile\\2018\\12\\20181229174708_7471722c425a2ec08fa513ddf4f8c76306d55fbb0fbd9d8.jpg"));
        // bmp = new (path);
        MemoryStream ms = new MemoryStream();
        (ms, );
        byte[] arr = new byte[];
         = 0;
        (arr, 0, (int));
        ();
        ();
        base64str = Convert.ToBase64String(arr);
      }
      catch (Exception e)
      {
        string mss = ;
      }
      return "data:image/jpg;base64," + base64str;
    }
    #endregion

The above example of c# base64 to convert strings is all the content I share with you. I hope you can give you a reference and I hope you can support me more.