MrsLSD
MrsLSD

Reputation: 27

Deserialization .json to Bitmap image c#

I have a bitmap image, which I converted to a JSON file using:

string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Bitmap image); 

Content of .json file:

{  
   "Tag":null,
   "PhysicalDimension":{  
      "IsEmpty":false,
      "Width":376,
      "Height":221
   },
   "Size":{  
      "IsEmpty":false,
      "Width":376,
      "Height":221
   },
   "Width":376,
   "Height":221,
   "HorizontalResolution":96,
   "VerticalResolution":96,
   "Flags":2,
   "RawFormat":{  
      "Guid":"b96b3caa-0728-11d3-9d7b-0000f81ef32e"
   },
   "PixelFormat":2498570,
   "Palette":{  
      "Flags":62,
      "Entries":[  

      ]
   },
   "FrameDimensionsList":[  
      "7462dc86-6180-4c7e-8e3f-ee7333a7a483"
   ],
   "PropertyIdList":[  

   ],
   "PropertyItems":[  

   ]
}

Now I tried to deserialize the file in this way:

Bitmap bm = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Bitmap>(json);

But got the error:

System.MissingMethodException: "For the" System.Bitmap "type, the constructor without parameters is not defined."

Is there another way to deserialize the file or how can I fix this code?

Thanks!

Upvotes: 1

Views: 5537

Answers (2)

MBender
MBender

Reputation: 5650

The error of the message is... quite clear on this. If you look at the MSDN documentation for Bitmap, you'll find that it has no empty constructors defined. The JSON deserializer basically creates an new instance of a specified type (using the empty constructor) then fills out the properties. The Bitmap is a bit too complex for that to work. So... you need to work around that.

Bernard Vander Beken already showed you how to read a base64 string containin bitmap data. Here's how you can CREATE it from an existing bitmap:

ImageConverter converter = new ImageConverter();
var bitmapData = Convert.ToBase64String((byte[])converter.ConvertTo(bitmap, typeof(byte[])));

Note that bitmapData, in the example above, is a string which can easily be sent, stored, etc. (so no need to serialize this into JSON).

EDIT:

Please note that given the above, you need to update BOTH your serialization and deserialization code. I.e. to use the deserialization samples provided, you need to send / store a completely different JSON from the one you're currently working with... which won't be a JSON any more.

In fact, your current JSON file doesn't really contain an actual image; it only has some metadata about the image you originally used. So you definitely need to change the code responsible for serialization.

I would also modify the code for deserializing a bitmap ever so slightly (MemoryStream should, in theory, be disposed of properly and setting the position isn't required).

var bytes = Convert.FromBase64String(bitmapData);
Bitmap bitmap;
using (var ms = new MemoryStream(bytes))
  bitmap = new Bitmap(Bitmap.FromStream(ms));

Upvotes: 4

Bernard Vander Beken
Bernard Vander Beken

Reputation: 5056

Since Bitmap has no default constructor, it cannot be used out of the box for deserialization.

You can try to deserialize the JSON payload to a byte array, then pass it to Bitmap.FromStream via a MemoryStream.

string image = json;

byte[] byteBuffer = Convert.FromBase64String(image);
MemoryStream memoryStream = new MemoryStream(byteBuffer);
memoryStream.Position = 0;

return (Bitmap)Bitmap.FromStream(memoryStream);

Credits to http://ingsharksoft.blogspot.be/2013/07/solution-problem-serializedeserialize.html

Upvotes: 3

Related Questions