-1

First I upload a .bmp image of size 7KB to a server and then when required I send a request to the server to get that image and the image would be sent in base64 encoded format. If I save that file to a .bmp format the saved image would look exactly like the one I uploaded but it's size would be around 4KB.

I think that while uploading the image is being compressed. If it is, how to I uncompress it.

I'm saving the image in with the following code:

MemoryStream stream = new MemoryStream(Convert.FromBase64String(strImageInBase64Format));
Bitmap bmp = new Bitmap(stream);
bmp.Save("some.bmp");
pasha
  • 2,035
  • 20
  • 34
  • If you compare both of them pixel by pixel, is there any difference? What did the metadata said? One could be saved as 24 bpp, the other as 32 bpp. Also are you *sure* the returned file is a BMP? Windows & most image viewer may ignore wrong extension & just display the image according to it's real codec. Also, why would you need to *uncompress* it? If it was compressed losslessly with BMP 24-bit RLE or PNG, what's the harm? If it's compressed lossily with JPG, the information is lost anyway. – Martheen Aug 03 '17 at 07:59
  • Well compressed image could't be printed by some hardware that I'm working with. I'll look at the bpp thing – pasha Aug 03 '17 at 08:02
  • Then just load the decoded bytes as Image, and save it as BMP. This way even if the web server return a JPG or WEBP, you still feed your hardware plain BMP – Martheen Aug 03 '17 at 08:04
  • I did that, the hardware returned the error that it is not in a format as expected – pasha Aug 03 '17 at 08:06
  • I've edited my question to include the code – pasha Aug 03 '17 at 08:27
  • 1
    You're re-encoding the image before saving it. Why not just save the byte array returned from `Convert.FromBase64String(strImageInBase64Format)` with [`File.WriteAllBytes(string path, byte[] bytes)`](https://stackoverflow.com/a/381529)? – dbc Aug 03 '17 at 08:32
  • Tried that, surprisingly nothing changed, not the file size, the image, no nothing.. – pasha Aug 03 '17 at 08:40
  • I feel that the image is being compressed because the size of string strImageInBase64Format is around 4KB but the image that I uploaded is 7KB – pasha Aug 03 '17 at 08:41
  • 1
    Try passing additional [EncoderParameter](http://www.ridgesolutions.ie/index.php/2014/02/06/save-8-bit-uncompressed-windows-bitmap-file-bmp-in-c-net/) – Martheen Aug 03 '17 at 09:25
  • The hardware did accept the original 7KB image right? Is there a documentation of supported formats? – Martheen Aug 03 '17 at 09:28
  • Unfortunately NO – pasha Aug 03 '17 at 09:33
  • @martheen, Using EncoderParameter seems to have worked. I can see that the saved image file size is same as original. Let me confirm it works with hardware..Thanks very much – pasha Aug 03 '17 at 09:42
  • It works with the hardware too..@martheen thanks so much ... – pasha Aug 03 '17 at 13:00

2 Answers2

1

Just rewriting what @Martheen posted in comments to provide an answer.

The function that I've been using to save the image is:

bmp.Save("some.bmp");

From the microsoft document of this function (https://msdn.microsoft.com/en-us/library/ktx83wah(v=vs.110).aspx):

If no encoder exists for the file format of the image, the Portable Network Graphics (PNG) encoder is used.

So, even though I've been saving the image in .bmp format the image is actually being encoded in png format and then just save with .bmp extension.

To save the image from base64 string to .bmp format, I should use the function:

Image.Save Method (String, ImageCodecInfo, EncoderParameters)

Sample code

pasha
  • 2,035
  • 20
  • 34
0

Firstly, Base64 encoded format is a string, and you should decode this string if you want to see meaningful picture.

Secondly, I am represent your image data with a variable which is "serverImage" so, serverImage shows that base64 string in server.

I hope this code help you to understand this decoding:

var bytes = Convert.FromBase64String(resizeImage.Content); 
using (var serverImage = new FileStream(filePath, FileMode.Create))
//filePath is a path for localstorage.
{
    serverImage.Write(bytes ,0, bytes.Length);

}
Ugur Tufekci
  • 350
  • 2
  • 13
  • Sorry, I missed saying that I decoded the base64 string before saving it. – pasha Aug 03 '17 at 08:00
  • You should save image with a encoded version (base64 string). I cant understand why are you decode a base64 string before saving ? @pasha – Ugur Tufekci Aug 03 '17 at 08:15
  • The image is encoded in a base64 before being sent because if the original image is transferred byte by byte, it could trigger some escape sequences of some communication protocols. And I couldn't just save it in that format(I don't know if I could do that) because the hardware that I'm using won't be expecting a base64 encoded image. – pasha Aug 03 '17 at 08:25