0

I have convert one image into base64 string and that output same with online website.

But the same image when I convert it from Android is different.

Can you please explain why C# and Android base64 strings are different for the same image.

C#.NET Code

string cImagePath = @"G:\bg-listing.png";
byte[] imagebyte = StreamFile(cImagePath);
String result = System.Convert.ToBase64String(imagebyte);
System.IO.StreamWriter outFile;
try
{
    outFile = new System.IO.StreamWriter(Application.StartupPath + "//image2base641.txt",
                                     false,
                                     System.Text.Encoding.Default);
                outFile.Write(result.ToString());
                outFile.Close();
            }
            catch (System.Exception exp)
            {
                // Error creating stream or writing to it.
                System.Console.WriteLine("{0}", exp.Message);
            }

Android Code

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),  R.drawable.image);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] ba = bao.toByteArray();
 String ba1=Base64.encodeToString(ba,Base64.DEFAULT);

Both image base64 are different.

Please help me.

0

1 Answer 1

3

There are many variants of base 64, involving line length, padding, check sums, etc. The Wikipedia article on Base64 has a nice table of variants.

My guess is that C# and Android are simply using different variants.

EDIT Based on your updated post, there are a couple of other possibilities:

  • Android may be modifying the .jpg file when it packages it up as a resource (however, while the resource packager is extremely aggressive regarding compression, this is probably not the case);
  • Android may be re-encoding the image differently than the original (two .jpg files can represent the same pixel values and not be byte-for-byte identical)

A better test would be to skip (in the Android code) the conversion from a resource to a Bitmap and back to a .jpg encoding. Just open the resource as a stream, read it directly into a byte array, and encode that in base 64.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.