图片文件序列化之后通过XML传输
序列化
Bitmap bitMap = new Bitmap(filename);
MemoryStream ms = new MemoryStream();
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xw.Formatting = Formatting.Indented;
XmlSerializer ser = new XmlSerializer(ms.GetBuffer().GetType());
ser.Serialize(xw, ms.GetBuffer());
string xmlString = sw.GetStringBuilder().ToString();
xw.Close();
sw.Close();
ms.Close();
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
if (doc != null && doc.ChildNodes.Count > 1 && doc.ChildNodes[1] != null)
{
XmlNode node = doc.ChildNodes[1];
photoString = node.InnerText;
}
photoString 就是序列化之后的信息
反序列化
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version="1.0" encoding="utf-16"?>\r\n<base64Binary>");
sb.Append(photoString);
sb.Append("</base64Binary>");
XmlTextReader xrw = new XmlTextReader(new StringReader(sb.ToString()));
MemoryStream ms = new MemoryStream();
XmlSerializer ser = new XmlSerializer(ms.GetBuffer().GetType());
Bitmap bitMap = new Bitmap(new MemoryStream((byte[])ser.Deserialize(xrw)));
//bitMap.Save(filePath);
ms.Close();
xrw.Close();
对于其他文件序列化
FileStream stream = new FileInfo(value).OpenRead();
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xw.Formatting = Formatting.Indented;
XmlSerializer ser = new XmlSerializer(buffer.GetType());
ser.Serialize(xw, buffer);
string xmlString = sw.GetStringBuilder().ToString();
xw.Close();
sw.Close();
stream.Close();
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
if (doc != null && doc.ChildNodes.Count > 1 && doc.ChildNodes[1] != null)
{
XmlNode node = doc.ChildNodes[1];
fileString = node.InnerText;
}
反序列化
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version="1.0" encoding="utf-16"?>\r\n<base64Binary>");
sb.Append(fileString);
sb.Append("</base64Binary>");
XmlTextReader xrw = new XmlTextReader(new StringReader(sb.ToString()));
MemoryStream ms = new MemoryStream();
XmlSerializer ser = new XmlSerializer(ms.GetBuffer().GetType());
MemoryStream ms1 = new MemoryStream((byte[])ser.Deserialize(xrw));
FileStream fs = new FileStream("c:\\a." + fileEx, FileMode.CreateNew);
ms1.WriteTo(fs);
fs.Close();
ms1.Close();
ms.Close();
xrw.Close();
-------------------------------------
#region 保存 图像信息
XmlElement Images = doc.CreateElement("Images");
XmlAttribute imgcount = doc.CreateAttribute("Count");
imgcount.InnerText = imgFileName.Length.ToString();
Images.Attributes.Append(imgcount);
for (int i = 0; i < imgFileName.Length; i++)
{
XmlElement img = doc.CreateElement("image");
System.IO.FileInfo fi = new System.IO.FileInfo(imgFileName[i]);
int size = (int)fi.Length;
string type = fi.Name.Substring(fi.Name.LastIndexOf("."));
string strname = fi.Name;
// XmlElement elem = myXmlDoc.CreateElement(strname);
// 打开图片文件,利用该图片构造一个文件流
FileStream fs = new FileStream(imgFileName[i], FileMode.Open);
// 使用文件流构造一个二进制读取器将基元数据读作二进制值
BinaryReader br = new BinaryReader(fs);
byte[] imageBuffer = new byte[br.BaseStream.Length];
br.Read(imageBuffer, 0, Convert.ToInt32(br.BaseStream.Length));
string textString = System.Convert.ToBase64String(imageBuffer);
fs.Close();
br.Close();
img.InnerText = textString;
XmlAttribute ImgAttributesName = doc.CreateAttribute("Name");
ImgAttributesName.InnerText = strname.ToLower().ToString().Substring(0,strname.ToString().Length - 4);
XmlAttribute ImgAttributesSize = doc.CreateAttribute("Size");
ImgAttributesSize.InnerText = size.ToString();
XmlAttribute ImgAttributesType = doc.CreateAttribute("Type");
ImgAttributesType.InnerText = strname.ToLower().ToString().Substring(strname.ToString().Length - 3);
img.Attributes.Append(ImgAttributesSize);
img.Attributes.Append(ImgAttributesName);
img.Attributes.Append(ImgAttributesType);
Images.AppendChild(img);
}
EMRRepInfo.AppendChild(Images);
#endregion
--------------------------------------------------
以下代码来自:http://blog.csdn.net/marquess/archive/2008/07/29/2732629.aspx
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
namespace base64_img
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//图片 转为 base64编码的文本
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "选择要转换的图片";
dlg.Filter = "Image files (*.jpg;*.bmp;*.gif)|*.jpg*.jpeg;*.gif;*.bmp|AllFiles (*.*)|*.*";
if (DialogResult.OK == dlg.ShowDialog())
{
ImgToBase64String(dlg.FileName);
}
}
//图片 转为 base64编码的文本
private void ImgToBase64String(string Imagefilename)
{
try
{
Bitmap bmp = new Bitmap(Imagefilename);
this.pictureBox1.Image = bmp;
FileStream fs = new FileStream(Imagefilename + ".txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
String strbaser64 = Convert.ToBase64String(arr);
sw.Write(strbaser64);
sw.Close();
fs.Close();
MessageBox.Show("转换成功!");
}
catch (Exception ex)
{
MessageBox.Show("ImgToBase64String 转换失败\nException:" + ex.Message);
}
}
//base64编码的文本 转为 图片
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "选择要转换的base64编码的文本";
dlg.Filter = "txt files|*.txt";
if (DialogResult.OK == dlg.ShowDialog())
{
Base64StringToImage(dlg.FileName);
}
}
//base64编码的文本 转为 图片
private void Base64StringToImage(string txtFileName)
{
try
{
FileStream ifs = new FileStream(txtFileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(ifs);
String inputStr = sr.ReadToEnd();
byte[] arr = Convert.FromBase64String(inputStr);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms);
bmp.Save(txtFileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
//bmp.Save(txtFileName + ".bmp", ImageFormat.Bmp);
//bmp.Save(txtFileName + ".gif", ImageFormat.Gif);
//bmp.Save(txtFileName + ".png", ImageFormat.Png);
ms.Close();
sr.Close();
ifs.Close();
this.pictureBox1.Image = bmp;
MessageBox.Show("转换成功!");
}
catch (Exception ex)
{
MessageBox.Show("Base64StringToImage 转换失败\nException:"+ex.Message);
}
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/heyu1000/archive/2009/09/09/4534268.aspx