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.
Some of my previous articles are as follows: How
to Add HyperLink and Retrieve Row Value on Link Click in DataGridView #,
Windows application, How
to Add Button Control and Retrieve Row Value on Button Click in DataGridView
Using C#.Net, Windows Application, How
to Save Record in XML File and Read XML to Display in DataGridview Using C#.net
in Windows Application, Splash
Screen With Please Wait OR Loading Message in C#.net in Windows Application,
Save
Panel with Control Inside It as Image C#.net, VB.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.
CODE O'K ))))
ReplyDeleteError 1 The name 'picturebox1' does not exist in the current context C:\Users\user\Desktop\project\test2\test2\Form1.cs 78 17 test2
ReplyDeletei have this error :(