[MonoDevelop] Convert.ToBase64String yields different result than on windows...

Quandary quandary82 at hailmail.net
Sat Jun 4 14:19:34 EDT 2011


Hi,

Question on the below code (with mono 2.10.2):
I'm converting an image to base64 string, and use this string to embed
it on a webpage, there I write the image back in the ResponseStream.

For that I use the below code, using
Convert.ToBase64String on the image byte array.

It seems it results in a different base64 string on Linux than on Windows.
With PHP, it doesn't.

To reproduce:
Encode an image (i used gif format) on Windows to base64.
Then put the resulting base64 string into a string on an asp.net
website, and base64 decode that string there, writing the image to the
output stream.
It works fine on Windows.
It works fine on Linux.
But if you base64 encode the image on windows, and put the result string
from windows into the decoding website on Linux, then you will get an
invalid image..
 
If you copy the base64 string generated on Windows to Linux into a PHP
site, and base64 decode it there, it results in a perfectly fine image.

I think this is a bug...
And if it is, this should be put into a unit test.
Because once base64 values are in a database, you can't just correct the
encoding/decoding algorithms, because in the database there will still
be values encoded by the old faulty algorithm (I do this in a reporting
service solution, to get the logo image for each customer into the
header)...


Here the code:
Note that you need to put a constant as value into variable str to cause
the error.

------------------ Code running on Linux
string strBase64Content = "base 64 converted image string here";
byte[] bt64 = System.Convert.FromBase64String(strBase64Content);
    //string strMIME = GetImageMime(ref bt64);
   
   
    Response.ContentType = "image/gif"; //strMIME;
    Response.OutputStream.Write(bt64, 0, strBase64Content.Length);
    Response.End();
--------------------------------------------




        void _Default_Load(object sender, EventArgs e)
        {
            string strPath = Server.MapPath("~/file.gif");
            System.Drawing.Image imgThisImage =
System.Drawing.Image.FromFile(strPath);
            
            string str = ImageToBase64(imgThisImage,
System.Drawing.Imaging.ImageFormat.Gif);


            Base64ToImage(str);
            Response.Write(str);
            // load event
        }
        
        
        public string ImageToBase64(System.Drawing.Image image,
              System.Drawing.Imaging.ImageFormat format)
        {
              using (System.IO.MemoryStream ms = new
System.IO.MemoryStream())
              {
                // Convert Image to byte[]
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();

                // Convert byte[] to Base64 String
                string base64String = Convert.ToBase64String(imageBytes);
                return base64String;
              }
        }
        
        
        public System.Drawing.Image Base64ToImage(string base64String)
        {
              // Convert Base64 String to byte[]
              byte[] imageBytes = Convert.FromBase64String(base64String);
              System.IO.MemoryStream ms = new
System.IO.MemoryStream(imageBytes, 0, imageBytes.Length);
            
            // Convert byte[] to Image
              ms.Write(imageBytes, 0, imageBytes.Length);
              System.Drawing.Image image =
System.Drawing.Image.FromStream(ms, true);
              return image;
        }
       


More information about the Monodevelop-list mailing list