利用.NET 框架下的FromBase64String和ToBase64String方法可以很容易地实现图象文件和XML文件的互换。这样可以轻易解决以XML格式保存图片的问题。代码如下:
1
Public Class Form1Class Form1
2
Inherits System.Windows.Forms.Form
3
4
Windows 窗体设计器生成的代码#Region " Windows 窗体设计器生成的代码 "
5
Public Sub New()Sub New()
6
MyBase.New()
7
InitializeComponent()
8
'在 InitializeComponent() 调用之后添加任何初始化
9
End Sub
10
11
'窗体重写处置以清理组件列表。
12
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
13
If disposing Then
14
If Not (components Is Nothing) Then
15
components.Dispose()
16
End If
17
End If
18
MyBase.Dispose(disposing)
19
End Sub
20
21
'Windows 窗体设计器所必需的
22
Private components As System.ComponentModel.IContainer
23
'注意:以下过程是 Windows 窗体设计器所必需的
24
'可以使用 Windows 窗体设计器修改此过程。
25
'不要使用代码编辑器修改它。
26
Friend WithEvents Button1 As System.Windows.Forms.Button
27
Friend WithEvents Button2 As System.Windows.Forms.Button
28
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
29
Friend WithEvents Button3 As System.Windows.Forms.Button
30
Friend WithEvents Label1 As System.Windows.Forms.Label
31
Friend WithEvents Label2 As System.Windows.Forms.Label
32
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
33
Me.Button1 = New System.Windows.Forms.Button()
34
Me.Button2 = New System.Windows.Forms.Button()
35
Me.PictureBox1 = New System.Windows.Forms.PictureBox()
36
Me.Button3 = New System.Windows.Forms.Button()
37
Me.Label1 = New System.Windows.Forms.Label()
38
Me.Label2 = New System.Windows.Forms.Label()
39
Me.SuspendLayout()
40
'
41
'Button1
42
'
43
Me.Button1.Location = New System.Drawing.Point(365, 63)
44
Me.Button1.Name = "Button1"
45
Me.Button1.Size = New System.Drawing.Size(115, 23)
46
Me.Button1.TabIndex = 0
47
Me.Button1.Text = "将图象保存成XML"
48
'
49
'Button2
50
'
51
Me.Button2.Location = New System.Drawing.Point(365, 98)
52
Me.Button2.Name = "Button2"
53
Me.Button2.Size = New System.Drawing.Size(115, 23)
54
Me.Button2.TabIndex = 1
55
Me.Button2.Text = "从XML中得到图象"
56
'
57
'PictureBox1
58
'
59
Me.PictureBox1.Location = New System.Drawing.Point(18, 6)
60
Me.PictureBox1.Name = "PictureBox1"
61
Me.PictureBox1.Size = New System.Drawing.Size(320, 460)
62
Me.PictureBox1.TabIndex = 2
63
Me.PictureBox1.TabStop = False
64
'
65
'Button3
66
'
67
Me.Button3.Location = New System.Drawing.Point(365, 28)
68
Me.Button3.Name = "Button3"
69
Me.Button3.Size = New System.Drawing.Size(115, 23)
70
Me.Button3.TabIndex = 3
71
Me.Button3.Text = "浏览图片…"
72
'
73
'Label1
74
'
75
Me.Label1.Location = New System.Drawing.Point(369, 135)
76
Me.Label1.Name = "Label1"
77
Me.Label1.Size = New System.Drawing.Size(105, 95)
78
Me.Label1.TabIndex = 4
79
'
80
'Label2
81
'
82
Me.Label2.Location = New System.Drawing.Point(367, 437)
83
Me.Label2.Name = "Label2"
84
Me.Label2.Size = New System.Drawing.Size(130, 16)
85
Me.Label2.TabIndex = 5
86
Me.Label2.Text = "【踢西工作室】"
87
'
88
'Form1
89
'
90
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
91
Me.ClientSize = New System.Drawing.Size(500, 480)
92
Me.Controls.AddRange(New System.Windows.Forms.Control() _
93
{Me.Label2, Me.Label1, Me.Button3, Me.PictureBox1, Me.Button2, Me.Button1})
94
Me.Name = "Form1"
95
Me.Text = "图象文件和XML格式文件互换例子"
96
Me.ResumeLayout(False)
97
98
End Sub
99
100
#End Region
101
102
Private MyFile As String = ""
103
Private MyFileExt As String = ""
104
Private Sub Button2_Click()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()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()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()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()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
171
End Class
172
173