利用
NET 框架下的FromBase
String和ToBase
String方法可以很容易地實現圖象文件和XML文件的互換
這樣可以輕易解決以XML格式保存圖片的問題
代碼如下
Public Class Form
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 Button
As System
Windows
Forms
Button
Friend WithEvents Button
As System
Windows
Forms
Button
Friend WithEvents PictureBox
As System
Windows
Forms
PictureBox
Friend WithEvents Button
As System
Windows
Forms
Button
Friend WithEvents Label
As System
Windows
Forms
Label
Friend WithEvents Label
As System
Windows
Forms
Label
<System
Diagnostics
DebuggerStepThrough()> Private Sub InitializeComponent()
Me
Button
= New System
Windows
Forms
Button()
Me
Button
= New System
Windows
Forms
Button()
Me
PictureBox
= New System
Windows
Forms
PictureBox()
Me
Button
= New System
Windows
Forms
Button()
Me
Label
= New System
Windows
Forms
Label()
Me
Label
= New System
Windows
Forms
Label()
Me
SuspendLayout()
Button
Me
Button
Location = New System
Drawing
Point(
)
Me
Button
Name =
Button
Me
Button
Size = New System
Drawing
Size(
)
Me
Button
TabIndex =
Me
Button
Text =
將圖象保存成XML
Button
Me
Button
Location = New System
Drawing
Point(
)
Me
Button
Name =
Button
Me
Button
Size = New System
Drawing
Size(
)
Me
Button
TabIndex =
Me
Button
Text =
從XML中得到圖象
PictureBox
Me
PictureBox
Location = New System
Drawing
Point(
)
Me
PictureBox
Name =
PictureBox
Me
PictureBox
Size = New System
Drawing
Size(
)
Me
PictureBox
TabIndex =
Me
PictureBox
TabStop = False
Button
Me
Button
Location = New System
Drawing
Point(
)
Me
Button
Name =
Button
Me
Button
Size = New System
Drawing
Size(
)
Me
Button
TabIndex =
Me
Button
Text =
浏覽圖片…
Label
Me
Label
Location = New System
Drawing
Point(
)
Me
Label
Name =
Label
Me
Label
Size = New System
Drawing
Size(
)
Me
Label
TabIndex =
Label
Me
Label
Location = New System
Drawing
Point(
)
Me
Label
Name =
Label
Me
Label
Size = New System
Drawing
Size(
)
Me
Label
TabIndex =
Me
Label
Text =
【孟憲會之精彩世界】
Form
Me
AutoScaleBaseSize = New System
Drawing
Size(
)
Me
ClientSize = New System
Drawing
Size(
)
Me
Controls
AddRange(New System
Windows
Forms
Control() {Me
Label
Me
Label
Me
Button
Me
PictureBox
Me
Button
Me
Button
})
Me
Name =
Form
Me
Text =
圖象文件和XML格式文件互換例子
Me
ResumeLayout(False)
End Sub
#End Region
Private MyFile As String =
Private MyFileExt As String =
Private Sub Button
_Click(ByVal sender As System
Object
ByVal e As System
EventArgs) _
Handles Button
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
FromBase
String(pic))
Me
PictureBox
Image = New System
Drawing
Bitmap(memoryStream)
memoryStream
Close()
End Sub
Private Sub Button
_Click(ByVal sender As System
Object
ByVal e As System
EventArgs) _
Handles Button
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
ToBase
String(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
)
Label
Text =
文件被保存到了
+ Microsoft
VisualBasic
ChrW(
) +
c:\MyPhoto
xml
End Sub
Private Sub Button
_Click(ByVal sender As System
Object
ByVal e As System
EventArgs) _
Handles Button
Click
Dim openFileDialog
As New OpenFileDialog()
openFileDialog
InitialDirectory =
c:\
openFileDialog
Filter =
PNG(*
png)|*
png|Gif(*
gif)|*
gif|Jpg(*
jpg)|*
jpg|所有圖象文件(*
*)|*
*
openFileDialog
FilterIndex =
openFileDialog
RestoreDirectory = True
If openFileDialog
ShowDialog() = DialogResult
OK Then
MyFile = openFileDialog
FileName()
MyFileExt = MyFile
Substring(MyFile
LastIndexOf(
) +
)
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 Form
_Closing(ByVal sender As Object
ByVal e As System
ComponentModel
CancelEventArgs) _
Handles MyBase
Closing
System
Diagnostics
Process
Start(
IExplore
exe
)
End Sub
End Class
From:http://tw.wingwit.com/Article/program/net/201311/11970.html