gpt4 book ai didi

c# - 序列化包含 BitmapImage 的对象

转载 作者:行者123 更新时间:2023-11-30 16:16:24 25 4
gpt4 key购买 nike

这是关于该主题的进一步问题:How to use deserialized object?我的类中的一些变量有问题,现在我只是将 [XmlIgnore] 放在无法序列化的变量前面,所以类的序列化暂时有效。

我的类(class)是这样的:

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,这样我就可以将这些信息传递给我的下一个 View 。

我找到了一种方法(Serialize a Bitmap in C#/.NET to XML),但这不适用于 Windows 8 应用程序。 System.Drawing.Bitmap 在 Windows 8 中不可用。

有人可以帮我解决这个问题吗?

谢谢!

最佳答案

这帮助我做了同样的事情。只需先转换为字节数组即可。

http://jamessdixon.wordpress.com/2013/10/01/handling-images-in-webapi/

您可以像这样在 JSON 负载中包含您的图像:

public class Person
{
public Int32 PersonId { get; set; }
public String FirstName { get; set; }
public byte[] Image { get; set; }
}

或者您可以像这样在您的 JSON 负载中包含 imageUri:

public class Person
{
public Int32 PersonId { get; set; }
public String FirstName { get; set; }
public String ImageUri { get; set; }
}

并且您可以像这样将位图图像转换为字节数组;

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();
}
}

关于c# - 序列化包含 BitmapImage 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18459620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com