我想要XML序列化一个复杂的类型(类),它有一个System.Drawing.Bitmap 类型的属性除其他外。
/// <summary>
/// Gets or sets the large icon, a 32x32 pixel image representing this face.
/// </summary>
/// <value>The large icon.</value>
public Bitmap LargeIcon { get; set; }
我现在发现使用默认 XML 序列化程序序列化 Bitmap 不起作用,因为它没有公共无参数构造函数,而默认 xml 序列化程序则强制使用该构造函数。
我知道以下几点:
- 有一个解决方法,发布在这里:http://www.dotnetspider.com/resources/4759-XML-Serialization-C-Part-II-Images.aspx。然而,由于这包括添加另一个属性,这在我看来有点像黑客。
- sourceforge 上还有一个深度 XML 序列化项目。
我不想引用另一个项目,也不想广泛调整我的类以仅允许这些位图的 xml 序列化。
难道就没有办法保持这么简单吗?
非常感谢,马塞尔
我会做类似的事情:
[XmlIgnore]
public Bitmap LargeIcon { get; set; }
[Browsable(false),EditorBrowsable(EditorBrowsableState.Never)]
[XmlElement("LargeIcon")]
public byte[] LargeIconSerialized
{
get { // serialize
if (LargeIcon == null) return null;
using (MemoryStream ms = new MemoryStream()) {
LargeIcon.Save(ms, ImageFormat.Bmp);
return ms.ToArray();
}
}
set { // deserialize
if (value == null) {
LargeIcon = null;
} else {
using (MemoryStream ms = new MemoryStream(value)) {
LargeIcon = new Bitmap(ms);
}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)