利用
NET 框架下的FromBase
String和ToBase
String方法可以很容易地实现图象文件和XML文件的互换
这样可以轻易解决以XML格式保存图片的问题
代码如下
Public Class Form
Inherits SystemWindowsFormsForm
#Region Windows 窗体设计器生成的代码
Public Sub New()
MyBaseNew()
InitializeComponent()
在 InitializeComponent() 调用之后添加任何初始化
End Sub
窗体重写处置以清理组件列表
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
componentsDispose()
End If
End If
MyBaseDispose(disposing)
End Sub
Windows 窗体设计器所必需的
Private components As SystemComponentModelIContainer
注意以下过程是 Windows 窗体设计器所必需的
可以使用 Windows 窗体设计器修改此过程
不要使用代码编辑器修改它
Friend WithEvents Button As SystemWindowsFormsButton
Friend WithEvents Button As SystemWindowsFormsButton
Friend WithEvents PictureBox As SystemWindowsFormsPictureBox
Friend WithEvents Button As SystemWindowsFormsButton
Friend WithEvents Label As SystemWindowsFormsLabel
Friend WithEvents Label As SystemWindowsFormsLabel
<SystemDiagnosticsDebuggerStepThrough()> Private Sub InitializeComponent()
MeButton = New SystemWindowsFormsButton()
MeButton = New SystemWindowsFormsButton()
MePictureBox = New SystemWindowsFormsPictureBox()
MeButton = New SystemWindowsFormsButton()
MeLabel = New SystemWindowsFormsLabel()
MeLabel = New SystemWindowsFormsLabel()
MeSuspendLayout()
Button
MeButtonLocation = New SystemDrawingPoint( )
MeButtonName = Button
MeButtonSize = New SystemDrawingSize( )
MeButtonTabIndex =
MeButtonText = 将图象保存成XML
Button
MeButtonLocation = New SystemDrawingPoint( )
MeButtonName = Button
MeButtonSize = New SystemDrawingSize( )
MeButtonTabIndex =
MeButtonText = 从XML中得到图象
PictureBox
MePictureBoxLocation = New SystemDrawingPoint( )
MePictureBoxName = PictureBox
MePictureBoxSize = New SystemDrawingSize( )
MePictureBoxTabIndex =
MePictureBoxTabStop = False
Button
MeButtonLocation = New SystemDrawingPoint( )
MeButtonName = Button
MeButtonSize = New SystemDrawingSize( )
MeButtonTabIndex =
MeButtonText = 浏览图片…
Label
MeLabelLocation = New SystemDrawingPoint( )
MeLabelName = Label
MeLabelSize = New SystemDrawingSize( )
MeLabelTabIndex =
Label
MeLabelLocation = New SystemDrawingPoint( )
MeLabelName = Label
MeLabelSize = New SystemDrawingSize( )
MeLabelTabIndex =
MeLabelText = 【孟宪会之精彩世界】
Form
MeAutoScaleBaseSize = New SystemDrawingSize( )
MeClientSize = New SystemDrawingSize( )
MeControlsAddRange(New SystemWindowsFormsControl() {MeLabel MeLabel MeButton MePictureBox MeButton MeButton})
MeName = Form
MeText = 图象文件和XML格式文件互换例子
MeResumeLayout(False)
End Sub
#End Region
Private MyFile As String =
Private MyFileExt As String =
Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) _
Handles ButtonClick
Dim pic As String
Dim MyXml As SystemXmlXmlDocument = New SystemXmlXmlDocument()
MyXmlLoad(c:\MyPhotoxml)
Dim picNode As SystemXmlXmlNode
picNode = MyXmlSelectSingleNode(/pic/photo)
pic = picNodeInnerText
Dim memoryStream As SystemIOMemoryStream
memoryStream = New SystemIOMemoryStream(ConvertFromBaseString(pic))
MePictureBoxImage = New SystemDrawingBitmap(memoryStream)
memoryStreamClose()
End Sub
Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) _
Handles ButtonClick
If MyFile = Then
MessageBoxShow(请选择一个图片! 错误 MessageBoxButtonsOK MessageBoxIconWarning)
Exit Sub
End If
Dim MyImg As SystemDrawingImage = MyImgFromFile(MyFile)
Dim memoryStream As SystemIOMemoryStream = New SystemIOMemoryStream()
MyImgSave(memoryStream GetImageType(MyFileExt))
Dim b() As Byte
b = memoryStreamGetBuffer()
Dim pic As String = ConvertToBaseString(b)
memoryStreamClose()
Dim MyXml As SystemXmlXmlDocument = New SystemXmlXmlDocument()
MyXmlLoadXml(<pic><name>孟宪会</name><photo> + pic + </photo></pic>)
MyXmlSave(c:\MyPhotoxml)
LabelText = 文件被保存到了 + MicrosoftVisualBasicChrW() + c:\MyPhotoxml
End Sub
Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) _
Handles ButtonClick
Dim openFileDialog As New OpenFileDialog()
openFileDialogInitialDirectory = c:\
openFileDialogFilter = PNG(*png)|*png|Gif(*gif)|*gif|Jpg(*jpg)|*jpg|所有图象文件(**)|**
openFileDialogFilterIndex =
openFileDialogRestoreDirectory = True
If openFileDialogShowDialog() = DialogResultOK Then
MyFile = openFileDialogFileName()
MyFileExt = MyFileSubstring(MyFileLastIndexOf() + )
End If
End Sub
Public Function GetImageType(ByVal str As String) As SystemDrawingImagingImageFormat
Select Case strToLower()
Case jpg
Return SystemDrawingImagingImageFormatJpeg
Case gif
Return SystemDrawingImagingImageFormatGif
Case tiff
Return SystemDrawingImagingImageFormatTiff()
Case icon
Return SystemDrawingImagingImageFormatIcon
Case image/png
Return SystemDrawingImagingImageFormatPng
Case Else
Return SystemDrawingImagingImageFormatMemoryBmp
End Select
End Function
Private Sub Form_Closing(ByVal sender As Object ByVal e As SystemComponentModelCancelEventArgs) _
Handles MyBaseClosing
SystemDiagnosticsProcessStart(IExploreexe )
End Sub
End Class