Warning: session_start(): open(/tmp/sess_9b12t1kdki62fgphojtitru300, O_RDWR) failed: Disk quota exceeded (122) in /home/tensungn/public_html/www.tzlink.com/info/show.php on line 8

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/tensungn/public_html/www.tzlink.com/info/show.php:8) in /home/tensungn/public_html/www.tzlink.com/info/show.php on line 8

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/tensungn/public_html/www.tzlink.com/info/show.php:8) in /home/tensungn/public_html/www.tzlink.com/info/show.php on line 8
VB.NET实现图象文件和XML文件互换 - ASP/ASP.NET编程 - 站长之家

热烈祝贺台州维博网络的站长论坛隆重上线!(2012-05-28)    热烈庆祝伟大的祖国60周年生日 点击进来我们一起为她祝福吧(2009-09-26)    站长论坛禁止发布广告,一经发现立即删除。谢谢各位合作!.(2009-08-08)    热烈祝贺台州网址导航全面升级,全新版本上线!希望各位一如既往地支持台州网址导航的发展.(2009-03-28)    台州站长论坛恭祝各位新年快乐,牛年行大运!(2009-01-24)    台州Link正式更名为台州网址导航,专业做以台州网址为主的网址导航!(2008-05-23)    热烈祝贺台州Link资讯改名为中国站长资讯!希望在以后日子里得到大家的大力支持和帮助!(2008-04-10)    热烈祝贺台州Link论坛改名为台州站长论坛!希望大家继续支持和鼓励!(2008-04-10)    台州站长论坛原[社会琐碎]版块更名为[生活百科]版块!(2007-09-05)    特此通知:新台州站长论坛的数据信息全部升级成功!">特此通知:新台州站长论坛的数据信息全部升级成功!(2007-09-01)    台州站长论坛对未通过验证的会员进行合理的清除,请您谅解(2007-08-30)    台州网址导航|上网导航诚邀世界各地的网站友情链接和友谊联盟,共同引领网站导航、前进!(2007-08-30)    禁止发广告之类的帖,已发现立即删除!(2007-08-30)    希望各位上传与下载有用资源和最新信息(2007-08-30)    热烈祝贺台州站长论坛全面升级成功,全新上线!(2007-08-30)    
便民网址导航,轻松网上冲浪。
台州维博网络专业开发网站门户平台系统
您当前的位置: 首页 » ASP/ASP.NET编程 » VB.NET实现图象文件和XML文件互换

VB.NET实现图象文件和XML文件互换

论坛链接
  • VB.NET实现图象文件和XML文件互换
  • 发布时间:2011-05-04 15:38:54    浏览数:5481    发布者:abcdef133    设置字体【   
通过.NET 框架下的FromBase64String和ToBase64String方法可以很容易地实现图象文件和XML文件的互换。这样可以轻易解决以XML格式保存图片的问题。以下是利用VB.NET实现图象文件和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
娱乐休闲专区A 影视预告B 音乐咖啡C 英语阶梯D 生活百科
网页编程专区E AMPZF HTMLG CSSH JSI ASPJ PHPK JSPL MySQLM AJAX
Linux技术区 N 系统管理O 服务器架设P 网络/硬件Q 编程序开发R 内核/嵌入
管理中心专区S 发布网址T 版主议事U 事务处理

Warning: Unknown: open(/tmp/sess_9b12t1kdki62fgphojtitru300, O_RDWR) failed: Disk quota exceeded (122) in Unknown on line 0

Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0