Search code examples
c#windows-phone-7base64

Picture quality is very poor when convert from jpg to Base64String in windows phone 7


I am trying to take a picture and convert into Base64String. When i try to convert from jpg to Base64String, the quality of the picture is very poor.

I have tried like this:

  void convert64()
    {
        byte[] bytearray = null;
        using (MemoryStream ms = new MemoryStream())
        {

            if (imgSelected == null)
            {

            }
            else
            {
                WriteableBitmap wbitmp = new WriteableBitmap((imgSelected));

                wbitmp.SaveJpeg(ms, 40, 40, 0, 82);
                bytearray = ms.ToArray();
            }
        }
        strimage = Convert.ToBase64String(bytearray);
    }




    WriteableBitmap Base64StringToBitmap(string base64String)
        {
            byte[] byteBuffer = Convert.FromBase64String(base64String);
            MemoryStream memoryStream = new MemoryStream(byteBuffer);
            memoryStream.Position = 0;

            WriteableBitmap bitmapImage = new WriteableBitmap(imgSelected);
            bitmapImage.SetSource(memoryStream);

            memoryStream.Close();
            memoryStream = null;
            byteBuffer = null;
            return bitmapImage;
        }

My Output is:

Output image

Please let me give any idea to improve the image quality.


Solution

  • You save your picture with the size of 40x40, which is far too little, and you also only use quality = 82. Use:

    wbitmp.SaveJpeg(ms, imgSelected.Width, imgSelected.Height, 0, 100);
    

    and check if it helps.


    By the way - you should use

    using(MemoryStream memoryStream = new MemoryStream(byteBuffer)){  }
    

    in your Base64StringToBitmap function, just like it is used in your convert64. You don't have to worry about closing your stream and Exceptions when you do it that way.