欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-22 08:16:41
把图象文件转换成xml格式文件 利用.net 框架下的frombase64string...

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