Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
This seems to be quite a tricky one.

I am uploading images into a database using C# 4.

I serialize them, then convert them to byte[] so that they can be loaded into a "Image" type field.

This works great.

If I convert back from byte[] to string and save the image, it saves perfectly and there are no issues.

Now the catch is, I need to show these images in a silverlight application, s they get used in another application (which happens to be a silverlight application).

Now Silverlight doesn't have a bitmap class, only a Bitmap image.

When I try and deserialize I get the following error:

Type 'System.Windows.Media.ImageSource' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required.<br />


My Code to Serialize:

C#
byte[] newByte = new byte[0];
string fileName = System.IO.Path.GetFileName(s);
Guid CustomPinID = new Guid(Session["Upload"].ToString());
Bitmap myImage = new Bitmap(s);
oDataService.InsertCustomPinImage(CustomPinID, oDataService.GetMyBytes(SerializeMe(myImage)), 0.00, 0.00, fileName);

public string SerializeMe(object obj)
{
    using (MemoryStream memoryStream = new MemoryStream())
    {
        System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(obj.GetType());
        serializer.WriteObject(memoryStream, obj);
        return Encoding.UTF8.GetString(memoryStream.ToArray());
    }
}

public byte[] GetMyBytes(string text)
{
    return ASCIIEncoding.UTF8.GetBytes(text);
}


My Code is to Deserialize below:

C#
BitmapImage DisplayImage = new BitmapImage();
DisplayImage = (BitmapImage)(cSerialization.DeserializeMe(cSerialization.GetMyString(oPinImage.imagePath), typeof(BitmapImage)));

public class Serialization
{

    public string GetMyString(byte[] oArray)
    {
        return Encoding.UTF8.GetString(oArray, 0, oArray.Length);
    }

    public object DeserializeMe(string xml, Type toType)
    {
        using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
        {
            System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(toType);
            return serializer.ReadObject(memoryStream);
        }
    }
}
Posted
Updated 12-Aug-11 0:11am
v2

To convert a byte array to a BitmapImage object:

MemoryStream stream = new MemoryStream(bytearray);
BitmapImage image = new BitmapImage();
image.SetSource(stream);


At this point, you can set your object's Source property to your new BitmapImage object.

myImage.Source = image;
 
Share this answer
 
Comments
fasan21 12-Aug-11 6:37am    
I think maybe I am serializing the image incorrectly.

With your solution I get the following error :
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

Here is my code for serializing now:

Bitmap myImage = new Bitmap(s);

MemoryStream stream = new MemoryStream();
myImage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
Byte[] bytes = stream.ToArray();

Am I doing somthing wrong?
fasan21 15-Aug-11 1:36am    
Hi John

Did you ever manage to figure this one out?

A pain in the bum it is :)
I used normal deserializing in my webservice of my asp app.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900