How to store an image in XML data and read it out by Icesnaker-program Rhapsody

Source: Internet
Author: User
Tags int size string
xml| data

It was interesting to see how XML stored data in the past, especially when I saw an article saying that you could use XML instead of SQL server2000 to store data and interactive data, which is still under study. Finally how to access the image to the XML code to fix:

Reading images

Defining an image source and destination XML file

String imgfilename = @ "d:\ China Mobile advertising. JPG ";

String xmlfilename = @ "D:\img.xml";

XmlTextWriter axmltextwriter = new XmlTextWriter (XMLfileName, System.Text.Encoding.Default);

axmltextwriter.formatting = formatting.indented;

Try

{

Axmltextwriter.writestartdocument ();

Axmltextwriter.writecomment ("Contains a BinHex JPEG image");

Axmltextwriter.writestartelement ("JPEG");

Below is a common code for reading images.

System.IO.FileInfo fi = new System.IO.FileInfo (imgfilename);

int size = (int) fi. Length;

Read the JPEG file

byte []img = new Byte[size];

System.IO.FileStream fs = new System.IO.FileStream (Imgfilename, System.IO.FileMode.Open);

System.IO.BinaryReader br = new System.IO.BinaryReader (FS);

img = br. Readbytes (size);

Br. Close ();

Note that the BinHex code is used here.

Axmltextwriter.writebinhex (img,0,size);

Axmltextwriter.writeenddocument ();

}

catch (XmlException Xmle)

{

Response.Write (Xmle.message);

}

Finally

{

Axmltextwriter.close ();

}

displaying images

Simply put a PictureBox in the window and write the following code in a button

String xmlfilename = @ "D:\img.xml";

XmlTextReader axmltextreader = new XmlTextReader (XMLfileName);

Axmltextreader.read ();

Axmltextreader.movetocontent ();

if (Axmltextreader.localname = = "JPEG")

{

System.IO.FileInfo fi = new System.IO.FileInfo (xmlfilename);

int isize = (int) fi. Length;

byte []img = new Byte[isize];

Axmltextreader.readbinhex (img,0,isize);

Byte to Image Object

System.IO.MemoryStream ms = new System.IO.MemoryStream ();

Ms. Write (img,0,isize);

Bitmap bmp = new Bitmap (MS);

Ms. Close ();

This.pictureBox1.Image = BMP;

}

Axmltextreader.close ();

Thank you, the author, haha, i "steal" over, easy to see myself;




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.