Click here to Skip to main content
15,887,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello everybody!I'm beginner of Silverlight.I use Silverlight 4 for my project.
I have custom class with BitmapImage property.Currently all my properties has tagged DataMember and BitmapImage tagged IgnoreDataMember. I must serialize this class, but after some hours I couldn't find nothing about serialize BitmapImage.
Can Somebody provide some example where serializing and deserializing class with BitmapImage property.
I know only that I have to convert it in byte, but how I canvert back?Please note that I'm using Silverlight 4.Thank you
Posted

1 solution

http://stackoverflow.com/questions/4308777/wpf-bitmapimage-serialization-deserialization[^] see comments for changes, to make. As far as I see, this solution should do for you. This is very common, but I use something like this
C#
public string ImageToBase64(Image image,
  System.Drawing.Imaging.ImageFormat format)
{
  using (MemoryStream ms = new MemoryStream())
  {
    // Convert Image to byte[]
    image.Save(ms, format);
    byte[] imageBytes = ms.ToArray();

    // Convert byte[] to Base64 String
    string base64String = Convert.ToBase64String(imageBytes);
    return base64String;
  }
}

public Image Base64ToImage(string base64String)
{
  // Convert Base64 String to byte[]
  byte[] imageBytes = Convert.FromBase64String(base64String);
  MemoryStream ms = new MemoryStream(imageBytes, 0, 
    imageBytes.Length);

  // Convert byte[] to Image
  ms.Write(imageBytes, 0, imageBytes.Length);
  Image image = Image.FromStream(ms, true);
  return image;
}
 
Share this answer
 
v2
Comments
Member 7665732 22-Jun-12 8:27am    
I am surprised this is marked as solution since the Silverlight Image API does not have the Save method.

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