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

时间:2006/5/24 18:39:03      阅读:3107          
利用.NET 框架下的FromBase64String和ToBase64String方法可以很容易地实现图象文件和XML文件的互换。这样可以轻易解决以XML格式保存图片的问题。代码如下:
  1Public Class Form1
  2    Inherits System.Windows.Forms.Form
  3
  4Windows 窗体设计器生成的代码
101
102  Private MyFile As String = ""
103  Private MyFileExt As String = ""
104  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
105    Handles Button2.Click
106    Dim pic As String
107    Dim MyXml As System.Xml.XmlDocument = New System.Xml.XmlDocument()
108    MyXml.Load("c:\MyPhoto.xml")
109    Dim picNode As System.Xml.XmlNode
110    picNode = MyXml.SelectSingleNode("/pic/photo")
111    pic = picNode.InnerText
112    Dim memoryStream As System.IO.MemoryStream
113    memoryStream = New System.IO.MemoryStream(Convert.FromBase64String(pic))
114    Me.PictureBox1.Image = New System.Drawing.Bitmap(memoryStream)
115    memoryStream.Close()
116  End Sub
117
118  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
119    Handles Button1.Click
120    If MyFile = "" Then
121      MessageBox.Show("请选择一个图片!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning)
122      Exit Sub
123    End If
124    Dim MyImg As System.Drawing.Image = MyImg.FromFile(MyFile)
125    Dim memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream()
126    MyImg.Save(memoryStream, GetImageType(MyFileExt))
127    Dim b() As Byte
128    b = memoryStream.GetBuffer()
129    Dim pic As String = Convert.ToBase64String(b)
130    memoryStream.Close()
131    Dim MyXml As System.Xml.XmlDocument = New System.Xml.XmlDocument()
132    MyXml.LoadXml("踢西工作室" + pic + "")
133    MyXml.Save("c:\MyPhoto.xml")
134    Label1.Text = "文件被保存到了:" + Microsoft.VisualBasic.ChrW(13) + "c:\MyPhoto.xml"
135  End Sub
136
137  Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
138    Handles Button3.Click
139    Dim openFileDialog1 As New OpenFileDialog()
140    openFileDialog1.InitialDirectory = "c:\"
141    openFileDialog1.Filter = "PNG(*.png)|*.png|Gif(*.gif)|*.gif|Jpg(*.jpg)|*.jpg|所有图象文件(*.*)|*.*"
142    openFileDialog1.FilterIndex = 2
143    openFileDialog1.RestoreDirectory = True
144    If openFileDialog1.ShowDialog() = DialogResult.OK Then
145      MyFile = openFileDialog1.FileName()
146      MyFileExt = MyFile.Substring(MyFile.LastIndexOf(".") + 1)
147    End If
148  End Sub
149
150  Public Function GetImageType(ByVal str As String) As System.Drawing.Imaging.ImageFormat
151    Select Case str.ToLower()
152      Case "jpg"
153        Return System.Drawing.Imaging.ImageFormat.Jpeg
154      Case "gif"
155        Return System.Drawing.Imaging.ImageFormat.Gif
156      Case "tiff"
157        Return System.Drawing.Imaging.ImageFormat.Tiff()
158      Case "icon"
159        Return System.Drawing.Imaging.ImageFormat.Icon
160      Case "image/png"
161        Return System.Drawing.Imaging.ImageFormat.Png
162      Case Else
163        Return System.Drawing.Imaging.ImageFormat.MemoryBmp
164    End Select
165  End Function
166
167  Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
168    Handles MyBase.Closing
169    System.Diagnostics.Process.Start("IExplore.exe", "http://www.web3.cn/")
170  End Sub
171End Class
172
173
评论
  • Re:把图象文件转换成XML格式文件  (2006/8/16 15:33:36) by 飘萍 
    不错,学习一下。
标 题:
 
姓 名:
 
主 页:

验证码:

评论:
 

Because of the cache,you may see your comments several minutes later.