Need serialize bitmap image silverlight

[亡魂溺海] 提交于 2019-12-11 04:39:03

问题


I need serialize custom class with bitmapImage(tagged by xmlIgnore right now). I'm using xmlSerialization, but I think thats bad.Do you have some ideas how can I serialize my class??Probably you can provide some simple example??

class X
{
private BitmapImage someImage;
public BitmaImage{get;set}
}

Actually later I will be use WCF Service. Thanks)


回答1:


You can expose the image as a byte array, e.g.:

public byte[] ImageAsBytes
{
    get { return BytesFromImage(someImage); }
    set { someImage = ImageFromBytes(value); }
}

You can of course convert back using a stream and the StreamSource property.




回答2:


You could convert the image to a Base64 string. Examples from here:

//Convert image to the string
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;
   }
}

//when deserializing, convert the string back to an image
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;
}


来源:https://stackoverflow.com/questions/8701272/need-serialize-bitmap-image-silverlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!