首页 电脑学院 黑客教程 网站技术 网页特效 免费论文 公文写作 演讲发言 实用文档 职场指南 时尚生活 情感男女 其他资讯

您的位置:首页-> 电脑学院-> 电脑优化-> 把图象文件转换成XML格式文件
把图象文件转换成XML格式文件

利用.NET 框架下的FromBase64String和ToBase64String方法可以很容易地实现图象文件和XML文件的互换。这样可以轻易解决以XML格式保存图片的问题。代码如下:

Public Class Form1
  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 Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  Me.Button1 = New System.Windows.Forms.Button()
  Me.Button2 = New System.Windows.Forms.Button()
  Me.PictureBox1 = New System.Windows.Forms.PictureBox()
  Me.Button3 = New System.Windows.Forms.Button()
  Me.Label1 = New System.Windows.Forms.Label()
  Me.Label2 = New System.Windows.Forms.Label()
  Me.SuspendLayout()
  '
  'Button1
  '
  Me.Button1.Location = New System.Drawing.Point(365, 63)
  Me.Button1.Name = "Button1"
  Me.Button1.Size = New System.Drawing.Size(115, 23)
  Me.Button1.TabIndex = 0
  Me.Button1.Text = "将图象保存成XML"
  '
  'Button2
  '
  Me.Button2.Location = New System.Drawing.Point(365, 98)
  Me.Button2.Name = "Button2"
  Me.Button2.Size = New System.Drawing.Size(115, 23)
  Me.Button2.TabIndex = 1
  Me.Button2.Text = "从XML中得到图象"
  '
  'PictureBox1
  '
  Me.PictureBox1.Location = New System.Drawing.Point(18, 6)
  Me.PictureBox1.Name = "PictureBox1"
  Me.PictureBox1.Size = New System.Drawing.Size(320, 460)
  Me.PictureBox1.TabIndex = 2
  Me.PictureBox1.TabStop = False
  '
  'Button3
  '
  Me.Button3.Location = New System.Drawing.Point(365, 28)
  Me.Button3.Name = "Button3"
  Me.Button3.Size = New System.Drawing.Size(115, 23)
  Me.Button3.TabIndex = 3
  Me.Button3.Text = "浏览图片…"
  '
  'Label1
  '
  Me.Label1.Location = New System.Drawing.Point(369, 135)
  Me.Label1.Name = "Label1"
  Me.Label1.Size = New System.Drawing.Size(105, 95)
  Me.Label1.TabIndex = 4
  '
  'Label2
  '
  Me.Label2.Location = New System.Drawing.Point(367, 437)
  Me.Label2.Name = "Label2"
  Me.Label2.Size = New System.Drawing.Size(130, 16)
  Me.Label2.TabIndex = 5
  Me.Label2.Text = "【孟宪会之精彩世界】"
  '
  'Form1
  '
  Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
  Me.ClientSize = New System.Drawing.Size(500, 480)
  Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label2, Me.Label1, _
                  Me.Button3, Me.PictureBox1, Me.Button2, Me.Button1})
  Me.Name = "Form1"
  Me.Text = "图象文件和XML格式文件互换例子"
  Me.ResumeLayout(False)

End Sub

#End Region

Private MyFile As String = ""
Private MyFileExt As String = ""
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  Handles Button2.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.FromBase64String(pic))
  Me.PictureBox1.Image = New System.Drawing.Bitmap(memoryStream)
  memoryStream.Close()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  Handles Button1.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.ToBase64String(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")
  Label1.Text = "文件被保存到了:" + Microsoft.VisualBasic.ChrW(13) + "c:\MyPhoto.xml"
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  Handles Button3.Click
  Dim openFileDialog1 As New OpenFileDialog()
  openFileDialog1.InitialDirectory = "c:\"
  openFileDialog1.Filter = "PNG(*.png)|*.png|Gif(*.gif)|*.gif|Jpg(*.jpg)|*.jpg|所有图象文件(*.*)|*.*"
  openFileDialog1.FilterIndex = 2
  openFileDialog1.RestoreDirectory = True
  If openFileDialog1.ShowDialog() = DialogResult.OK Then
   MyFile = openFileDialog1.FileName()
   MyFileExt = MyFile.Substring(MyFile.LastIndexOf(".") + 1)
  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 Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
  Handles MyBase.Closing
  System.Diagnostics.Process.Start("IExplore.exe", "http://xml.sz.luohuedu.net/")
End Sub
End Class 
在百度中查找更多把图象文件转换成XML格式文件的内容
职场宝典   职场故事   职场跳槽   职场文化   职场理财   职场充电   情感天地   职场女性   职场礼仪   职场新人
报告总结   述职报告 工作总结 调查报告 工作汇报 计划方案 个人总结 社会实践 规章制度 调研报告 
  实习报告 考察报告 辞职报告 
演讲发言   竞职演说   就职演说   精彩演说   爱国演讲   英语演讲   十七大演讲   安全生产演讲稿   
节日祝福   重阳节 国庆节 教师节 中秋节 情人节 七夕节 劳动节 妇女节 清明节 愚人节 春节 元旦 圣诞节  儿童节  端午节 母亲节 新婚祝福 生日祝福 
讲话致辞   开业开幕   会议主持   庆典致辞   会议发言   党风廉政   党政报告   贺电慰问   婚丧嫁娶   思想宣传
法律常识   基本常识   法律文书   权益常识   劳动保障   婚姻继承   民事诉讼   刑事诉讼   
党建材料   入党申请   思想学习   党性分析   思想汇报   转正申请   民主生活   党委党建   入团申请   申报材料
求职简历   个人简历   求职自荐   求职谋略   面试技巧   求职英语   自我鉴定   英文简历   简历封面
心得体会   心得体会   经验交流   读后感   
时政热点   和谐社会   先进性教育   新农村建设   十七大   八荣八耻   科学发展观   劳动合同法   
人际沟通   社交技巧   社交礼仪   口才技巧   谈话技巧   演讲技巧   
营销技巧   电话销售   网络销售   推销技巧   促销技巧   销售口才   营销手段   销售技巧   谈判技巧   

“ 把图象文件转换成XML格式文件”来源于网络,版权归作者所有!勿用于商业用途。

电脑优化

操作系统
工具软件
办公应用
电脑DIY
电脑优化
故障解决
注册表
经验技巧
硬件相关
知识问答

本类阅读TOP10

·忘记登录密码的解决方案
·COMS密码无敌破解大法
·Foxmail和Outlook互连互通
·让Windows自动清除历史文档
·BIOS设置教程
·硬盘故障的分析与维修
·BIOS、CMOS经典故障
·禁止复制锁定网页右键代码
·重建任务栏“显示桌面”的图标
·U盘怎么样做启动盘

广告


关于本站|服务条款|广告服务|客服中心|发布文章|网站留言