把图象文件转换成XML格式文件

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

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
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

  1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()   
  2Me.Button1 = New System.Windows.Forms.Button()   
  3Me.Button2 = New System.Windows.Forms.Button()   
  4Me.PictureBox1 = New System.Windows.Forms.PictureBox()   
  5Me.Button3 = New System.Windows.Forms.Button()   
  6Me.Label1 = New System.Windows.Forms.Label()   
  7Me.Label2 = New System.Windows.Forms.Label()   
  8Me.SuspendLayout()   
  9'   
 10'Button1   
 11'   
 12Me.Button1.Location = New System.Drawing.Point(365, 63)   
 13Me.Button1.Name = "Button1"   
 14Me.Button1.Size = New System.Drawing.Size(115, 23)   
 15Me.Button1.TabIndex = 0   
 16Me.Button1.Text = "将图象保存成XML"   
 17'   
 18'Button2   
 19'   
 20Me.Button2.Location = New System.Drawing.Point(365, 98)   
 21Me.Button2.Name = "Button2"   
 22Me.Button2.Size = New System.Drawing.Size(115, 23)   
 23Me.Button2.TabIndex = 1   
 24Me.Button2.Text = "从XML中得到图象"   
 25'   
 26'PictureBox1   
 27'   
 28Me.PictureBox1.Location = New System.Drawing.Point(18, 6)   
 29Me.PictureBox1.Name = "PictureBox1"   
 30Me.PictureBox1.Size = New System.Drawing.Size(320, 460)   
 31Me.PictureBox1.TabIndex = 2   
 32Me.PictureBox1.TabStop = False   
 33'   
 34'Button3   
 35'   
 36Me.Button3.Location = New System.Drawing.Point(365, 28)   
 37Me.Button3.Name = "Button3"   
 38Me.Button3.Size = New System.Drawing.Size(115, 23)   
 39Me.Button3.TabIndex = 3   
 40Me.Button3.Text = "浏览图片…"   
 41'   
 42'Label1   
 43'   
 44Me.Label1.Location = New System.Drawing.Point(369, 135)   
 45Me.Label1.Name = "Label1"   
 46Me.Label1.Size = New System.Drawing.Size(105, 95)   
 47Me.Label1.TabIndex = 4   
 48'   
 49'Label2   
 50'   
 51Me.Label2.Location = New System.Drawing.Point(367, 437)   
 52Me.Label2.Name = "Label2"   
 53Me.Label2.Size = New System.Drawing.Size(130, 16)   
 54Me.Label2.TabIndex = 5   
 55Me.Label2.Text = "【孟宪会之精彩世界】"   
 56'   
 57'Form1   
 58'   
 59Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)   
 60Me.ClientSize = New System.Drawing.Size(500, 480)   
 61Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label2, Me.Label1, _   
 62Me.Button3, Me.PictureBox1, Me.Button2, Me.Button1})   
 63Me.Name = "Form1"   
 64Me.Text = "图象文件和XML格式文件互换例子"   
 65Me.ResumeLayout(False)   
 66  
 67End Sub   
 68  
 69#End Region   
 70  
 71Private MyFile As String = ""   
 72Private MyFileExt As String = ""   
 73Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _   
 74Handles Button2.Click   
 75Dim pic As String   
 76Dim MyXml As System.Xml.XmlDocument = New System.Xml.XmlDocument()   
 77MyXml.Load("c:\MyPhoto.xml")   
 78Dim picNode As System.Xml.XmlNode   
 79picNode = MyXml.SelectSingleNode("/pic/photo")   
 80pic = picNode.InnerText   
 81Dim memoryStream As System.IO.MemoryStream   
 82memoryStream = New System.IO.MemoryStream(Convert.FromBase64String(pic))   
 83Me.PictureBox1.Image = New System.Drawing.Bitmap(memoryStream)   
 84memoryStream.Close()   
 85End Sub   
 86  
 87Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _   
 88Handles Button1.Click   
 89If MyFile = "" Then   
 90MessageBox.Show("请选择一个图片!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning)   
 91Exit Sub   
 92End If   
 93Dim MyImg As System.Drawing.Image = MyImg.FromFile(MyFile)   
 94Dim memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream()   
 95MyImg.Save(memoryStream, GetImageType(MyFileExt))   
 96Dim b() As Byte   
 97b = memoryStream.GetBuffer()   
 98Dim pic As String = Convert.ToBase64String(b)   
 99memoryStream.Close()   
100Dim MyXml As System.Xml.XmlDocument = New System.Xml.XmlDocument()   
101MyXml.LoadXml("<pic><name>孟宪会</name><photo>" + pic + "</photo></pic>")   
102MyXml.Save("c:\MyPhoto.xml")   
103Label1.Text = "文件被保存到了:" + Microsoft.VisualBasic.ChrW(13) + "c:\MyPhoto.xml"   
104End Sub   
105  
106Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _   
107Handles Button3.Click   
108Dim openFileDialog1 As New OpenFileDialog()   
109openFileDialog1.InitialDirectory = "c:\"   
110openFileDialog1.Filter = "PNG(*.png)|*.png|Gif(*.gif)|*.gif|Jpg(*.jpg)|*.jpg|所有图象文件(*.*)|*.*"   
111openFileDialog1.FilterIndex = 2   
112openFileDialog1.RestoreDirectory = True   
113If openFileDialog1.ShowDialog() = DialogResult.OK Then   
114MyFile = openFileDialog1.FileName()   
115MyFileExt = MyFile.Substring(MyFile.LastIndexOf(".") + 1)   
116End If   
117End Sub   
118  
119Public Function GetImageType(ByVal str As String) As System.Drawing.Imaging.ImageFormat   
120Select Case str.ToLower()   
121Case "jpg"   
122Return System.Drawing.Imaging.ImageFormat.Jpeg   
123Case "gif"   
124Return System.Drawing.Imaging.ImageFormat.Gif   
125Case "tiff"   
126Return System.Drawing.Imaging.ImageFormat.Tiff()   
127Case "icon"   
128Return System.Drawing.Imaging.ImageFormat.Icon   
129Case "image/png"   
130Return System.Drawing.Imaging.ImageFormat.Png   
131Case Else   
132Return System.Drawing.Imaging.ImageFormat.MemoryBmp   
133End Select   
134End Function   
135  
136Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _   
137Handles MyBase.Closing   
138System.Diagnostics.Process.Start("IExplore.exe", "http://xml.sz.luohuedu.net/")   
139End Sub   
140End Class</system.diagnostics.debuggerstepthrough()>
Published At
Categories with 站长专区
Tagged with
comments powered by Disqus