Re: Problem with byte[] and images?

2009-04-09 Thread VSmirk

Thank god for simple problems.

Thanks for the quick response and the good catch.  I'm not sure how
long it would have taken me to go through all of your source code
before going back and comparing types in each serialize call!

On Apr 9, 9:28 am, Marc Gravell  wrote:
> Oops - slight error in my reply! Here's the *working* code ;-p
>
>     using (Stream gpkFile = File.OpenRead(Path.Combine(folderPath,
> String.Format("{0}.gpk", image.ImageName {
>         Image img2 = Serializer.Deserialize(gpkFile);
>     }
>     string path2 = Path.Combine(folderPath, "b_" + image.ImageName);
>     File.WriteAllBytes(path2, img2.ImageJPGData);
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Problem with byte[] and images?

2009-04-09 Thread Marc Gravell

Oops - slight error in my reply! Here's the *working* code ;-p

using (Stream gpkFile = File.OpenRead(Path.Combine(folderPath,
String.Format("{0}.gpk", image.ImageName {
Image img2 = Serializer.Deserialize(gpkFile);
}
string path2 = Path.Combine(folderPath, "b_" + image.ImageName);
File.WriteAllBytes(path2, img2.ImageJPGData);

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Problem with byte[] and images?

2009-04-09 Thread Marc Gravell

Simple fix - you are asking it to save the gif **as a protobuf**,
which means it'll include a few extra things (like a dummy field and a
length prefix). Simply replace the line (incorrect):

Serializer.Serialize(targetImage,img2.ImageJPGData);

with (correct):

File.WriteAllBytes(targetImage, img2.ImageJPGData);

Job done.

Let me know if you get any other problems,

Marc Gravell

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Problem with byte[] and images?

2009-04-09 Thread Marc Gravell

OK; I'm investigating now... As an aside - if you are going to read
the entire file, you might as well use File.ReadAllBytes(path) - it'll
be a lot easier.

I'll see what I can find out...

Marc Gravell
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Problem with byte[] and images?

2009-04-09 Thread VSmirk

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
(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
-~--~~~~--~~--~--~---