VB.NET如何实现图象文件和XML文件互换
发表于:2025-01-18 作者:千家信息网编辑
千家信息网最后更新 2025年01月18日,VB.NET如何实现图象文件和XML文件互换,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。通过.NET 框架下的From
千家信息网最后更新 2025年01月18日VB.NET如何实现图象文件和XML文件互换
VB.NET如何实现图象文件和XML文件互换,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
通过.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.LabelPrivate 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(" ") 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 孟宪会 " + pic + "
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。
文件
图象
窗体
设计
代码
图片
格式
过程
帮助
清楚
精彩
世界
例子
内容
对此
文章
新手
方法
更多
框架
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
七年级信息技术网络安全
安徽企业软件开发收费报价表
数据库原理的思政作业
服务器3200当2666
app积分墙软件开发价格
企业电商软件开发机构
数据库参照性完整缺失
类似于神经网络技术
群晖服务器的安全级别
变压器设计软件开发
软件开发后端指什么
高端软件开发这个专业好学吗
数据库备份多了怎么删除
方舟手游六人服务器一个月多少钱
mdf架设数据库
服务器访问具体流程
沈阳华纳迈特网络技术有限公司
碧蓝航线国服有哪些服务器
检索工具与数据库
博翀网络技术
校园安全及网络安全笔记
网络技术评分表
我的世界手游起床战争的服务器
博纳软件开发公司
玩转DISCUZ数据库下载
lol端游服务器人数
学生学习用的服务器配置
如何建立数据库管理
用友的系统管理查不到服务器
网络安全班会课有哪些内容