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:
Please let me give any idea to improve the image quality.
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.