把图象文件转换成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

(0)

相关推荐

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

    把图象文件转换成XML格式文件 利用.NET 框架下的FromBase64String和ToBase64String方法可以很容易地实现图象文件和XML文件的互换.这样可以轻易解决以XML格式保存图片的问题.代码如下: Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows 窗体设计器生成的代码 " Public Sub New() MyBase.New() InitializeComponent(

  • java 读取excel文件转换成json格式的实例代码

    需要读取excel数据转换成json数据,写了个测试功能,转换正常: JSON转换:org.json.jar 测试类:  importFile.java: package com.siemens.util; import java.util.ArrayList; import java.util.List; import org.json.JSONException; import org.json.JSONObject; import org.apache.poi.ss.usermodel.R

  • php将数组转换成csv格式文件输出的方法

    本文实例讲述了php将数组转换成csv格式文件输出的方法.分享给大家供大家参考.具体实现方法如下: <?php $sales = array( array('east','2005-01-01','2005-02-01',12.54), array('west','2005-01-01','2005-02-01',546.33), array('south','2005-01-01','2005-02-01',93.26), array('north','2005-01-01','2005-02

  • Python实现把utf-8格式的文件转换成gbk格式的文件

    需求:将utf-8格式的文件转换成gbk格式的文件 实现代码如下: 复制代码 代码如下: def ReadFile(filePath,encoding="utf-8"):     with codecs.open(filePath,"r",encoding) as f:         return f.read()   def WriteFile(filePath,u,encoding="gbk"):     with codecs.open(

  • C#中利用LINQ to XML与反射把任意类型的泛型集合转换成XML格式字符串的方法

    在工作中,如果需要跟XML打交道,难免会遇到需要把一个类型集合转换成XML格式的情况.之前的方法比较笨拙,需要给不同的类型,各自写一个转换的函数.但是后来接触反射后,就知道可以利用反射去读取一个类型的所有成员,也就意味着可以替不同的类型,创建更通用的方法.这个例子是这样做的:利用反射,读取一个类型的所有属性,然后再把属性转换成XML元素的属性或者子元素.下面注释比较完整,就话不多说了,有需要看代码吧! using System; using System.Collections.Generic;

  • Python实现将HTML转换成doc格式文件的方法示例

    本文实例讲述了Python实现将HTML转换成doc格式文件的方法.分享给大家供大家参考,具体如下: 网页上的一些文章,因为有格式的原因,它们在网页上的源码都是带有html标签的,用css来进行描述.本文利用HTML Parser 和docx两个模块,对网页进行解析并存储到word文档中.转换出来的格式相对还是有些粗糙,不喜勿喷.话不多说,直接上代码. class HTMLClient: #获取html网页源码 def GetPage(self, url): #user_agent = 'Moz

  • python将.ppm格式图片转换成.jpg格式文件的方法

    将.ppm格式的图片转换成.jpg格式的图像,除了通过软件转换,还可以使用python脚本直接转换,so easy!!! from PIL import Image img = Image.open("images/25_color.ppm") img.save("JPGimg/25_color.jpg") img.show() 以上这篇python将.ppm格式图片转换成.jpg格式文件的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持

  • python heic后缀图片文件转换成jpg格式的操作

    我就废话不多说了,直接上代码 heic_to_jpg.py import subprocess import os import io import whatimage import pyheif import traceback from PIL import Image def decodeImage(bytesIo): try: fmt = whatimage.identify_image(bytesIo) # print('fmt = ', fmt) if fmt in ['heic']

  • PHP中将数组转成XML格式的实现代码

    下面是网上的 复制代码 代码如下: class ArrayToXML { /** * The main function for converting to an XML document. * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document. * * @param array $data * @param string $rootNodeName

  • 解决python将xml格式文件转换成txt文件的问题(xml.etree方法)

    概述 先来介绍一下xml格式的文件,从数据分析的角度去看xml格式的数据集,具有以下的优点开放性(能在任何平台上读取和处理数据,允许通过一些网络协议交换xml数据).简单性(纯文本,能在不同的系统之间交换数据).结构和内容分离(不同于HTML,数据的显示和数据本身是分开的).可扩展性(派生出其他标记语言) 问题描述 那么我们在进行数据分析的时候,如何运用xml里面的数据呢? 我们就需要将这类文件转化成其他类型的文件. (其实我认为说成提取xml的数据组成新的类型文件比较好一点) 就我个人的观点,

随机推荐