将C#/NET中的位图序列化为XML

浏览:46日期:2024-03-17
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解决将C#/NET中的位图序列化为XML?

我会做类似的事情:

[XmlIgnore]public Bitmap LargeIcon { get; set; }[browsable(false),Editorbrowsable(EditorbrowsableState.Never)][XmlElement('LargeIcon')]public byte[] LargeIconSerialized{ get { // serializeif (LargeIcon == null) return null;using (MemoryStream ms = new MemoryStream()) { LargeIcon.Save(ms, ImageFormat.Bmp); return ms.ToArray();} } set { // deserializeif (value == null) { LargeIcon = null;} else { using (MemoryStream ms = new MemoryStream(value)) {LargeIcon = new Bitmap(ms); }} }}解决方法

我想对一个复杂类型(类)进行 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序列化器序列化位图是行不通的,因为它没有公共的无参数构造函数,而默认的XML序列化器是必需的。

我知道以下几点:

有一个解决方法,发布在这里:http : //www.dotnetspider.com/resources/4759-XML-Serialization-C-Part-II-Images.aspx 。但是,由于这包括添加其他属性,在我看来,这似乎有点hack。在sourceforge上还有一个深入的XML序列化项目。

我宁愿不引用其他项目,也不希望广泛地调整类,以仅允许这些位图的xml序列化。

有没有办法保持简单?

非常感谢,马塞尔

相关文章: