2011年7月26日星期二

VB.NET實現圖象文件和XML文件互換


http://www.inspirr.com

核心提示:通過.NET 框架下的FromBase64String和ToBase64String方法可以很容易地實現圖象文件和XML文件的互換。這樣可以輕易解決以XML格式保存圖片的問題。
通過.NET 框架下的FromBase64String和ToBase64String方法可以很容易地實現圖象文件和XML文件的互換。這樣可以輕易解決以XML格式保存圖片的問題。以下是利用VB.NET實現圖象文件和XML文件的互換代碼:

  1. Public Class Form1   Inherits System.Windows.Forms.Form  
  2. #Region " Windows 窗體設計器生成的代碼 "   Public Sub New  
  3. MyBase.New   InitializeComponent  
  4. 在 InitializeComponent 調用之后添加任何初始化   End Sub 
  5. 窗體重寫處置以清理組件列表。   Protected Overloads Overrides Sub Dispose  
  6. If disposing Then  If Not  Then 
  7. components.Dispose   End If 
  8. End If  MyBase.Dispose  
  9. End Sub  Windows 窗體設計器所必需的  
  10. Private components As System.ComponentModel.IContainer   注意:以下過程是 Windows 窗體設計器所必需的  
  11. 可以使用 Windows 窗體設計器修改此過程。   不要使用代碼編輯器修改它。  
  12. Friend WithEvents Button1 As System.Windows.Forms.Button   Friend WithEvents Button2 As System.Windows.Forms.Button  
  13. Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox   Friend WithEvents Button3 As System.Windows.Forms.Button  
  14. Friend WithEvents Label1 As System.Windows.Forms.Label   Friend WithEvents Label2 As System.Windows.Forms.Label  
  15. <System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent   Me.Button1 = New System.Windows.Forms.Button  
  16. Me.Button2 = New System.Windows.Forms.Button   Me.PictureBox1 = New System.Windows.Forms.PictureBox  
  17. Me.Button3 = New System.Windows.Forms.Button   Me.Label1 = New System.Windows.Forms.Label  
  18. Me.Label2 = New System.Windows.Forms.Label   Me.SuspendLayout  
  19.   Button1  
  20.   Me.Button1.Location = New System.Drawing.Point  
  21. Me.Button1.Name = "Button1"  Me.Button1.Size = New System.Drawing.Size  
  22. Me.Button1.TabIndex = 0   Me.Button1.Text = "將圖象保存成XML" 
  23.   Button2  
  24.   Me.Button2.Location = New System.Drawing.Point  
  25. Me.Button2.Name = "Button2"  Me.Button2.Size = New System.Drawing.Size  
  26. Me.Button2.TabIndex = 1   Me.Button2.Text = "從XML中得到圖象" 
  27.   PictureBox1  
  28.   Me.PictureBox1.Location = New System.Drawing.Point  
  29. Me.PictureBox1.Name = "PictureBox1"  Me.PictureBox1.Size = New System.Drawing.Size  
  30. Me.PictureBox1.TabIndex = 2   Me.PictureBox1.TabStop = False 
  31.   Button3  
  32.   Me.Button3.Location = New System.Drawing.Point  
  33. Me.Button3.Name = "Button3"  Me.Button3.Size = New System.Drawing.Size  
  34. Me.Button3.TabIndex = 3   Me.Button3.Text = "瀏覽圖片…" 
  35.   Label1  
  36.   Me.Label1.Location = New System.Drawing.Point  
  37. Me.Label1.Name = "Label1"  Me.Label1.Size = New System.Drawing.Size  
  38. Me.Label1.TabIndex = 4    
  39. Label2    
  40. Me.Label2.Location = New System.Drawing.Point   Me.Label2.Name = "Label2" 
  41. Me.Label2.Size = New System.Drawing.Size   Me.Label2.TabIndex = 5  
  42. Me.Label2.Text = "【孟憲會之精彩世界】"   
  43. Form1    
  44. Me.AutoScaleBaseSize = New System.Drawing.Size   Me.ClientSize = New System.Drawing.Size  
  45. Me.Controls.AddRange {Me.Label2, Me.Label1, Me.Button3, Me.PictureBox1, Me.Button2, Me.Button1})   Me.Name = "Form1" 
  46. Me.Text = "圖象文件和XML格式文件互換例子"  Me.ResumeLayout  
  47. End Sub  #End Region  
  48. Private MyFile As String = ""  Private MyFileExt As String = "" 
  49. Private Sub Button2_Click _   Handles Button2.Click  
  50. Dim pic As String  Dim MyXml As System.Xml.XmlDocument = New System.Xml.XmlDocument  
  51. MyXml.Load   Dim picNode As System.Xml.XmlNode  
  52. picNode = MyXml.SelectSingleNode   pic = picNode.InnerText  
  53. Dim memoryStream As System.IO.MemoryStream   memoryStream = New System.IO.MemoryStream)  
  54. Me.PictureBox1.Image = New System.Drawing.Bitmap   memoryStream.Close  
  55. End Sub  Private Sub Button1_Click _  
  56. Handles Button1.Click   If MyFile = "" Then 
  57. MessageBox.Show   Exit Sub 
  58. End If  Dim MyImg As System.Drawing.Image = MyImg.FromFile  
  59. Dim memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream   MyImg.Save)  
  60. Dim b As Byte  b = memoryStream.GetBuffer  
  61. Dim pic As String = Convert.ToBase64String   memoryStream.Close  
  62. Dim MyXml As System.Xml.XmlDocument = New System.Xml.XmlDocument   MyXml.LoadXml  
  63. MyXml.Save   Label1.Text = "文件被保存到了:" + Microsoft.VisualBasic.ChrW + "c:\MyPhoto.xml" 
  64. End Sub  Private Sub Button3_Click _  
  65. Handles Button3.Click   Dim openFileDialog1 As New OpenFileDialog  
  66. openFileDialog1.InitialDirectory = "c:\"  openFileDialog1.Filter = "PNG|*.png|Gif|*.gif|Jpg|*.jpg|所有圖象文件|*.*" 
  67. openFileDialog1.FilterIndex = 2   openFileDialog1.RestoreDirectory = True 
  68. If openFileDialog1.ShowDialog = DialogResult.OK Then  MyFile = openFileDialog1.FileName  
  69. MyFileExt = MyFile.Substring + 1)   End If 
  70. End Sub  Public Function GetImageType As System.Drawing.Imaging.ImageFormat  
  71. Select Case str.ToLower   Case "jpg" 
  72. Return System.Drawing.Imaging.ImageFormat.Jpeg   Case "gif" 
  73. Return System.Drawing.Imaging.ImageFormat.Gif   Case "tiff" 
  74. Return System.Drawing.Imaging.ImageFormat.Tiff   Case "icon" 
  75. Return System.Drawing.Imaging.ImageFormat.Icon   Case "image/png" 
  76. Return System.Drawing.Imaging.ImageFormat.Png   Case Else 
  77. Return System.Drawing.Imaging.ImageFormat.MemoryBmp   End Select 
  78. End Function  Private Sub Form1_Closing _  
  79. Handles MyBase.Closing   System.Diagnostics.Process.Start  
  80. End Sub  End Class 

  Tag: 設計公司 | 網頁設計公司 | 廣告公司 | 網站設計 | 平面設計 | 互動媒體 | 網頁設計 | Web design | Website design | design house | 媒體公司 | Iphone app | 程式設計 | Flash 網頁 | Flash game | 動畫設計 | 後期製作 | 網上商店 | 網上宣傳 | 網頁服務 |

没有评论:

发表评论