I am finding a problem with the serialization and deserialization of
image files in my .Net application.  The deserialized image is not a
valid GIF image, and will not display.

I have been able to determine that the problem comes down to an odd
insertion of characters just before the byte[] element in the
serialized protobuf file.  Instead of the "GIF89" entry at the start
of the original image, I find a line-fed and "ôÖ GIF89".  Otherwise, I
do not see any difference.

Here is the code I am using in .Net 3.5 SP1, Visual Studio 2008 SP.  I
am using the NET30 from protobuf-net r241.zip.

I tried looking in your history, but couldn't find anything suggesting
byte[] was the problem.  Sorry if I hit on an old issue.  I've only
been working with protocol buffers for a week.

V

Program.cs
-------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ProtoBuf;
using System.IO;

namespace GProtoBuffers
{
    class Program
    {
        static void Main(string[] args)
        {
            Test();

            return;
        }

        static void Test()
        {
            Image image = new Image();
            string parentPath = Environment.CurrentDirectory.ToLower
();
            string folderPath = Path.Combine(parentPath, "Images");
            string img = Directory.GetFiles(folderPath)[0];
            MemoryStream mem = new MemoryStream();
            using (FileStream fs = new FileStream(img, FileMode.Open))
            {
                byte[] buffer = new byte[16 * 1024];
                int read = 0;
                while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
                {
                    mem.Write(buffer, 0, read);
                    if ((fs.Length - fs.Position) < buffer.Length)
                        buffer = new byte[fs.Length - fs.Position];
                }
            }
            image.ImageID = DateTime.Now.Ticks.ToString();
            image.ImageName = img.Split(new Char[] { '\\' }).Last();
            image.ImageJPGData = mem.ToArray();

            string imagePath = Path.Combine(folderPath, String.Format
("{0}.gpk", image.ImageName));
            using (Stream file = File.Create(imagePath))
            {
                Serializer.Serialize(file, image);
                file.Close();
            }

            using (Stream targetImage = File.Create(Path.Combine
(folderPath, "b_" + image.ImageName)))
            {
                using (Stream gpkFile = File.OpenRead(Path.Combine
(folderPath, String.Format("{0}.gpk", image.ImageName))))
                {
                    Image img2 = Serializer.Deserialize<Image>
(gpkFile);
                    Serializer.Serialize(targetImage,
img2.ImageJPGData);
                }
            }
        }
    }
}
-----------------------------------

DataObject.cs
----------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProtoBuf;
using System.IO;

namespace GProtoBuffers
{
    [ProtoContract]
    public class Image
    {
        [ProtoMember(1)]
        public string ImageID { get; set; }

        [ProtoMember(2)]
        public string ImageName { get; set; }

        [ProtoMember(3)]
        public byte[] ImageJPGData { get; set; }
    }
}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to