public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
if (nominalType != typeof(Image) && nominalType != typeof(Bitmap))
{
var message = string.Format("Nominal type must be Image or Bitmap, not {0}.", nominalType.FullName);
throw new ArgumentException(message, "nominalType");
}
if (actualType != typeof(Bitmap))
{
var message = string.Format("Actual type must be Bitmap, not {0}.", actualType.FullName);
throw new ArgumentException(message, "actualType");
}
var bsonType = bsonReader.GetCurrentBsonType();
byte[] bytes;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Binary:
bytes = bsonReader.ReadBytes();
break;
case BsonType.Document:
bsonReader.ReadStartDocument();
bsonReader.ReadString("_t");
bytes = bsonReader.ReadBytes("bitmap");
bsonReader.ReadEndDocument();
break;
default:
var message = string.Format("BsonType must be Null, Binary or Document, not {0}.", bsonType);
throw new FileFormatException(message);
}
var stream = new MemoryStream(bytes);
return new Bitmap(stream);
}