. NET to save picture data to an XML document

Source: Internet
Author: User
Tags format object exit net return string tostring
xml| data because I'm going to build the form in XML and look at the UI pattern for the most recent project, I'm complaining: I build the form dynamically through XML, but the UI in the main form uses an icon to determine which form to use. How do I make the icon for the main form also dynamically loaded and the icon and the form you want to generate are also associated? And I thought about using XML to check MSDN, See only XmlTextWriter and XmlTextReader respectively have xmltextwriter.writebase64 and xmltextreader.readbase64 can manipulate the image of binary byte data. But XmlTextWriter and XmlTextReader are far less convenient to operate than XmlDocument, and if you use both of these I have to write too much code.
Haunted Me one day, remember that I have seen an article about how to store picture data to XML files, but how can not find, and then finally found the relevant content in an English website, but also posted in 2003, Khan.
OK, no nonsense, I put my implementation code posted to everyone.
Private XmlDocument document;
private string FilePath = Application.startuppath + " \\.. \\.. \\FormStyle.xml"; Formstyle.xml file Address

private void Frmmain_load (object sender, System.EventArgs e)
{
if (document = = NULL)
{
Document = new XmlDocument ();
Document. Load (FilePath);
}

Select only nodes that contain form
XmlNodeList Formnodes = document. getElementsByTagName ("Form");
Lbicons.beginupdate ();
LbIcons.Items.Clear ();
foreach (XmlNode node in formnodes)
{
Put the name of the node in the Drop-down list
LBICONS.ITEMS.ADD (node. attributes["Name"]. Value);
}
Lbicons.endupdate ();
}

private void Lbicons_selectedvaluechanged (object sender, System.EventArgs e)
{
Find out if there is an image element under the form selected by the Drop-down box, or exit if none
XmlNode node = document. Documentelement.selectsinglenode (String. Format ("descendant::form[@Name = ' {0} ']/image", LbIcons.SelectedItem.ToString ()));
if (node = null)
Return

If the image element is included, the element value is converted to base64string and then placed in the memory stream
using (MemoryStream mem = new MemoryStream (convert.frombase64string) (node. innertext)))
{
Load memory stream data as bitmap
Bitmap bmp = Bitmap.fromstream (mem) as Bitmap;
pictureBox1.Image = BMP;
}
}

private void btnAdd_Click (object sender, System.EventArgs e)
{
If the Txtfilepath.text file is not present, exit
if (! File.exists (txtfilepath.text) | | LbIcons.Items.Count = 0)
Return

if (Lbicons.selectedindex = = 1)
Lbicons.selectedindex = 0;

if (document = = NULL)
{
Document = new XmlDocument ();
Document. Load (FilePath);
}

Read the bitmap.
string data = null;
Bitmap bmp = new Bitmap (txtfilepath.text);
using (MemoryStream mem = new MemoryStream ())
{
Bmp. Save (Mem, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.BMP);
Convert bitmap data to base64string into a string
data = convert.tobase64string (mem. ToArray ());
}

Finds whether the currently selected form contains an image node, and creates a new
XmlNode node = document. Documentelement.selectsinglenode (String. Format ("descendant::form[@Name = ' {0} ']", lbIcons.SelectedItem.ToString ());
XmlNode Imagenode = document. Documentelement.selectsinglenode (String. Format ("descendant::form[@Name = ' {0} ']/image", LbIcons.SelectedItem.ToString ()));
if (Imagenode = null)
{
Imagenode = document. CreateElement ("Image");
Node. AppendChild (Imagenode);
}

To save bitmap data to an XML document
Imagenode.innertext = data;
Document. Save (FilePath);
}


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.