VB.NET实现图象文件和XML文件互换
创始人
2024-07-24 16:31:50
0

  通过.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 Nothing) Then 
  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.  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.Object, ByVal 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.Object, ByVal 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 + "")  
  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.Object, ByVal 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 String) As 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 Object, ByVal 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程序员必须知道的五大辅助工具

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...