在C#/。NET序列化的位图。到XML [英] Serialize a Bitmap in C#/.NET to XML

查看:157
本文介绍了在C#/。NET序列化的位图。到XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要的 XML序列化的复杂类型(类),有型System.Drawing.Bitmap之中他人的属性即可。

I want to XML-Serialize a complex type (class), that has a property of type System.Drawing.Bitmap among others.

    /// <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序列化。

I now have found out that serializing the Bitmap with the default XML serializer does not work, because it does not have a public parameterless constructor, which is mandatory with the default xml serializer.

我注意以下事项:


  • 存在一个解决方法,贴在这里:<一href=\"http://www.dotnetspider.com/resources/4759-XML-Serialization-C-Part-II-Images.aspx\">http://www.dotnetspider.com/resources/4759-XML-Serialization-C-Part-II-Images.aspx
    。但是,由于这包括添加另一个属性,这似乎给了我一个黑客位的。

  • 还有一个很深的XML在SourceForge上项目序列。

我宁愿不喜欢引用另一个项目也广泛地调整我的课的,只允许这些位图的XML序列化。

I rather would not like referencing another project nor extensively tweak my class to just allow xml serialization of those bitmaps.

有没有办法让这么简单?

非常感谢,马塞尔

推荐答案

我会做这样的事情:

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

这篇关于在C#/。NET序列化的位图。到XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆