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?