This project is read-only.

Serialize a Bitmap Array

Jul 19, 2011 at 12:11 PM

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.

Jul 28, 2011 at 5: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 8: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.

Jul 29, 2011 at 5:46 PM
Edited Aug 14, 2011 at 12:32 PM

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

<Added on August 14th, 2011>

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



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:
            var stream = new MemoryStream(Photo);
            myBO.Photo = Image.FromStream(stream);
            return myBO;

Aug 3, 2011 at 6:25 AM

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