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

把图象文件转换成XML格式文件,第1张

利用 NET 框架下的FromBase String和ToBase String方法可以很容易地实现图象文件和XML文件的互换 这样可以轻易解决以XML格式保存图片的问题裤晌雹 代码如下 Public Class Form 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 ThenIf Not (ponents Is Nothing) Thenponents Dispose()End IfEnd IfMyBase Dispose(disposing)End SubWindows 窗胡帆体设计器所必需的Private ponents As System ComponentModel IContainer注意 以下过程是 Windows 窗体设计器所必需的可以使用 Windows 窗体设计器修改此过程 不要使用代码编辑器修改它 Friend WithEvents Button As System Windows Forms ButtonFriend WithEvents Button As System Windows Forms ButtonFriend WithEvents PictureBox As System Windows Forms PictureBoxFriend WithEvents Button As System Windows Forms ButtonFriend WithEvents Label As System Windows Forms LabelFriend WithEvents Label As System Windows Forms Label<System Diagnostics DebuggerStepThrough()>Private Sub InitializeComponent()Me Button = New System Windows Forms Button()Me Button = New System Windows Forms Button()Me PictureBox = New System Windows Forms PictureBox()Me Button = New System Windows Forms Button()Me Label = New System Windows Forms Label()Me Label = New System Windows Forms Label()Me SuspendLayout()Button Me Button Location = New System Drawing Point( )Me Button Name = Button Me Button Size = New System Drawing Size( )Me Button TabIndex = Me Button Text = 将图象保存成XML Button Me Button Location = New System Drawing Point( )Me Button Name = Button Me Button Size = New System Drawing Size( )Me Button TabIndex = Me Button Text = 从XML中得到图象 PictureBox Me PictureBox Location = New System Drawing Point( )Me PictureBox Name = PictureBox Me PictureBox Size = New System Drawing Size( )Me PictureBox TabIndex = Me PictureBox TabStop = FalseButton Me Button Location = New System Drawing Point( )Me Button Name = Button Me Button Size = New System Drawing Size( )Me Button TabIndex = Me Button Text = 浏览图片… Label Me Label Location = New System Drawing Point( )Me Label Name = Label Me Label Size = New System Drawing Size( )Me Label TabIndex = Label Me Label Location = New System Drawing Point( )Me Label Name = Label Me Label Size = New System Drawing Size( )Me Label TabIndex = Me Label Text = 【孟宪会之精彩世界】 Form Me AutoScaleBaseSize = New System Drawing Size( )Me ClientSize = New System Drawing Size( )Me Controls AddRange(New System Windows Forms Control() {Me Label Me Label Me Button Me PictureBox Me Button Me Button })Me Name = Form Me Text = 图象文件和XML格式文件互换例子 Me ResumeLayout(False)End Sub#End RegionPrivate MyFile As String = Private MyFileExt As String = Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) _Handles Button ClickDim pic As StringDim MyXml As System Xml XmlDocument = New System Xml XmlDocument()MyXml Load( c:\MyPhoto xml )Dim picNode As System Xml XmlNodepicNode = MyXml SelectSingleNode( /pic/photo )pic = picNode InnerTextDim memoryStream As System IO MemoryStreammemoryStream = New System IO MemoryStream(Convert FromBase String(pic))Me PictureBox Image = New System Drawing Bitmap(memoryStream)memoryStream Close()End SubPrivate Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) _Handles Button ClickIf MyFile = ThenMessageBox Show( 请选择一个图片! 错误 MessageBoxButtons OK MessageBoxIcon Warning)Exit SubEnd IfDim 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 Byteb = memoryStream GetBuffer()Dim pic As String = Convert ToBase String(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 )Label Text = 文件被保存到了 + Microsoft VisualBasic ChrW( ) + c:\MyPhoto xml End SubPrivate Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) _Handles Button ClickDim openFileDialog As New OpenFileDialog()openFileDialog InitialDirectory = c:\ openFileDialog Filter = PNG(* png)|* png|Gif(* gif)|* gif|Jpg(* jpg)|* jpg|所有图象文件(* *)|* * openFileDialog FilterIndex = openFileDialog RestoreDirectory = TrueIf openFileDialog ShowDialog() = DialogResult OK ThenMyFile = openFileDialog FileName()MyFileExt = MyFile Substring(MyFile LastIndexOf( ) + )End IfEnd SubPublic Function GetImageType(ByVal str As String) As System Drawing Imaging ImageFormatSelect Case str ToLower()Case jpg Return System Drawing Imaging ImageFormat JpegCase gif Return System Drawing Imaging ImageFormat GifCase tiff Return System Drawing Imaging ImageFormat Tiff()Case icon Return System Drawing Imaging ImageFormat IconCase image/png Return System Drawing Imaging ImageFormat PngCase ElseReturn System Drawing Imaging ImageFormat MemoryBmpEnd SelectEnd FunctionPrivate Sub Form _Closing(ByVal sender As Object ByVal e As System ComponentModel CancelEventArgs) _Handles MyBase ClosingSystem Diagnostics Process Start( IExplore exe )End SubEnd Class lishixinzhi/Article/program/net/201311/11970

1.打开pr,新建项目。

2.导入世模素材,拖放新建或返银序列。

3.点击文件-导出-final cut pro xml选衫宴项。

4.d出设置保存路径以及名称的窗口,设置好点击确定

5保存好之后在保存的文件夹中就可以看到xml格式的文件。

业界通常的做法是把图片转春斗搏换销老成base64然后存入xml,所以解析其实就是把base64逆转换成图片。如果你遇到的也是这样的场景,baidu检索扒祥java image base64可以看到很多的代码例子。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12299757.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存