VB.NET实现图象文件和XML文件互换

开发 后端
本文教大家利用VB.NET实现图象文件和XML文件的互换。

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

  1. Public Class Form1  
  2. Inherits System.Windows.Forms.Form  
  3. #Region " Windows 窗体设计器生成的代码 "  
  4. Public Sub New()  
  5. MyBase.New()  
  6. InitializeComponent()  
  7. 在 InitializeComponent() 调用之后添加任何初始化  
  8. End Sub 
  9. 窗体重写处置以清理组件列表。  
  10. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)  
  11. If disposing Then 
  12. If Not (components Is NothingThen 
  13. components.Dispose()  
  14. End If 
  15. End If 
  16. MyBase.Dispose(disposing)  
  17. End Sub 
  18. Windows 窗体设计器所必需的  
  19. Private components As System.ComponentModel.IContainer  
  20. 注意:以下过程是 Windows 窗体设计器所必需的  
  21. 可以使用 Windows 窗体设计器修改此过程。  
  22. 不要使用代码编辑器修改它。  
  23. Friend WithEvents Button1 As System.Windows.Forms.Button  
  24. Friend WithEvents Button2 As System.Windows.Forms.Button  
  25. Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox  
  26. Friend WithEvents Button3 As System.Windows.Forms.Button  
  27. Friend WithEvents Label1 As System.Windows.Forms.Label  
  28. Friend WithEvents Label2 As System.Windows.Forms.Label  
  29. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()  
  30. Me.Button1 = New System.Windows.Forms.Button()  
  31. Me.Button2 = New System.Windows.Forms.Button()  
  32. Me.PictureBox1 = New System.Windows.Forms.PictureBox()  
  33. Me.Button3 = New System.Windows.Forms.Button()  
  34. Me.Label1 = New System.Windows.Forms.Label()  
  35. Me.Label2 = New System.Windows.Forms.Label()  
  36. Me.SuspendLayout()  
  37.  
  38. Button1  
  39.  
  40. Me.Button1.Location = New System.Drawing.Point(365, 63)  
  41. Me.Button1.Name = "Button1" 
  42. Me.Button1.Size = New System.Drawing.Size(115, 23)  
  43. Me.Button1.TabIndex = 0  
  44. Me.Button1.Text = "将图象保存成XML" 
  45.  
  46. Button2  
  47.  
  48. Me.Button2.Location = New System.Drawing.Point(365, 98)  
  49. Me.Button2.Name = "Button2" 
  50. Me.Button2.Size = New System.Drawing.Size(115, 23)  
  51. Me.Button2.TabIndex = 1  
  52. Me.Button2.Text = "从XML中得到图象" 
  53.  
  54. PictureBox1  
  55.  
  56. Me.PictureBox1.Location = New System.Drawing.Point(18, 6)  
  57. Me.PictureBox1.Name = "PictureBox1" 
  58. Me.PictureBox1.Size = New System.Drawing.Size(320, 460)  
  59. Me.PictureBox1.TabIndex = 2  
  60. Me.PictureBox1.TabStop = False 
  61.  
  62. Button3  
  63.  
  64. Me.Button3.Location = New System.Drawing.Point(365, 28)  
  65. Me.Button3.Name = "Button3" 
  66. Me.Button3.Size = New System.Drawing.Size(115, 23)  
  67. Me.Button3.TabIndex = 3  
  68. Me.Button3.Text = "浏览图片…" 
  69.  
  70. Label1  
  71.  
  72. Me.Label1.Location = New System.Drawing.Point(369, 135)  
  73. Me.Label1.Name = "Label1" 
  74. Me.Label1.Size = New System.Drawing.Size(105, 95)  
  75. Me.Label1.TabIndex = 4  
  76.  
  77. Label2  
  78.  
  79. Me.Label2.Location = New System.Drawing.Point(367, 437)  
  80. Me.Label2.Name = "Label2" 
  81. Me.Label2.Size = New System.Drawing.Size(130, 16)  
  82. Me.Label2.TabIndex = 5  
  83. Me.Label2.Text = "【孟宪会之精彩世界】" 
  84.  
  85. Form1  
  86.  
  87. Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)  
  88. Me.ClientSize = New System.Drawing.Size(500, 480)  
  89. Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label2, Me.Label1, Me.Button3, Me.PictureBox1, Me.Button2, Me.Button1})  
  90. Me.Name = "Form1" 
  91. Me.Text = "图象文件和XML格式文件互换例子" 
  92. Me.ResumeLayout(False)  
  93. End Sub 
  94. #End Region  
  95. Private MyFile As String = "" 
  96. Private MyFileExt As String = "" 
  97. Private Sub Button2_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) _  
  98. Handles Button2.Click  
  99. Dim pic As String 
  100. Dim MyXml As System.Xml.XmlDocument = New System.Xml.XmlDocument()  
  101. MyXml.Load("c:\MyPhoto.xml")  
  102. Dim picNode As System.Xml.XmlNode  
  103. picNode = MyXml.SelectSingleNode("/pic/photo")  
  104. pic = picNode.InnerText  
  105. Dim memoryStream As System.IO.MemoryStream  
  106. memoryStream = New System.IO.MemoryStream(Convert.FromBase64String(pic))  
  107. Me.PictureBox1.Image = New System.Drawing.Bitmap(memoryStream)  
  108. memoryStream.Close()  
  109. End Sub 
  110. Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) _  
  111. Handles Button1.Click  
  112. If MyFile = "" Then 
  113. MessageBox.Show("请选择一个图片!""错误", MessageBoxButtons.OK, MessageBoxIcon.Warning)  
  114. Exit Sub 
  115. End If 
  116. Dim MyImg As System.Drawing.Image = MyImg.FromFile(MyFile)  
  117. Dim memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream()  
  118. MyImg.Save(memoryStream, GetImageType(MyFileExt))  
  119. Dim b() As Byte 
  120. b = memoryStream.GetBuffer()  
  121. Dim pic As String = Convert.ToBase64String(b)  
  122. memoryStream.Close()  
  123. Dim MyXml As System.Xml.XmlDocument = New System.Xml.XmlDocument()  
  124. MyXml.LoadXml("<pic><name>孟宪会</name><photo>" + pic + "</photo></pic>")  
  125. MyXml.Save("c:\MyPhoto.xml")  
  126. Label1.Text = "文件被保存到了:" + Microsoft.VisualBasic.ChrW(13) + "c:\MyPhoto.xml" 
  127. End Sub 
  128. Private Sub Button3_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) _  
  129. Handles Button3.Click  
  130. Dim openFileDialog1 As New OpenFileDialog()  
  131. openFileDialog1.InitialDirectory = "c:\" 
  132. openFileDialog1.Filter = "PNG(*.png)|*.png|Gif(*.gif)|*.gif|Jpg(*.jpg)|*.jpg|所有图象文件(*.*)|*.*" 
  133. openFileDialog1.FilterIndex = 2  
  134. openFileDialog1.RestoreDirectory = True 
  135. If openFileDialog1.ShowDialog() = DialogResult.OK Then 
  136. MyFile = openFileDialog1.FileName()  
  137. MyFileExt = MyFile.Substring(MyFile.LastIndexOf(".") + 1)  
  138. End If 
  139. End Sub 
  140. Public Function GetImageType(ByVal str As StringAs System.Drawing.Imaging.ImageFormat  
  141. Select Case str.ToLower()  
  142. Case "jpg" 
  143. Return System.Drawing.Imaging.ImageFormat.Jpeg  
  144. Case "gif" 
  145. Return System.Drawing.Imaging.ImageFormat.Gif  
  146. Case "tiff" 
  147. Return System.Drawing.Imaging.ImageFormat.Tiff()  
  148. Case "icon" 
  149. Return System.Drawing.Imaging.ImageFormat.Icon  
  150. Case "image/png" 
  151. Return System.Drawing.Imaging.ImageFormat.Png  
  152. Case Else 
  153. Return System.Drawing.Imaging.ImageFormat.MemoryBmp  
  154. End Select 
  155. End Function 
  156. Private Sub Form1_Closing(ByVal sender As ObjectByVal e As System.ComponentModel.CancelEventArgs) _  
  157. Handles MyBase.Closing  
  158. System.Diagnostics.Process.Start("IExplore.exe""http://xml.sz.luohuedu.net/")  
  159. End Sub 
  160. End Class 

【编辑推荐】

  1. VB.NET制作图片按钮实现步骤一一讲解
  2. VB.NET存取数据库图片相关方法介绍
  3. VB.NET调用过程步骤详解
  4. XML编程与应用教程
  5. 详谈.NET Framework处理XML操作技巧
  6. .NET程序员必须知道的五大辅助工具
责任编辑:韩亚珊 来源: 软件开发网
相关推荐

2010-01-07 13:40:50

VB.NET读取XML

2009-11-02 11:02:58

VB.NET XML文

2010-01-20 13:42:10

VB.NET访问INIGetPrivateP

2010-01-18 16:33:57

VB.NET加密文件

2009-11-02 12:35:10

VB.NET追加文件

2010-01-21 13:34:56

VB.NET删除文件夹

2009-11-10 13:27:30

VB.NET RND(

2009-10-29 15:16:02

VB.NET文件传送

2010-01-13 14:32:11

VB.NET确定文件版

2009-11-03 13:16:58

VB.NET读取文件

2010-01-15 10:05:35

VB.NET文件对象

2010-01-21 16:17:32

VB.NET文件对象

2009-10-29 15:02:04

VB.NET文件排序

2009-11-02 09:21:04

VB.NET文件系统

2010-01-15 19:04:09

2009-10-28 13:24:25

VB.NET文件

2009-10-29 15:28:38

VB.NET文件操作

2010-01-14 11:00:48

VB.NET文件合并

2010-01-12 09:51:07

VB.NET操作dbf

2010-01-12 17:02:54

VB.NET文件上传
点赞
收藏

51CTO技术栈公众号