您的位置:建站学院首页 >> 文章 >> asp.net >> VB.Net语言
把图象文件转换成XML格式文件

利用.NET 框架下的FromBase64String和ToBase64String方法可以很容易地实现图象文件和XML文件的互换。这样可以轻易解决以XML格式保存图片的问题。代码如下:

Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows 窗体设计器生成的代码 " Public Sub New() MyBase.New() InitializeComponent() '在 InitializeComponent() 调用之后添加任何初始化 End Sub '窗体重写处置以清理组件列表。 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Windows 窗体设计器所必需的 Private components As System.ComponentModel.IContainer '注意:以下过程是 Windows 窗体设计器所必需的 '可以使用 Windows 窗体设计器修改此过程。 '不要使用代码编辑器修改它。 Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button() Me.Button2 = New System.Windows.Forms.Button() Me.PictureBox1 = New System.Windows.Forms.PictureBox() Me.Button3 = New System.Windows.Forms.Button() Me.Label1 = New System.Windows.Forms.Label() Me.Label2 = New System.Windows.Forms.Label() Me.SuspendLayout() ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(365, 63) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(115, 23) Me.Button1.TabIndex = 0 Me.Button1.Text = "将图象保存成XML" ' 'Button2 ' Me.Button2.Location = New System.Drawing.Point(365, 98) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(115, 23) Me.Button2.TabIndex = 1 Me.Button2.Text = "从XML中得到图象" ' 'PictureBox1 ' Me.PictureBox1.Location = New System.Drawing.Point(18, 6) Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size(320, 460) Me.PictureBox1.TabIndex = 2 Me.PictureBox1.TabStop = False ' 'Button3 ' Me.Button3.Location = New System.Drawing.Point(365, 28) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(115, 23) Me.Button3.TabIndex = 3 Me.Button3.Text = "浏览图片…" ' 'Label1 ' Me.Label1.Location = New System.Drawing.Point(369, 135) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(105, 95) Me.Label1.TabIndex = 4 ' 'Label2 ' Me.Label2.Location = New System.Drawing.Point(367, 437) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(130, 16) Me.Label2.TabIndex = 5 Me.Label2.Text = "【孟宪会之精彩世界】" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(500, 480) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label2, Me.Label1, Me.Button3, Me.PictureBox1, Me.Button2, Me.Button1}) Me.Name = "Form1" Me.Text = "图象文件和XML格式文件互换例子" Me.ResumeLayout(False) End Sub #End Region Private MyFile As String = "" Private MyFileExt As String = "" Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button2.Click Dim pic As String Dim MyXml As System.Xml.XmlDocument = New System.Xml.XmlDocument() MyXml.Load("c:\MyPhoto.xml") Dim picNode As System.Xml.XmlNode picNode = MyXml.SelectSingleNode("/pic/photo") pic = picNode.InnerText Dim memoryStream As System.IO.MemoryStream memoryStream = New System.IO.MemoryStream(Convert.FromBase64String(pic)) Me.PictureBox1.Image = New System.Drawing.Bitmap(memoryStream) memoryStream.Close() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button1.Click If MyFile = "" Then MessageBox.Show("请选择一个图片!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning) Exit Sub End If Dim MyImg As System.Drawing.Image = MyImg.FromFile(MyFile) Dim memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream() MyImg.Save(memoryStream, GetImageType(MyFileExt)) Dim b() As Byte b = memoryStream.GetBuffer() Dim pic As String = Convert.ToBase64String(b) memoryStream.Close() Dim MyXml As System.Xml.XmlDocument = New System.Xml.XmlDocument() MyXml.LoadXml("<p><div align="center">本内容共<span style="color:red">2</span>页&nbsp;&nbsp;首 页&nbsp;&nbsp;上一页&nbsp;&nbsp;<a href="101957837_2.html">下一页</a>&nbsp;&nbsp;<a href="101957837_2.html">尾 页</a>&nbsp;&nbsp;当前在第<span style="color:red">1</span>页</div></p></div> </div> <div class="google"> <!-- SiteSearch Google --> <form method="get" action="http://www.google.cn/custom" target="google_window"> <table border="0" bgcolor="#ffffff"> <tr><td nowrap="nowrap" valign="top" align="left" height="32"> <a href="http://www.google.com/"> <img src="http://www.google.com/logos/Logo_25wht.gif" border="0" alt="Google" align="middle"></img></a> </td> <td nowrap="nowrap"> <input type="hidden" name="domains" value="www.cqxw.net"></input> <input type="text" name="q" size="64" maxlength="255" value=""></input> <input type="submit" name="sa" value="搜索"></input> </td></tr> <tr> <td>&nbsp;</td> <td nowrap="nowrap"> <table> <tr> <td> <input type="radio" name="sitesearch" value=""></input> <font size="-1" color="#000000">Web</font> </td> <td> <input type="radio" name="sitesearch" value="www.cqxw.net" checked="checked"></input> <font size="-1" color="#000000">www.cqxw.net</font> </td> </tr> </table> <input type="hidden" name="client" value="pub-7049110951060221"></input> <input type="hidden" name="forid" value="1"></input> <input type="hidden" name="ie" value="GB2312"></input> <input type="hidden" name="oe" value="GB2312"></input> <input type="hidden" name="cof" value="GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:FFFFFF;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;FORID:1"></input> <input type="hidden" name="hl" value="zh-CN"></input> </td></tr></table> </form> <!-- SiteSearch Google --> </div> <b class="rbottom"> <b class="r4"></b><b class="r3"></b><b class="r2"></b><b class="r1"></b> </b> </div> <div> </div> </div> </div> <div class="footer"><!--底部--> <div class="RoundedCorner"> <b class="rtop"><b class="r1"></b><b class="r2"></b><b class="r3"></b><b class="r4"></b></b> <div class="bottom">&nbsp;<a href="http://www.csbest.cn" target="_blank" title="长寿百事通">长寿百事通</a>&nbsp;&nbsp;<a href="http://www.17new.net" target="_blank" title="新势力论坛">新势力论坛</a>&nbsp;&nbsp;<a href="http://www.9bin.com" target="_blank" title="九滨中文网">九滨中文网</a>&nbsp;&nbsp;<a href="http://www.opxv.com" target="_blank" title="沸点数码网">沸点数码网</a>&nbsp;&nbsp;<a href="http://www.upjk.com" target="_blank" title="up健康网">up健康网</a>&nbsp;&nbsp;<a href="http://www.weikelai.cn" target="_blank" title="威客来">威客来</a>&nbsp;&nbsp;<a href="http://bbs.cnnol.com/" target="_blank" title="中网在线论坛">中网在线论坛</a>&nbsp;&nbsp;<a href="http://nshk.023com.com" target="_blank" title="那时花开">那时花开</a>&nbsp;&nbsp;<a href="http://www.80boby.com" target="_blank" title="八零后生">八零后生</a>&nbsp;</div> <hr class="hr"/> <div class="bottom">关于我们 | 版权声明 | 共同成长 | 友情链接 | <a href="javascript:window.external.AddFavorite('','迅微网络:建站学院')">加入收藏</a> | <a href="http://www.miibeian.gov.cn">渝ICP备05007588号</a> <br /> &copy; Copyright 2004-2008 CQXW.NET All rights reserved.<script src="http://s47.cnzz.com/stat.php?id=223412&web_id=223412" language="JavaScript" charset="gb2312"></script> </div> <b class="rbottom"> <b class="r4"></b><b class="r3"></b><b class="r2"></b><b class="r1"></b> </b> </div> </div> </body> </html>