序列化包含BitmapImage的对象 [英] Serializing Object containing BitmapImage
问题描述
这是关于此主题的另一个问题:如何使用反序列化的对象? 我的班上有一些变量存在问题,现在我只是将[XmlIgnore]
放在无法序列化的变量前面,因此该类的序列化现在可以使用.
This is a further question about this topic: How to use deserialized object? I have a Problem with some variables in my class, right now I just put [XmlIgnore]
infront of the variables wich cannot be serialized, so the serialazation of the class works for now.
我的课看起来像这样:
public class Channel : INotifyPropertyChanged
{
public int Width { get; set; }
public int Height { get; set; }
[XmlIgnore]
public BitmapImage Logo { get; set; }
public string CurrentCoverURL { get; set; }
[XmlIgnore]
public SolidColorBrush Background { get; set; }
private string name;
public string Name
{
get { return name; }
set
{
name = value;
NotifyPropertyChanged("Name");
}
}
}
现在,我还需要以某种方式对Bitmapimage和SolidColorBrush进行序列化,以便将这些信息传递给下一个视图.
Now I somehow need to serialize the Bitmapimage and the SolidColorBrush too, so I can pass these informations to my next view.
我找到了一种方法(在C#中序列化位图/.NET到XML ),但这不适用于Windows 8 Apps. System.Drawing.Bitmap
在Windows 8中不可用.
I found a way to do this(Serialize a Bitmap in C#/.NET to XML), but this doesn't work for Windows 8 Apps. System.Drawing.Bitmap
is not available in Windows 8.
有人可以帮助我解决这个问题吗?
Can someone help me with this issue?
谢谢!
推荐答案
这帮助我完成了同样的事情.只需先转换为字节数组即可.
This helped me do the same thing. Just convert to a byte array first.
http://jamessdixon.wordpress.com/2013/10/01/handling-images-in-webapi/
您可以像这样将图像包含在JSON有效负载中:
You can include your image in your JSON payload like this:
public class Person
{
public Int32 PersonId { get; set; }
public String FirstName { get; set; }
public byte[] Image { get; set; }
}
或者您可以将imageUri包含在JSON有效负载中,如下所示:
or you can include the imageUri in your JSON payload like this:
public class Person
{
public Int32 PersonId { get; set; }
public String FirstName { get; set; }
public String ImageUri { get; set; }
}
您可以将位图图像转换为这样的字节数组;
And you can convert your bitmapimage to a byte array like this;
public static byte[] ConvertToBytes(BitmapImage bitmapImage)
{
using (var ms = new MemoryStream())
{
var btmMap = new WriteableBitmap
(bitmapImage.PixelWidth, bitmapImage.PixelHeight);
// write an image into the stream
btmMap.SaveJpeg(ms, bitmapImage.PixelWidth, bitmapImage.PixelHeight, 0, 100);
return ms.ToArray();
}
}
这篇关于序列化包含BitmapImage的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!