.Net中将图片数据保存到XML文档
2005-04-07 10:37
267 查看
因为最近要做的项目中,我要通过XML动态生成窗体,看了UI图样,我有些叫苦:我通过XML动态生成窗体,可是主窗体中UI要用图标来确定要使用的窗体,怎么才能使主窗体的图标也是动态加载而且图标和要生成的窗体还有关联呢?我又想到用XML,查MSDN,看到只有XmlTextWriter和XmlTextReader里分别有XmlTextWriter.WriteBase64和XmlTextReader.ReadBase64可以操作图片这种二进制字节的数据。但是XmlTextWriter和XmlTextReader远不如XmlDocument操作方便,如果用这两者我就得写太多的代码。
困扰了我一天,记得以前看到过一篇文章介绍怎样将图片数据存储到Xml文件,可是怎么也找不到,后来终于在一个英文网站上找到了相关内容,而且还是2003年贴出来的,汗。
好了,不废话了,我把我的实现代码贴给大家吧。源代码下载
private XmlDocument document;
private string FilePath = Application.StartupPath + "\\..\\..\\FormStyle.xml"; // FormStyle.xml 文件地址
private void frmMain_Load(object sender, System.EventArgs e)
private void lbIcons_SelectedValueChanged(object sender, System.EventArgs e)
private void btnAdd_Click(object sender, System.EventArgs e)
{
// 如果不存在txtFilePath.Text所指文件,就退出
if(!File.Exists(txtFilePath.Text) || lbIcons.Items.Count == 0)
return;
if(lbIcons.SelectedIndex == -1)
lbIcons.SelectedIndex = 0;
if(document == null)
{
document = new XmlDocument();
document.Load(FilePath);
}
//Read the bitmap.
string data = null;
Bitmap bmp = new Bitmap(txtFilePath.Text);
using (MemoryStream mem = new MemoryStream())
{
bmp.Save(mem, System.Drawing.Imaging.ImageFormat.Bmp);
// 将位图数据转换为Base64String放入字符串中
data = Convert.ToBase64String(mem.ToArray());
}
// 查找当前所选的窗体是否含有Image节点,若就新建一个
XmlNode node = document.DocumentElement.SelectSingleNode(string.Format("descendant::Form[@Name='{0}']", lbIcons.SelectedItem.ToString()));
XmlNode ImageNode = document.DocumentElement.SelectSingleNode(string.Format("descendant::Form[@Name='{0}']/Image", lbIcons.SelectedItem.ToString()));
if(ImageNode == null)
{
ImageNode = document.CreateElement("Image");
node.AppendChild(ImageNode);
}
// 将位图数据保存到XML文档
ImageNode.InnerText = data;
document.Save(FilePath);
}
困扰了我一天,记得以前看到过一篇文章介绍怎样将图片数据存储到Xml文件,可是怎么也找不到,后来终于在一个英文网站上找到了相关内容,而且还是2003年贴出来的,汗。
好了,不废话了,我把我的实现代码贴给大家吧。源代码下载
private XmlDocument document;
private string FilePath = Application.StartupPath + "\\..\\..\\FormStyle.xml"; // FormStyle.xml 文件地址
private void frmMain_Load(object sender, System.EventArgs e)
private void lbIcons_SelectedValueChanged(object sender, System.EventArgs e)
private void btnAdd_Click(object sender, System.EventArgs e)
{
// 如果不存在txtFilePath.Text所指文件,就退出
if(!File.Exists(txtFilePath.Text) || lbIcons.Items.Count == 0)
return;
if(lbIcons.SelectedIndex == -1)
lbIcons.SelectedIndex = 0;
if(document == null)
{
document = new XmlDocument();
document.Load(FilePath);
}
//Read the bitmap.
string data = null;
Bitmap bmp = new Bitmap(txtFilePath.Text);
using (MemoryStream mem = new MemoryStream())
{
bmp.Save(mem, System.Drawing.Imaging.ImageFormat.Bmp);
// 将位图数据转换为Base64String放入字符串中
data = Convert.ToBase64String(mem.ToArray());
}
// 查找当前所选的窗体是否含有Image节点,若就新建一个
XmlNode node = document.DocumentElement.SelectSingleNode(string.Format("descendant::Form[@Name='{0}']", lbIcons.SelectedItem.ToString()));
XmlNode ImageNode = document.DocumentElement.SelectSingleNode(string.Format("descendant::Form[@Name='{0}']/Image", lbIcons.SelectedItem.ToString()));
if(ImageNode == null)
{
ImageNode = document.CreateElement("Image");
node.AppendChild(ImageNode);
}
// 将位图数据保存到XML文档
ImageNode.InnerText = data;
document.Save(FilePath);
}
相关文章推荐
- 基于.net 的oracleOracle数据库图片保存,byte数据和image数据的转换
- C++中将数据保存成jpg格式图片
- jquery的ajaxSubmit()异步上传图片并保存表单数据演示代码
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- OpenCV中将视频保存为连续的图片和连续的图片保存为视频
- python中将手写数据数组转换为图片输入出来
- android创建数据库(SQLite)保存图片示例ZZ 分类: Android数据存储 2015-03-22 09:58 41人阅读 评论(0) 收藏
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- 如何通过表单上传图片数据,PHP后端获取并且保存文件数据
- php保存二进制数据为图片
- vc/mfc获取rgb图像数据后动态显示及保存图片的方法
- php保存二进制原始数据为图片的程序代码
- 海康威视API-单帧数据捕获并保存成JPEG图片NET_DVR_CaptureJPEGPicture
- 将控件中的数据输出保存到本地excel或word中,同时保存图片到本地(c#)
- Retrofit+RxJava+OkHttp封装类请求接口数据→原生登录,注册,个人信息页+设置用户头像(图片上传或拍照)+SharedPreferences保存信息+Fresco加载图片+Mvp分层
- java中将数据对象序列化后保存到数据库中,并反序列化取出显示
- 在OpenLayers4中将地图及叠加显示的图层生成图片保存到数据库方法
- Opencv中将视频保存为连续的图片和连续的图片保存为视频
- blob类型的数据,可以保存图片,直接把图片转化为代码,和text不同,editkindor常用到
- iPhone开发笔记(12)利用.net实现服务端接收上传的图片并保存