1

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?

Community
  • 1
  • 1
7three
  • 11
  • 3

1 Answers1

0

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());
7three
  • 11
  • 3