Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Exchange between Image File and XML File by VB.NET

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

VB.NET how to achieve image files and XML file exchange, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

The interchange between image file and XML file can be easily realized by FromBase64String and ToBase64String methods in .NET framework. This can easily solve the problem of saving pictures in XML format. The following is the exchange code between image file and XML file using VB.NET:

Public Class Form1 Inherits System.Windows.Forms.Form # Region Code generated by the Windows forms designer Public Sub New () MyBase.New () InitializeComponent () adds any initialization End Sub forms rewrite disposal to clean up the component list after the InitializeComponent () call. 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 forms designer required Private components As System.ComponentModel.IContainer Note: the following procedure is necessary for the Windows forms designer, which can be modified using the Windows forms designer. Do not use the code editor to modify it. 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 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 63) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size (115,23) Me.Button1.TabIndex = 0 Me.Button1.Text = "Save the image as XML" Button2 Me.Button2.Location = New System.Drawing.Point (365,98) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size 23) Me.Button2.TabIndex = 1 Me.Button2.Text = "get an image from 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 23) Me.Button3.TabIndex = 3 Me.Button3.Text = "browse pictures …" 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 16) Me.Label2.TabIndex = 5 Me.Label2.Text = "[Meng Xianhui's wonderful World]" 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 = "Image file and XML format file exchange example" 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 ("Please select a picture!" , "error", 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 ("Meng Xianhui" + pic + ") MyXml.Save (" c:\ MyPhoto.xml ") Label1.Text =" the file is saved to: + 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 | all image files (*. *) | *." 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", "is it helpful for http://xml.sz.luohuedu.net/") End Sub End Class to read the above content?" If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report