分享
 
 
 

把图象文件转换成XML格式文件

王朝other·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

把图象文件转换成XML格式文件

作者: 孟宪会 出自: 【孟宪会之精彩世界】 发布日期: 2003-8-31 2:10:12

利用.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

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 + "

")

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

结果如下:

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有