Monday, 29 September 2014

Image Save In XML and Load XML Bitmap Image File in Windows Application Using C#.Net

9/29/2014 - By Pranav Singh 2

This article will show you how you can convert the image into bitmap image and save it into xml file and how you can reload the xml saved bitmap image into image form using c#.net in windows application.


So for this article first we will create a new windows application and add some controls in it.


Now create an xml folder in you bin-> debug folder


Now add the below code into you code end.

using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml;
using System.IO;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        ///
        /// Image Save In XML and Load XML Bitmap Image File in Windows Application Using C#.Net
             ///
        ///
        private void button1_Click(object sender, EventArgs e)
        {


            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    textBox1.Text = ofd.FileName;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap bmp = new Bitmap(textBox1.Text);
                TypeConverter converter = TypeDescriptor.GetConverter(typeof(Bitmap));
                string image = Convert.ToBase64String((byte[])converter.ConvertTo(bmp, typeof(byte[])));
                XmlTextWriter writer = new XmlTextWriter("XMLFile/PrintLabelList.xml", System.Text.Encoding.UTF8);
                writer.WriteStartDocument(true);
                writer.Formatting = Formatting.Indented;
                writer.Indentation = 2;
                writer.WriteStartElement("ImageBitMap");
                writer.WriteStartElement("ImageDetail");
                writer.WriteStartElement("Image");
                writer.WriteString(image);
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.Close();
                MessageBox.Show("File converted successfully.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            XDocument doc = XDocument.Load("XMLFile/PrintLabelList.xml");
            var records = (from lablemname in doc.Root.Elements("ImageDetail")
                           select new
                           {
                               Image = (string)lablemname.Element("Image").Value
                           }).FirstOrDefault();
            if (records != null)
            {
                string val = records.Image;
                byte[] bytes = Convert.FromBase64String(val);
                MemoryStream mem = new MemoryStream(bytes);
                Bitmap bmp2 = new Bitmap(mem);
                pictureBox1.Image = bmp2;
            }
        }
    }
}

   
In above code first I have written the code to browse the file and then converted the image into bit map image and saved it into XML file.

Now on load button click I have again loaded the file from xml and converted the bitmap image into image using c#.

So we have done have a look of this output.


In above i have selected below mention image


In above I have browsed the file and then converted it into bitmap image and saved it into xml. Here is the xml file.


Now click on load button to again load the file.

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

2 comments:

  1. CODE O'K ))))

    ReplyDelete
  2. Error 1 The name 'picturebox1' does not exist in the current context C:\Users\user\Desktop\project\test2\test2\Form1.cs 78 17 test2
    i have this error :(

    ReplyDelete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top