ASP.NET将图片存储和读取
-
ASP.NET 将图片存储在xml中
///<summary>
///将图片转为byte[]存储在xml中
///</summary>
publicvoid PicToBtye()
{
XmlDocumentmyXmlDoc = newXmlDocument();
myXmlDoc.LoadXml("<picture></picture>");
XmlElementelem = myXmlDoc.CreateElement("image");
//打开图片文件,利用图片构造一个文件流
FileStreamfs = newFileStream("E://1.jpg", FileMode.Open);
//使用文件流构造一个二进制读取器将基元数据读作二进制值
BinaryReaderbr = newBinaryReader(fs);
byte[]imageBuffer = newbyte[br.BaseStream.Length];
br.Read(imageBuffer, 0, Convert.ToInt32(br.BaseStream.Length));
stringtextstring = System.Convert.ToBase64String(imageBuffer);
fs.Close();
br.Close();
XmlTexttext = myXmlDoc.CreateTextNode(textstring);
myXmlDoc.DocumentElement.AppendChild(elem);
myXmlDoc.DocumentElement.LastChild.AppendChild(text);
myXmlDoc.Save("DocSave.xml");
}
-
ASP.NET 从xml中将图片读取
最近在做WebService,一般返回XML文件,如何将xml文件中的二进制流转换为图片
///<summary>
///通过WebService请求将xml中的byte[]转换为相应的文件形式
///</summary>
///<paramname="url">访问的网址</param>
///<paramname="param">请求的参数</param>
publicvoidBtyeToPic(string url, stringparam)
{
stringfilename = "2.jpg";
intreadByte = 0;
intbytesdToRead = 1044;
byte[]parabt = Encoding.GetEncoding("gb2312").GetBytes(param);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = 30000;
request.ContentLength = parabt.Length;
Streamstreampara = request.GetRequestStream();
streampara.Write(parabt, 0, parabt.Length);
WebResponse response = request.GetResponse();
Stream streamResult = response.GetResponseStream();
//如果加载本地的文件,可以直接写成 XmlTextReader xmlTxtRd = newXmlTextReader("http://222.24.63.109/Power/PubInfo.xml");
XmlTextReader xmlTxtRd = newXmlTextReader(streamResult);
byte[]base64buffer = newbyte[bytesdToRead];
while(xmlTxtRd.Read())
{
if (xmlTxtRd.NodeType == XmlNodeType.Element && xmlTxtRd.LocalName == "File_name")
{
filename = xmlTxtRd.ReadString();
}
if (xmlTxtRd.NodeType == XmlNodeType.Element && xmlTxtRd.Name == "Attachment")
{
FileStream fs = newFileStream(filename, FileMode.Create);
BinaryWriter bw = newBinaryWriter(fs);
do
{
readByte =xmlTxtRd.ReadBase64(base64buffer, 0, bytesdToRead);
bw.Write(base64buffer,0, readByte);
} while (bytesdToRead <= readByte);
bw.Flush();
bw.Close();
}
}
}
-
ASP.NET 将图片存储在数据库中
///<summary>
///将图片以二进制文件流存储到数据库中
///</summary>
publicvoidPicToBtyeSaveDB()
{
System.IO.FileStreamfs = new System.IO.FileStream("E://1.jpg", System.IO.FileMode.Open);
byte[]imgData = newbyte[fs.Length];
fs.Read(imgData, 0, (int)fs.Length);
stringsql = "insert into system_pubInfo(AttachMent)values('" + imgData + "') wherepub_id=3";
stringstrConn = "Server=WORK-PC\\SQLEXPRESS;DataBase=SXDW;IntegratedSecurity=true";
SqlConnectionconnection = newSqlConnection(strConn);
connection.Open();
SqlCommandcm = newSqlCommand(sql,connection);
SqlDataReaderreader = cm.ExecuteReader();
reader.Close();
}
-
ASP.NET 从数据库中将图片读取
///<summary>
///将数据库中的二进制文件流独处并显示成相对应的形式
///</summary>
publicvoidDBToPic()
{
byte[]streamByte = null;
stringsql = string.Format("selectAttachMent from system_pubInfo whereType_ID=1");
stringstrConn = "Server=WORK-PC\\SQLEXPRESS;DataBase=SXDW;IntegratedSecurity=true";
SqlConnection connection = newSqlConnection(strConn);
connection.Open();
SqlCommand cm = newSqlCommand(sql,connection);
SqlDataReader reader = cm.ExecuteReader();
while(reader.Read())
{
streamByte = reader["AttachMent"]isSystem.DBNull ? null: (byte[])reader["AttachMent"];
}
MemoryStream ms = newSystem.IO.MemoryStream(streamByte);
System.Drawing.Image img =System.Drawing.Image.FromStream(ms);
pictureBox1.Image = img;
}