VB.NET如何實現(xiàn)圖象文件和XML文件互換,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
10多年的沂水網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。成都營銷網(wǎng)站建設的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調(diào)整沂水建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“沂水網(wǎng)站設計”,“沂水網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
通過.NET框架下的FromBase64String和ToBase64String方法可以很容易地實現(xiàn)圖象文件和XML文件的互換。這樣可以輕易解決以XML格式保存圖片的問題。以下是利用VB.NET實現(xiàn)圖象文件和XML文件的互換代碼:
Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows 窗體設計器生成的代碼 " Public Sub New() MyBase.New() InitializeComponent() 在 InitializeComponent() 調(diào)用之后添加任何初始化 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("<pic><name>孟憲會</name><photo>" + pic + "</photo></pic>") MyXml.Save("c:\MyPhoto.xml") Label1.Text = "文件被保存到了:" + Microsoft.VisualBasic.ChrW(13) + "c:\MyPhoto.xml" End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button3.Click Dim openFileDialog1 As New OpenFileDialog() openFileDialog1.InitialDirectory = "c:\" openFileDialog1.Filter = "PNG(*.png)|*.png|Gif(*.gif)|*.gif|Jpg(*.jpg)|*.jpg|所有圖象文件(*.*)|*.*" openFileDialog1.FilterIndex = 2 openFileDialog1.RestoreDirectory = True If openFileDialog1.ShowDialog() = DialogResult.OK Then MyFile = openFileDialog1.FileName() MyFileExt = MyFile.Substring(MyFile.LastIndexOf(".") + 1) End If End Sub Public Function GetImageType(ByVal str As String) As System.Drawing.Imaging.ImageFormat Select Case str.ToLower() Case "jpg" Return System.Drawing.Imaging.ImageFormat.Jpeg Case "gif" Return System.Drawing.Imaging.ImageFormat.Gif Case "tiff" Return System.Drawing.Imaging.ImageFormat.Tiff() Case "icon" Return System.Drawing.Imaging.ImageFormat.Icon Case "image/png" Return System.Drawing.Imaging.ImageFormat.Png Case Else Return System.Drawing.Imaging.ImageFormat.MemoryBmp End Select End Function Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _ Handles MyBase.Closing System.Diagnostics.Process.Start("IExplore.exe", "http://xml.sz.luohuedu.net/") End Sub End Class
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
名稱欄目:VB.NET如何實現(xiàn)圖象文件和XML文件互換
新聞來源:http://www.rwnh.cn/article42/jgpjec.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供自適應網(wǎng)站、手機網(wǎng)站建設、品牌網(wǎng)站制作、面包屑導航、App設計、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)