Convert image files into XML files
The frombase64string and tobase64string methods in the. NET Framework can easily implement interchange between image files and XML files. This can easily solve the problem of saving images in XML format.CodeAs follows:
Public class form1
Inherits system. Windows. Forms. Form
# Region "code generated by Windows Form Designer"
Public sub new ()
Mybase. New ()
Initializecomponent ()
Add any initialization after initializecomponent () is called
End sub
Form rewriting to clear the component list.
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
Required by the Windows Form Designer
Private components as system. componentmodel. icontainer
Note: The following procedure is required by the Windows Forms designer
You can use the Windows Form Designer to modify this process.
Do not use the code editor to modify it.
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 = "Save the image as 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 = "getting images from 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 = "browse images... "
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 = "[wonderful world of Meng xianhui ]"
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 = "Examples of interchange between image files and XML files"
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 button#click (byval sender as system. Object, byval e as system. eventargs )_
Handles button1.click
If myfile = "" then
MessageBox. Show ("select an image! "," Error ", 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> Meng xianhui </Name> <photo>" + PIC + "</photo> </PIC> ")
Myxml. Save ("C: \ myphoto. xml ")
Label1.text = "file saved to:" + 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 | all image files (*. *) | *. *"
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 ("ipolice.exe", "http://xml.sz.luohuedu.net /")
End sub
End Class