Tony Cheung

Hi all,

I expect using C# to serialize a Bitmap would be straight forward, but
it turns out that I am wrong. I tried with the code below but I failed
to get the image data.

Code Snippet
class Program
{
static void Main(string[] args)
{
Bitmap corolla = new Bitmap(@"C:\corolla.jpg", true); //
some image file.
MemoryStream ms = new MemoryStream();
XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8);
xw.Formatting = Formatting.Indented;
XmlSerializer ser = new XmlSerializer(typeof(Bitmap));
ser.Serialize(xw, corolla);
Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
}
}

And the console output is

Code Snippet
< xml version="1.0" encoding="utf-8" >
<Bitmap xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance" </A< A>xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Palette />
</Bitmap>

How can I serialize the image data into a xml file




Re: ASMX Web Services and XML Serialization How do I serialize a Bitmap class?

Tony Cheung

Hi all,

I expect using C# to serialize a Bitmap would be straight forward, but
it turns out that I am wrong. I tried with the code below but I failed
to get the image data.

Code Snippet
class Program
{
static void Main(string[] args)
{
Bitmap corolla = new Bitmap(@"C:\corolla.jpg", true); //
some image file.
MemoryStream ms = new MemoryStream();
XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8);
xw.Formatting = Formatting.Indented;
XmlSerializer ser = new XmlSerializer(typeof(Bitmap));
ser.Serialize(xw, corolla);
Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
}
}

And the console output is

Code Snippet
< xml version="1.0" encoding="utf-8" >
<Bitmap xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance" </A< A>xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Palette />
</Bitmap>

How can I serialize the image data into a xml file




Re: ASMX Web Services and XML Serialization How do I serialize a Bitmap class?

Indian Ocean

Hi,

The following code writes proper serialized data in console and I have also written deserializing code if you want to use it,

Serialization/Deserialization of Bitmap

Bitmap corolla = new Bitmap(@"C:\corolla.jpg", true); // some image file.

MemoryStream ms = new MemoryStream();

corolla.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

StringWriter sw = new StringWriter();

XmlTextWriter xw = new XmlTextWriter(sw);

xw.Formatting = Formatting.Indented;

XmlSerializer ser = new XmlSerializer(ms.GetBuffer().GetType());

ser.Serialize(xw, ms.GetBuffer());

Console.WriteLine("Serialized Content::");

string sSerializedContent = sw.GetStringBuilder().ToString();

Console.WriteLine(sSerializedContent);

xw.Close();

//Deserialize

XmlTextReader xrw = new XmlTextReader(new StringReader(sSerializedContent));

corolla = new Bitmap(

new MemoryStream((byte[])ser.Deserialize(xrw)));

corolla.Save("<new file name to check what it writes>");

xrw.Close();

HTH,






Re: ASMX Web Services and XML Serialization How do I serialize a Bitmap class?

Tony Cheung

Thanks, Mr. Indian Ocean.

I understand that the Bitmap.Save can help to serialize the image data to a byte stream. However, I would like to stick with a Bitmap object, i.e. the serialized xml should reside in <Bitmap> element and with corresponding Bitmap properties element under it. I did tried to create my own serializer inherited from XmlSerializer, however, the overrided Serialize method does not get called anyway.

When I look at it in Reflector, I found out that the Serialize method first call the abstrac API, CreateWriter, of the XmlSerializer base class. This API suppose to return a XmlSerializationWriter object which is documented as .Net Framework internal class and not to be used by us. I have no ideas on how to implement a XmlSerializer subclass at the moment. Any suggestion






Re: ASMX Web Services and XML Serialization How do I serialize a Bitmap class?

Indian Ocean

1. The serialization of Bitmap directly will not give you the serialized data of actual bitmap because BitMap object will not have it with it. Its similar like FileInfo that FileInfo is information about File but not actual file in it, you need to use FileStream to get the content and same way BitMap is information about the image but not having actual content so you will need to somehow save the content in stream and then you need to serialize that stream for which i have given above an example

2. If you want to do that using your custom serializer then also internally you will need to do the same thing

3. In your custom serializer, Serialize method will not be called because it has different parameters and its protected.

Please have a look at the following link. This thread is not relavant to you but it has the code which may help you under "Re: Config Load Time" heading written by Piotr.

He shown code for custom xml serializer from Enterprise lib somewhere.

http://www.theserverside.net/news/thread.tss thread_id=31518

HTH,






Re: ASMX Web Services and XML Serialization How do I serialize a Bitmap class?

Sowmy Srinivasan

Moved this from the remoting forum




Re: ASMX Web Services and XML Serialization How do I serialize a Bitmap class?

Jim W.

This isn't a complete solution, but it should get you on the right track:

You can use the Save() method on the Bitmap class to get the bytes into a memory stream, and from there, a byte[] array. Then you can serialize the byte array... a common way to do that is to use the System.Convert.ToBase64String(). Then you can send the bitmap as a string parameter.

On the other side, you can "reconstitute" the image this way:

public Bitmap SetImage(string Base64Image)

{

System.IO.MemoryStream ImageStream = new System.IO.MemoryStream(System.Convert.FromBase64String(Base64Image));

Bitmap theImage = new Bitmap(ImageStream);

return theImage;

}

Take care,

-Jim W.





Re: ASMX Web Services and XML Serialization How do I serialize a Bitmap class?

Daniel Roth

If you are using .NET 3.0, then you can take advantage of the DataContractSerializer to serialize Bitmaps and any other Serializable type:

Code Snippet

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.Serialization;

using System.Drawing;

using System.IO;

namespace BitmapSerializationTest

{

class Program

{

static void Main(string[] args)

{

Bitmap bitmap = new Bitmap(@"c:\users\me\pictures\test.bmp");

DataContractSerializer dcs = new DataContractSerializer(typeof(Bitmap));

dcs.WriteObject(File.Create("test.xml"), bitmap);

}

}

}