net9:图片变成二进制流存入XML文档,从XML文档中读出图片以及从XML文档中读取并创建图片文件

原文发布时间为:2008-08-10 —— 来源于本人的百度文章 [由搬家工具导入]

fileToXml类:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.IO;
using System.Xml;

/// <summary>
/// fileToXml 的摘要说明
/// </summary>
public class fileToXml
{
public fileToXml()
{
   //
   // TODO: 在此处添加构造函数逻辑
   //
}
    public static void toXml(FileUpload fu, string xmlpath, Guid id)
    {
        if (fu.HasFile)
        {
            string fpath = fu.FileName;
            int fileLength = fu.PostedFile.ContentLength;
           Byte[] fileBytes = new Byte[fileLength];
            Stream strm = fu.PostedFile.InputStream;
            strm.Read(fileBytes, 0, fileLength);
            XmlDocument dom = new XmlDocument();
            if (!File.Exists(xmlpath))
            {
                XmlDeclaration xdec = dom.CreateXmlDeclaration("1.0", "utf-8", null);
                dom.AppendChild(xdec);

                XmlElement root = dom.CreateElement("File");
                dom.AppendChild(root);

                XmlElement father = dom.CreateElement("Image");
                root.AppendChild(father);

                XmlElement guid = dom.CreateElement("Guid");
                guid.InnerText = id.ToString();
                father.AppendChild(guid);

                XmlElement imgData = dom.CreateElement("imgData");
                imgData.InnerText = Convert.ToBase64String(fileBytes);
                father.AppendChild(imgData);

                dom.Save(xmlpath);
            }
            else
            {

                dom.Load(xmlpath);
                XmlNode root = dom.SelectSingleNode("File");
                XmlElement father = dom.CreateElement("Image");
                root.AppendChild(father);

                XmlElement guid = dom.CreateElement("Guid");
                guid.InnerText = id.ToString();
                father.AppendChild(guid);

                XmlElement imgData = dom.CreateElement("imgData");
                imgData.InnerText = Convert.ToBase64String(fu.FileBytes);//这是利用fileupload控件本身的方法获取二进制流
                father.AppendChild(imgData);

                dom.Save(xmlpath);
            }
        }
    }
}
------------------------------

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;
using System.IO;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string xmlpath = Server.MapPath("~/App_Data/imgToBry.xml");
        Guid gid = Guid.NewGuid();
        fileToXml.toXml(FileUpload1, xmlpath, gid);

        Session["gid"] = gid;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        XmlDocument dom = new XmlDocument();
        dom.Load(Server.MapPath("~/App_Data/imgToBry.xml"));
        XmlNodeList xnl = dom.SelectSingleNode("//Image[Guid='" + Session["gid"].ToString() + "']").ChildNodes;
        for (int i = 0; i < xnl.Count; i++) ;
        {
            string imgdata = xnl.Item(1).InnerText;
            Response.OutputStream.Write(Convert.FromBase64String(imgdata),0,imgdata.Length);
            Response.End();
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        XmlDocument dom = new XmlDocument();
        dom.Load(Server.MapPath("~/App_Data/imgToBry.xml"));
        XmlNodeList xnl = dom.SelectSingleNode("//Image[Guid='" + Session["gid"].ToString() + "']").ChildNodes;
        for (int i = 0; i < xnl.Count; i++) ;
        {
            string imgdata = xnl.Item(1).InnerText;
            FileStream fs = new FileStream(Server.MapPath("~/xml.gif"), FileMode.Create, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(Convert.FromBase64String(imgdata));
            bw.Close();
            fs.Close();           
        }
        Image1.ImageUrl="~/xml.gif";
    }
}

时间: 2024-12-08 16:04:37

net9:图片变成二进制流存入XML文档,从XML文档中读出图片以及从XML文档中读取并创建图片文件的相关文章

ASP.Net将图片以二进制方式存入数据库,并读取

把图片转换成二进制--把二进制转换成图片 private void button1_Click(object sender, EventArgs e) { string path = this.textBox1.Text; byte[] imgBytesIn = SaveImage(path); ShowImgByByte(imgBytesIn); //Parameters.Add("@Photo", SqlDbType.Binary).Value = imgBytesIn; } //

【转载】C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte

C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte 转载:http://www.itdos.com/Mvc/20150302/0741255.html using System.IO; /// <summary> /// WebApi返回图片 /// </summary> public HttpResponseMessage GetQrCode() { var imgPath = @"D:\ITdosCom\Images

图片转二进制流存储到数据库

1.数据库名为Demo,数据结构如图 2.后台代码如下 class Program { public static readonly string conStr = "Data Source = .;Initial Catalog = Demo;Integrated Security = SSPI;"; static void Main(string[] args) { #region 图片转为二进制流写进数据库 byte[] buffer = ImageToByte(@"G

使用C#向Sql Sever中存取网络图片和本地图片(二进制流的形式)

先是做普通的,存储我们本地的图片,将它转化为二进制流存储到数据库对应的表中. 代码如下: string path = "../../A.jpg"; FileStream fs = new FileStream(path, FileMode.Open); int streamLength = (int)fs.Length; //获取文件流的长度. byte[] image = new byte[streamLength]; //声明字节数组,用于保存图片文件 fs.Read(image,

net9:图片文件转换成二进制流存入SQL数据库,以及从数据库中读取二进制流输出文件

原文发布时间为:2008-08-10 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebContro

iOS中用json接受图片的二进制流

标题可能说的有点混乱,再好好描述一下我遇到的问题: 我负责做一款App的iOS版本,服务器和Android版本都开发完了.服务器的图片存的不是路径,而是在数据库中的blob流对象,由于要求所以数据都用json传值,图片服务器端的处理方式是把图片二进制流转化成字符串.服务器和Android都是java开发的,很方便,直接都用jackson封装和解析就OK了,而iOS端说死也不能把这个字符串解析成图片! 不知道大家看到这能否直接发现问题出在哪里,反正我是被这个问题折磨了将近两天. 思路 1 起初我觉

用NOPI将图片二进制流导出到Excel

这儿采取的是将图片的二进制流导出到Excel,直接上代码: /// <summary> /// DataTable导出到Excel的MemoryStream /// </summary> /// <param name="dtSource">源DataTable</param> /// <param name="strHeaderText">表头文本</param> /// <param

php读取图片以二进制输出

header( "Content-type: image/jpeg");$PSize = filesize('1.jpg');$picturedata = fread(fopen('1.jpg', "r"), $PSize);echo $picturedata; 呵呵.就这么几句话,就将图片以二进制流的形式输出到客户端了,和打开一张图片没有任何区别,需要注意的是,发送的header要根据具体情况而定,不一定都是image/jpeg.JPG的是它,但PNG的就是ima

C#程序中将图片转换为二进制字符串,并将二进制字符串转换为图片

/// <summary> /// 将图片以二进制流 /// </summary> /// <param name="path"></param> /// <returns></returns> public string SaveImage(String path) { FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); //