Serialize a Bitmap Array

Jul 19, 2011 at 11:11 AM

I'm doing a project which need to serialize a few images which contain in the Bitmap array and few strings. I did it it using binary formatter without any errors. After because it's large size, i tried with sharp serializer, but it only can serialize those strings only. it's not serialized image array. Are there any particular ways to do that? (like property thing). Please help me.

Coordinator
Jul 28, 2011 at 4:01 PM

hi, System.Drawing.Bitmap has no standard, parameterless constructor and therefore cannot be deserialized with SharpSerializer. But you can create a config-class which stores the most important properties of your businness object. In this class the image could by stored as an array of bytes.

Before serialization you need to convert your BO into the config, i.e. with

var myConfig = new ConfigOfMyBO(myBO);

after deserialization you need to convert the config into your BO

var myBO = deserializedConfig.CreateBO();

Jul 29, 2011 at 7:53 AM

hi polo, thank you very much for help,

i did convert image to byte array and serialize it. but it gave me 96.7kbs, but image is only 17kbs. And the other problem is i cant find to way to convert whole bitmap array to byte array, so i can only convert image by image. and i can't understand what you meant by config-class. I think that is why this gave me a file lager than my image. can you explain it? thank you again for your helps.

Coordinator
Jul 29, 2011 at 4:46 PM
Edited Aug 14, 2011 at 11:32 AM

hi chapneox, how to convert a bitmap to byte[] is here:

http://www.vbforums.com/showthread.php?t=358917

<Added on August 14th, 2011>

On the Code Project there is detailed explanation how to serialize such types:

http://www.codeproject.com/KB/windows-phone-7/sharpserializer_serialize.aspx

</Added>

 

Please see an example, what i mean with BO and its config:

 

    /// <summary>
    /// This class cannot be deserialized because
    /// bitmap has no standard constructor.
    /// You have to turn it into sth more
    /// friendly for the serialization, i.e. MyBOConfig
    /// </summary>
    public class MyBO
    {
        /// <summary>
        /// Bitmap cannot be deserialized,
        /// as it has no standard constructor
        /// you have to turn it into array of byte.
        /// </summary>
        public Image Photo { get; set; }
    }


    /// <summary>
    /// This class will be serialized.
    /// It has simple properties with standard constructor
    /// </summary>
    public class MyBOConfig
    {
        /// <summary>
        /// Convert your BO into Config before serialization
        /// </summary>
        /// <param name="item"></param>
        public MyBOConfig(MyBO item)
        {
            using (var stream = new MemoryStream())
            {
                item.Photo.Save(stream, ImageFormat.Jpeg);
                Photo = stream.ToArray();
            }
        }

        /// <summary>
        /// This will be deserialized
        /// </summary>
        public byte[] Photo { get; set; }

        /// <summary>
        /// Create your BO from the config after deserialization
        /// </summary>
        /// <returns></returns>
        public MyBO CreateBO()
        {
            var myBO = new MyBO();
            // stream cannot be disposed as long the image lives.
            // below is only an example and should
            // not be used in the production
            // how to copy bitmaps is on:
            // http://www.vbforums.com/showthread.php?t=358917
            var stream = new MemoryStream(Photo);
            myBO.Photo = Image.FromStream(stream);
            return myBO;
        }
    }

Aug 3, 2011 at 5:25 AM

thank you very much polo,,i'll try with this...