Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

serialize image > compress best solution ?

ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭
edited October 2016 in Xamarin.Android

Hello all,
i have a little application who scan a code bar and also make a picture of a parcel.
the picture is take with an intent.

All code bar and picture in string format are store in database (sql lite)

On this little app, there is also a button.
When user click on this button i call sql lite and get all code bar and picture and make a xml file.
this xml file is send in a FTP Server.
All works fine ...

but, now with my test :) when i scan for exemple 100 code bar take 100 picture ... the xml file is big ...
And of course, i thing we can know some problem if file is too big ....

I use the Compress method of a bitmap for to serialize the image with a quality => 10 => second parameter.

I don't know if i must to do something else for to reduce the weight of the file ...
Have you got any suggestion, experience, anything else ....

Here is it my method for to convert the bitmap in string


public static string ConvertBitmapToString(Bitmap theBitmap)
string strImage = string.Empty;

        using (var stream = new System.IO.MemoryStream())
            theBitmap.Compress(Bitmap.CompressFormat.Png, 10, stream);

            var bytes = stream.ToArray();
            strImage = Convert.ToBase64String(bytes);

        return strImage;


thanks for your help and your knowledge ...


  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    Hello all,

    any idea ...or suggestions

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    Hello all xamarin developer,

    any idea about this question ?

    thanks a lot

  • JohannesHerterJohannesHerter USMember ✭✭
    edited October 2016

    Have you actually looked at the size of the byte array you create in your method? I am not sure if bitmap.Compress works as intended with CompressFormat.Png. Try to use Jpeg instead and compare the results for the same image(s).

    Is it an option to reduce the resolution of the images? This would probably save you a lot of memory and filesize.

    png vs jpeg: I just tried it myself (your exact implementation with png and with jpeg) and had around 7.000.000 bytes(png) vs 200.000 bytes (jpeg).

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    hello @JohannesHerter

    thanks a lot for your time that's great to help me to understand the best option.

    Yes i need to reduce the size of image because every day we save approximatively more 800 images (byte) on a database.

    I'm not an expert but i think if i can't reduce the image size (best) i will go to the wall after several month ...

    I think your approach will be better, i've changed the compressFormat to my project.
    This project will be on production in a few day :)

    thanks a lot

  • JohannesHerterJohannesHerter USMember ✭✭

    As I said before, you can reduce the filesize even further when you reduce the resolution of the images before compressing the image. Let's say you want your images with a width of 500 pixels then you can do something like

    var desiredWidth = 500;
    var scale = desiredWidth / theBitmap.Width;
    var newWidth = theBitmap.Width * scale;
    var newHeight = theBitmap.Height * scale;
    var smallerBitmap = Bitmap.CreateScaledBitmap(theBitmap, newWidth, newHeight, false);

    and call your "ConvertBitmapToString" method with the smallerBitmap.

    Note that reducing the resolution and compressing the image will reduce the quality of your images. Depending on what you want to do with these images afterwards, a user might not recognize what is shown on the images, so you might need to change the parameters. Just try a few combinations and see what suits best for your requirements.

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    Hello @JohannesHerter

    sorry my english is poor and i think i haven't well understand your last message ...

    thanks a lot for the sample that's great i will place this in my solution and test it ...

    The image will be the signature (get with signaturePad from xamarin component)

    great great great :)

    Have a nice day and many thanks for your time

Sign In or Register to comment.