用XML保存和读取图片

 1 None.gif protected   void  Page_Load( object  sender, System.EventArgs e)
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 3InBlock.gif            int ImgID = Convert.ToInt32(Request.QueryString["ID"]); //ID为图片ID 
 4InBlock.gif            //建立数据库链接
 5InBlock.gif            string fileName = Server.MapPath(".\\WriteXml.xml");   //要打开的文件
 6InBlock.gif            
 7InBlock.gif            XmlDocument xmlDoc = new XmlDocument();
 8InBlock.gif            xmlDoc.Load(fileName);
 9InBlock.gif            XmlNodeList node =  xmlDoc.SelectSingleNode("//Image[ImageID='"+ImgID.ToString()+"']").ChildNodes;
10InBlock.gif            if(node!=null)
11ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
12InBlock.gif                string strType = node.Item(1).InnerText;
13InBlock.gif                string strData =node.Item(4).InnerText;
14InBlock.gif                int nSize = int.Parse(node.Item(2).InnerText);
15InBlock.gif
16InBlock.gif                Response.ContentType = strType;//设定输出文件类型
17InBlock.gif                //输出图象文件二进制数制
18InBlock.gif                Response.OutputStream.Write(Convert.FromBase64String(strData), 0, nSize); 
19InBlock.gif                Response.End();
20InBlock.gif                //也可以保存为图像
21InBlock.gif                //            FileStream fs = new FileStream(@"C:\aa.BMP", FileMode.OpenOrCreate, FileAccess.Write);
22InBlock.gif                //            fs.Write((Convert.FromBase64String(strData), 0,nSize);
23InBlock.gif                //            fs.Close();
24ExpandedSubBlockEnd.gif            }

25InBlock.gif
26ExpandedBlockEnd.gif        }


保存
 1 None.gif protected   void  btnUpload_Click( object  sender, System.EventArgs e)
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 3InBlock.gif            //得到用户要上传的文件名
 4InBlock.gif            string strFilePathName = loFile.PostedFile.FileName;
 5InBlock.gif            string strFileName = Path.GetFileName(strFilePathName);
 6InBlock.gif            int FileLength = loFile.PostedFile.ContentLength;
 7InBlock.gif            if(FileLength<=0)
 8InBlock.gif                return;
 9InBlock.gif            try
10ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
11InBlock.gif                Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组
12InBlock.gif                Stream StreamObject = loFile.PostedFile.InputStream; //建立数据流对像
13InBlock.gif                //读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
14InBlock.gif                StreamObject.Read(FileByteArray,0,FileLength); 
15InBlock.gif                string fileName = Server.MapPath(".\\WriteXml.xml");   //要打开的文件
16InBlock.gif            
17InBlock.gif                XmlDocument xmlDoc = new XmlDocument();
18InBlock.gif                xmlDoc.Load(fileName);
19InBlock.gif                XmlNode root=xmlDoc.SelectSingleNode("dbImage");//查找<dbGuest>
20InBlock.gif                XmlNodeList xnl=xmlDoc.SelectSingleNode("dbImage").ChildNodes;
21InBlock.gif                int nIndex = xnl.Count;
22InBlock.gif                //以下添加新结点
23InBlock.gif                XmlElement xe1=xmlDoc.CreateElement("Image");//创建一个<User>节点
24InBlock.gif                XmlElement xesub1=xmlDoc.CreateElement("ImageID");
25InBlock.gif                xesub1.InnerText=nIndex.ToString();//设置文本节点
26InBlock.gif                xe1.AppendChild(xesub1);//添加到<User>节点中
27InBlock.gif                XmlElement xesub2=xmlDoc.CreateElement("ImageContentType");
28InBlock.gif                xesub2.InnerText=loFile.PostedFile.ContentType;
29InBlock.gif                xe1.AppendChild(xesub2);
30InBlock.gif                XmlElement xesub3=xmlDoc.CreateElement("ImageSize");
31InBlock.gif                xesub3.InnerText=FileLength.ToString();
32InBlock.gif                xe1.AppendChild(xesub3);
33InBlock.gif                XmlElement xesub4=xmlDoc.CreateElement("ImageDescription");
34InBlock.gif                xesub4.InnerText=tbDescription.Text;
35InBlock.gif                xe1.AppendChild(xesub4);
36InBlock.gif                XmlElement xesub5=xmlDoc.CreateElement("ImageData");
37InBlock.gif                xesub5.InnerText= Convert.ToBase64String(FileByteArray);
38InBlock.gif                xe1.AppendChild(xesub5);
39InBlock.gif
40InBlock.gif
41InBlock.gif                root.AppendChild(xe1);//添加到<dbGuest>节点中
42InBlock.gif                xmlDoc.Save(fileName);
43InBlock.gif            
44InBlock.gif                Response.Redirect("ShowAllImg.aspx");
45ExpandedSubBlockEnd.gif            }

46InBlock.gif            catch
47ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
48ExpandedSubBlockEnd.gif            }

49ExpandedBlockEnd.gif        }


读取

转载于:https://www.cnblogs.com/zwl12549/archive/2007/01/11/617809.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值