熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

把圖象文件轉換成XML格式文件

2022-06-13   來源: .NET編程 
利用NET 框架下的FromBaseString和ToBaseString方法可以很容易地實現圖象文件和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
From:http://tw.wingwit.com/Article/program/net/201311/11970.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.