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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1907077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:46:01  来源:igfitidea点击:

Serialize a Bitmap in C#/.NET to XML

c#xml-serializationbitmap

提问by Marcel

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

我想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; }

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.

我现在发现用默认的 XML 序列化器序列化位图不起作用,因为它没有公共无参数构造函数,这是默认的 xml 序列化器所必需的。

I am aware of the following:

我知道以下几点:

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

我宁愿不喜欢引用另一个项目,也不喜欢大量调整我的类以只允许这些位图的 xml 序列化。

Is there no way to keep that simple?

没有办法保持那么简单吗?

Many thanks, Marcel

非常感谢,马塞尔

采纳答案by Marc Gravell

I would do something like:

我会做这样的事情:

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

回答by John Saunders

The BitMap class has not been designed to be easily XML Serialized. So, no, there's not simple way to correct a design decision.

BitMap 类尚未设计为易于 XML 序列化。所以,不,没有简单的方法来纠正设计决策。

回答by Rubens Farias

You can also to implement ISerializableand to use SerializationInfoto deal manually with your bitmap content.

您还可以实现ISerializable和使用SerializationInfo手动处理您的位图内容。

EDIT:Jo?o is right: Correct way to deal with XML serialization is to implement IXmlSerializable, not ISerializable:

编辑:Jo?o 是对的:处理 XML 序列化的正确方法是实现IXmlSerializable,而不是ISerializable

public class MyImage : IXmlSerializable
{
    public string Name  { get; set; }
    public Bitmap Image { get; set; }

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        throw new NotImplementedException();
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        throw new NotImplementedException();
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteStartElement("Name");
        writer.WriteString(this.Name);
        writer.WriteEndElement();

        using(MemoryStream ms = new MemoryStream())
        {
            this.Image.Save(ms, ImageFormat.Bmp );
            byte[] bitmapData = ms.ToArray();
            writer.WriteStartElement("Image");
            writer.WriteBase64(bitmapData, 0, bitmapData.Length);
            writer.WriteEndElement();
        }
    }
}

回答by Jo?o Angelo

Implement IXmlSerializableand then handle all the serialization details yourself.

自己实现IXmlSerializable并处理所有序列化细节。

Since you say it's a large type and you only have a problem with the bitmap consider doing something like this:

既然你说它是一个大类型并且你只有位图有问题,请考虑做这样的事情:

public class BitmapContainer : IXmlSerializable
{
    public BitmapContainer() { }

    public Bitmap Data { get; set; }

    public XmlSchema GetSchema()
    {
        throw new NotImplementedException();
    }

    public void ReadXml(XmlReader reader)
    {
        throw new NotImplementedException();
    }

    public void WriteXml(XmlWriter writer)
    {
        throw new NotImplementedException();
    }
}

public class TypeWithBitmap
{
    public BitmapContainer MyImage { get; set; }

    public string Name { get; set; }
}