自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

VB.NET實(shí)現(xiàn)圖象文件和XML文件互換

開(kāi)發(fā) 后端
本文教大家利用VB.NET實(shí)現(xiàn)圖象文件和XML文件的互換。

  通過(guò).NET 框架下的FromBase64String和ToBase64String方法可以很容易地實(shí)現(xiàn)圖象文件和XML文件的互換。這樣可以輕易解決以XML格式保存圖片的問(wèn)題。以下是利用VB.NET實(shí)現(xiàn)圖象文件和XML文件的互換代碼:

  1. Public Class Form1  
  2. Inherits System.Windows.Forms.Form  
  3. #Region " Windows 窗體設(shè)計(jì)器生成的代碼 "  
  4. Public Sub New()  
  5. MyBase.New()  
  6. InitializeComponent()  
  7. 在 InitializeComponent() 調(diào)用之后添加任何初始化  
  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 窗體設(shè)計(jì)器所必需的  
  19. Private components As System.ComponentModel.IContainer  
  20. 注意:以下過(guò)程是 Windows 窗體設(shè)計(jì)器所必需的  
  21. 可以使用 Windows 窗體設(shè)計(jì)器修改此過(guò)程。  
  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 = "【孟憲會(huì)之精彩世界】" 
  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("請(qǐng)選擇一個(gè)圖片!""錯(cuò)誤", 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>孟憲會(huì)</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制作圖片按鈕實(shí)現(xiàn)步驟一一講解
  2. VB.NET存取數(shù)據(jù)庫(kù)圖片相關(guān)方法介紹
  3. VB.NET調(diào)用過(guò)程步驟詳解
  4. XML編程與應(yīng)用教程
  5. 詳談.NET Framework處理XML操作技巧
  6. .NET程序員必須知道的五大輔助工具
責(zé)任編輯:韓亞珊 來(lái)源: 軟件開(kāi)發(fā)網(wǎng)
相關(guān)推薦

2010-01-07 13:40:50

VB.NET讀取XML

2009-11-02 11:02:58

VB.NET XML文

2010-01-18 16:33:57

VB.NET加密文件

2010-01-20 13:42:10

VB.NET訪問(wèn)INIGetPrivateP

2009-10-29 15:16:02

VB.NET文件傳送

2010-01-13 14:32:11

VB.NET確定文件版

2009-11-03 13:16:58

VB.NET讀取文件

2009-11-02 12:35:10

VB.NET追加文件

2009-11-10 13:27:30

VB.NET RND(

2010-01-21 13:34:56

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文件上傳

2009-11-02 10:53:34

VB.NET INI文

2009-10-29 14:02:24

VB和VB.NET比較

2010-01-15 10:05:35

VB.NET文件對(duì)象

2010-01-21 16:17:32

VB.NET文件對(duì)象

2009-10-28 13:24:25

VB.NET文件

2009-11-02 09:21:04

VB.NET文件系統(tǒng)

2009-10-29 15:02:04

VB.NET文件排序
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)