将序列化图像类视为图像数据字段(DataSet 作为 DataGridView 的数据源)
首先,感谢您阅读我的问题。
背景
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于
Cast 到 MyImage 类,同时
会强制转换查看图像的方式。所以我需要知道如何定义 MyImage 目标是位图...:(
已解决:
我真的很愚蠢;)
而细胞:
The problem is that
Cast to MyImage Class, while
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 ;)
And the Cell: