将序列化图像类视为图像数据字段(DataSet 作为 DataGridView 的数据源)

发布于 2024-12-04 21:14:50 字数 2247 浏览 1 评论 0原文

首先,感谢您阅读我的问题。

背景

我正在开发一个 DataGridView,它使用预先存在的 XML 文件作为数据源:

public DataSet TableDatabase = new DataSet();
TableDatabase.ReadXml("xml_file.xml");
(DataGridView)DbTable.DataSource = TableDatabase.Tables[0];

基本 xml 文件是一个带有 id、int、text 的通用数据库。

我想做的

我基本上想做的是添加一个类型为 Bitmap 的新列(来自剪贴板)并将其序列化为Base64“字符串”。

如果我使用第一行“typeof(Bitmap)”,图像数据将显示为图像,但不知道如何序列化它......

// DataColumn col = TableDatabase.Tables[0].Columns.Add("columnname", typeof(Bitmap));
DataColumn col = TableDatabase.Tables[0].Columns.Add("columnname", typeof(MyImage));

我创建了一个名为“MyImage”的类(基于 将 C#/.NET 中的位图序列化为 XML):

[Serializable]
public class MyImage : IXmlSerializable
{
    public Bitmap Image { get; set; }
    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }
    public void ReadXml(System.Xml.XmlReader reader)
    {
        reader.ReadStartElement();
        MemoryStream ms = null;
        byte[] buffer = new byte[256];
        int bytesRead;
        while ((bytesRead = reader.ReadContentAsBase64(buffer, 0, buffer.Length)) > 0)
        {
            if (ms == null)
            {
                ms = new MemoryStream(bytesRead);
            }
            ms.Write(buffer, 0, bytesRead);
        }
        if (ms != null)
        {
            Image = (Bitmap)System.Drawing.Image.FromStream(ms,true,true);
        }
        reader.ReadEndElement();  
    }
    public void WriteXml(System.Xml.XmlWriter writer)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            Image.Save(ms, ImageFormat.Bmp);
            byte[] bitmapData = ms.ToArray();
            writer.WriteBase64(bitmapData, 0, bitmapData.Length);
        }
    }
}

它工作得非常好,我可以负载和在 xml 中写入图像数据:

<columnname>...base64 data...</columnname>


问题

DataGridView 中的数据字段不会显示为图像:(

它只显示一个字符串:“Namespace.MyImage”。所以我怎样才能告诉 DataCell 显示图像?

At first, thanks for reading my problem.

The Background

Im working on a DataGridView which use a pre-existing XML file as datasource:

public DataSet TableDatabase = new DataSet();
TableDatabase.ReadXml("xml_file.xml");
(DataGridView)DbTable.DataSource = TableDatabase.Tables[0];

The base xml file is a generic database with id, int, text.

What I want to do

What I basicly want to do, is to add a new column with typeof Bitmap (source from clipboard) and serialize it as Base64 "string".

If I use the first line "typeof(Bitmap)", the image data will be shown as image, but no idea how to serialize it...

// DataColumn col = TableDatabase.Tables[0].Columns.Add("columnname", typeof(Bitmap));
DataColumn col = TableDatabase.Tables[0].Columns.Add("columnname", typeof(MyImage));

I made a Class called "MyImage" (based on Serialize a Bitmap in C#/.NET to XML):

[Serializable]
public class MyImage : IXmlSerializable
{
    public Bitmap Image { get; set; }
    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }
    public void ReadXml(System.Xml.XmlReader reader)
    {
        reader.ReadStartElement();
        MemoryStream ms = null;
        byte[] buffer = new byte[256];
        int bytesRead;
        while ((bytesRead = reader.ReadContentAsBase64(buffer, 0, buffer.Length)) > 0)
        {
            if (ms == null)
            {
                ms = new MemoryStream(bytesRead);
            }
            ms.Write(buffer, 0, bytesRead);
        }
        if (ms != null)
        {
            Image = (Bitmap)System.Drawing.Image.FromStream(ms,true,true);
        }
        reader.ReadEndElement();  
    }
    public void WriteXml(System.Xml.XmlWriter writer)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            Image.Save(ms, ImageFormat.Bmp);
            byte[] bitmapData = ms.ToArray();
            writer.WriteBase64(bitmapData, 0, bitmapData.Length);
        }
    }
}

Its working really nice, I can load and write the imagedata in xml:

<columnname>...base64 data...</columnname>

The Problem

The Datafield in the DataGridView will not show as image :(

It just shows a string: "Namespace.MyImage". So how can I tell the DataCell to show the image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事和酒 2024-12-11 21:14:50

问题在于

 msdata:DataType="NAMESPACE.MyImage, APPNAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 

Cast 到 MyImage 类,同时

 msdata:DataType="System.Drawing.Bitmap, APPNAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 

会强制转换查看图像的方式。所以我需要知道如何定义 MyImage 目标是位图...:(

已解决:

我真的很愚蠢;)

    [XmlIgnoreAttribute()]
    public Bitmap Picture = new Bitmap(1, 1);

    // Serializes the 'Picture' Bitmap to XML.
    [XmlElementAttribute("Picture")]
    public byte[] PictureByteArray
    {
        get
        {
            TypeConverter BitmapConverter = TypeDescriptor.GetConverter(Picture.GetType());
            return (byte[])BitmapConverter.ConvertTo(Picture, typeof(byte[]));
        }

        set
        {
            Picture = new Bitmap(new MemoryStream(value));
        }
    }

而细胞:

DataColumn col = TableDatabase.Tables[0].Columns.Add("columnname", PictureByteArray.GetType());

The problem is that

 msdata:DataType="NAMESPACE.MyImage, APPNAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 

Cast to MyImage Class, while

 msdata:DataType="System.Drawing.Bitmap, APPNAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 

would cast the way to see the image. So I need to know how to define that MyImage target is a Bitmap... :(

Solved:

I was really stupid ;)

    [XmlIgnoreAttribute()]
    public Bitmap Picture = new Bitmap(1, 1);

    // Serializes the 'Picture' Bitmap to XML.
    [XmlElementAttribute("Picture")]
    public byte[] PictureByteArray
    {
        get
        {
            TypeConverter BitmapConverter = TypeDescriptor.GetConverter(Picture.GetType());
            return (byte[])BitmapConverter.ConvertTo(Picture, typeof(byte[]));
        }

        set
        {
            Picture = new Bitmap(new MemoryStream(value));
        }
    }

And the Cell:

DataColumn col = TableDatabase.Tables[0].Columns.Add("columnname", PictureByteArray.GetType());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文