Home Forums Support Protobuf-net Reply To: Protobuf-net

#1013
Anonymous
Inactive

I used to start your sample and I have modified the ImageWrapper class to suite the bitmap class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Graphics;
using NetworkCommsDotNet;
using System.IO;
using DPSBase;
using ProtoBuf;

namespace Discovery_Service
{
	[ProtoContract]
	public class ImageWrapper
	{
		/// <summary>
		/// Private store of the image data as a byte[]
		/// This will be populated automatically when the object is serialised
		/// </summary>
		[ProtoMember(1)]
		private byte[] _imageData;

		/// <summary>
		/// The image name
		/// </summary>
		[ProtoMember(2)]
		public string ImageName { get; set; }

		/// <summary>
		/// The public accessor for the image. This will be populated
		/// automatically when the object is deserialised.
		/// </summary>
		public Bitmap Image { get; set; }

		/// <summary>
		/// Private parameterless constructor required for deserialisation
		/// </summary>
		private ImageWrapper() { }

		/// <summary>
		/// Create a new ImageWrapper
		/// </summary>
		/// <param name="imageName"></param>
		/// <param name="image"></param>
		public ImageWrapper(string imageName, Bitmap _image)
		{
			this.ImageName = imageName;
			this.Image = _image;
		}

		/// <summary>
		/// Before serialising this object convert the image into binary data
		/// </summary>
		[ProtoBeforeSerialization]
		private void Serialize()
		{
			if (Image != null)
			{
				//We need to decide how to convert our image to its raw binary form here

				    byte[] bitmapData;
					using (var stream = new MemoryStream())
					{
						Image.Compress (Bitmap.CompressFormat.Png, 0, stream);

						bitmapData = stream.ToArray();
					}

			}
		}

		/// <summary>
		/// When deserialising the object convert the binary data back into an image object
		/// </summary>
		[ProtoAfterDeserialization]
		private void Deserialize()
		{
		    
			//If we added custom data processes we have the perform the reverse operations here before
			//trying to recreate the image object
			//e.g. DPSManager.GetDataProcessor<LZMACompressor>()
			Image = BitmapFactory.DecodeByteArray(_imageData , 0, _imageData.Length);

		    _imageData = null;
		}
	}
}

But I got several errors from the class. Do you can see any evidence ?
Thanks
Max