需要序列化位图图像silverlight 4 [英] Need serialize bitmapImage silverlight 4

查看:84
本文介绍了需要序列化位图图像silverlight 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我是Silverlight的初学者.我在项目中使用Silverlight4.
我有一个带有BitmapImage属性的自定义类,目前我所有的属性都标记了DataMember和BitmapImage标记了IgnoreDataMember.我必须序列化该类,但是几个小时后我找不到有关序列化BitmapImage的任何信息.
有人可以提供一些使用BitmapImage属性对类进行序列化和反序列化的示例.
我只知道我必须将其转换为字节,但如何转换回去?请注意,我正在使用Silverlight4.谢谢您

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

推荐答案

http://stackoverflow.com/questions/4308777/wpf-bitmapimage-serialization-deserialization [ ^ ]请参阅注释以进行更改.据我所知,此解决方案应该为您做.这很常见,但是我使用类似这样的东西
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
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;
}


这篇关于需要序列化位图图像silverlight 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆