Share via


how to convert svg image to base64 string using c#

Question

Tuesday, August 28, 2012 5:33 AM

how to convert svg image to base64 string using c#

All replies (2)

Tuesday, August 28, 2012 5:42 AM âś…Answered

Just use the Convert.ToBase64String method that takes a byte[].

For example:

   System.IO.FileStream inFile;    
   byte[]             binaryData;

   try {
      inFile = new System.IO.FileStream(inputFileName,
                                System.IO.FileMode.Open,
                                System.IO.FileAccess.Read);
      binaryData = new Byte[inFile.Length];
      long bytesRead = inFile.Read(binaryData, 0,
                           (int)inFile.Length);
      inFile.Close();
   }
   catch (System.Exception exp) {
      // Error creating stream or reading from it.
      return;
   }

   // Convert the binary input into Base64 UUEncoded output. 
   string base64String;
   try {
       base64String = 
         System.Convert.ToBase64String(binaryData, 
                                0,
                                binaryData.Length);
   }
   catch (System.ArgumentNullException) {
      // log error
      return;
   }

   // Write the UUEncoded version to the output file.
   System.IO.StreamWriter outFile; 
   try {
      outFile = new System.IO.StreamWriter(outputFileName,
                           false,
                           System.Text.Encoding.ASCII);          
      outFile.Write(base64String);
      outFile.Close();
   }
   catch (System.Exception exp) {
      // Error creating stream or writing to it.
   }

Tuesday, August 28, 2012 11:33 PM

Refer this

http://forums.asp.net/t/1764746.aspx/1

http://forums.asp.net/t/1796695.aspx/1