asp.net 将一个图片以二进制值的形式存入Xml文件中的实例代码

时间:2023-07-23 13:31:30 


try
   {
    int readByte = 0;        //
    int bytesToRead = 100;       //数据缓冲区大小
    string fileName = "../../WriteXml.xml";   //要打开的文件
    //   this.textBox1.Text = string.Empty;           
    // 打开图片文件,利用该图片构造一个文件流
    FileStream fs = new FileStream("../../001.jpg",FileMode.Open);
    // 使用文件流构造一个二进制读取器将基元数据读作二进制值
    BinaryReader br = new BinaryReader(fs);
    XmlTextWriter xmlTxtWt = new XmlTextWriter(fileName,Encoding.UTF8);
    //输出设置 代码缩进
    xmlTxtWt.Formatting = Formatting.Indented;
    //   xmlTxtWt.Indentation = 4;
    //书写声明
    xmlTxtWt.WriteStartDocument();
    xmlTxtWt.WriteStartElement("picture","ContactDetails","https://www.aspxhome.com");//定义命名空间
    xmlTxtWt.WriteStartElement("image");            //定义节点
    xmlTxtWt.WriteAttributeString("imageName","002.jpg");        //添加图片属性
    byte[] base64buffer = new byte[bytesToRead];          //开辟缓冲区
    do
    {
     readByte = br.Read(base64buffer,0,bytesToRead);      //将数据读入字节数组
     xmlTxtWt.WriteBase64(base64buffer,0,readByte);       //将数组中二进制值编码为Base64并写出到XML文件
    }while(bytesToRead <= readByte);
    xmlTxtWt.WriteEndElement();
    xmlTxtWt.WriteEndElement();
    xmlTxtWt.WriteEndDocument();
//    xmlTxtWt.Flush();
    xmlTxtWt.Close();
    MessageBox.Show("读写结束!");
    //   this.textBox1.Text = ReadXml(fileName);
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.ToString());
   }
标签:图片,二进制,Xml
0
投稿

猜你喜欢

  • Python标准库之随机数 (math包、random包)介绍

    2021-09-26 11:57:08
  • 在HTML中,常见的URL有多种表示方式:

    2009-07-28 12:18:00
  • Python中使用第三方库xlrd来写入Excel文件示例

    2023-08-16 21:37:29
  • 教你用压缩技术给SQL Server备份文件瘦身

    2009-03-05 14:59:00
  • python数据结构算法分析

    2022-06-11 02:57:15
  • Python 从attribute到property详解

    2022-03-15 18:52:40
  • 如何将anaconda安装配置的mmdetection环境离线拷贝到另一台电脑

    2022-12-16 00:48:47
  • python中关于CIFAR10数据集的使用

    2021-04-14 22:08:05
  • python中str内置函数用法总结

    2022-06-23 10:22:45
  • vue服务端渲染添加缓存的方法

    2024-04-30 10:37:30
  • python全面解析接口返回数据

    2023-09-15 17:02:28
  • Go语言编程中字符串切割方法小结

    2023-06-16 01:41:24
  • python实现中文转换url编码的方法

    2021-05-26 08:46:57
  • Python的Flask项目中获取请求用户IP地址 addr问题

    2021-09-08 08:38:47
  • 使用Python实现从各个子文件夹中复制指定文件的方法

    2023-11-09 12:04:05
  • 使用开源Cesium+Vue实现倾斜摄影三维展示功能

    2024-05-28 15:51:51
  • python 使用 with open() as 读写文件的操作方法

    2021-05-08 08:32:20
  • mysql 队列 实现并发读

    2024-01-14 21:16:26
  • Python下载指定页面上图片的方法

    2023-08-16 09:32:37
  • SQLServer 2000 数据库同步详细步骤[两台服务器]

    2024-01-21 11:18:03
  • asp之家 网络编程 m.aspxhome.com