如何将图像从pictureBox保存并加载到xml,然后从xml保存到picturebox [英] How do I save and load an image from pictureBox to xml then from xml to picturebox

查看:65
本文介绍了如何将图像从pictureBox保存并加载到xml,然后从xml保存到picturebox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做的是,

程序启动时会显示一个列表视图

已添加其照片和信息的名称。

因此,当选择列表视图中的名称时,所选名称的信息将显示在指定的文本框中

并且图像出现在图片框中。



我当然希望我解释正确



我想将图像存储到Image节点每个人

所以当我点击列表视图中的名字时。

图片将加载到每个人的图片框中。



http://abload.de/img/602568pjfzg.png [ ^ ]

What I'm trying to do is,
when the program is launched it shows a list view
of the names that have been added with their photo and info.
So when a name in the list view is selected the info
of selected name appears in the assigned text boxes
and the image appears in the picture box.

I sure hope I explained that right

I want to store an image to the Image node for each person
So when I click on a name in the list view.
the picture will load in the picture box for each person.

http://abload.de/img/602568pjfzg.png[^]

推荐答案

在XML中使用stoging图像并不常见,但它是一种选择。如果您需要更改,我会使用二进制数据存储区。将图像存储为Base64字符串是一种很好的方法。请参见此处(两个方向): https://social.msdn.microsoft.com/Forums/en-US/04a68d95-4951-48ab-badc-88f2c3799ff3/store-and-retrieve -image-in-xml-using-c-without-serialization?forum = winforms [ ^ ]。

请注意,您需要将图像保存为特定格式的图像,因为图像没有格式。在你的情况下,最好使用jpeg。



在这里你有:

Stoging image in XML is not common, but it is an option. I would rahter use a binary datastore, if you changes are needed. Storing image as Base64 string is a good approach. See here for example (both directions): https://social.msdn.microsoft.com/Forums/en-US/04a68d95-4951-48ab-badc-88f2c3799ff3/store-and-retrieve-image-in-xml-using-c-without-serialization?forum=winforms[^].
Please note that you need save the image to stream in a specific format as Image has no format. In your case it is better using jpeg.

Here you have it:
public static class JpegAsBase64String
{
	public static string fromBitmap(Bitmap bmp)
	{
		string result;

		using (MemoryStream ms = new MemoryStream())
		{
			bmp.Save(ms, ImageFormat.Jpeg);
			result = Convert.ToBase64String(ms.ToArray());
		}
		
		return result;
	}
	
	public static Bitmap fromBase64String(string input)
	{
		byte[] bytes = Convert.FromBase64String(input);

		Bitmap result = null;
		using (MemoryStream m = new MemoryStream(bytes))
		{
			Bitmap bmpTmp = new Bitmap(m);
			result = new Bitmap(bmpTmp);
			bmpTmp.Dispose();
		}
		
		return result;
	}
}

//LinqPad demo:
void Main()
{
    var img = new Bitmap(@"D:\TEMP\2013.jpg");
	string b64 = JpegAsBase64String.fromBitmap(img);
	b64.Dump();
	JpegAsBase64String.fromBase64String(b64).Dump();
}


private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
  try
  {
    textBox1.Text = nodes[listView1.SelectedItems[0].Index].Full_Name;
    textBox2.Text = nodes[listView1.SelectedItems[0].Index].Address;
    textBox3.Text = nodes[listView1.SelectedItems[0].Index].Location;
    textBox4.Text = nodes[listView1.SelectedItems[0].Index].Phone_Number;
    textBox5.Text = nodes[listView1.SelectedItems[0].Index].Email_Address;
    dateTimePicker1.Value = nodes[listView1.SelectedItems[0].Index].Birth_Date;
    byte[] buffer = Convert.FromBase64String(nodes[listView1.SelectedItems[0].Index].Image);
    MemoryStream ms = new MemoryStream(buffer);
    Bitmap bmp = (Bitmap)Image.FromStream(ms);
    pictureBox1.Image = bmp;
    textBox6.Text = nodes[listView1.SelectedItems[0].Index].Notes;
  }
  catch
  {
  }
}

private void button1_Click(object sender, EventArgs e)
{
  Person p = new Person();

  p.Full_Name = textBox1.Text;
  p.Address = textBox2.Text;
  p.Location = textBox3.Text;
  p.Phone_Number = textBox4.Text;
  p.Email_Address = textBox5.Text;
  p.Birth_Date = dateTimePicker1.Value;
  Bitmap bmp = new Bitmap(pictureBox1.Image);
  TypeConverter converter = TypeDescriptor.GetConverter(typeof(Bitmap));
  p.Image = Convert.ToBase64String((byte[])converter.ConvertTo(bmp, typeof(byte[])));
  p.Notes = textBox6.Text;
  nodes.Add(p);
  listView1.Items.Add(p.Full_Name);
  textBox1.Text = "";
  textBox2.Text = "";
  textBox3.Text = "";
  textBox4.Text = "";
  textBox5.Text = "";
  textBox6.Text = "";
  dateTimePicker1.Value = DateTime.Now;
}

private void button2_Click(object sender, EventArgs e)
{
  try
  {
    nodes[listView1.SelectedItems[0].Index].Full_Name = textBox1.Text;
    nodes[listView1.SelectedItems[0].Index].Address = textBox2.Text;
    nodes[listView1.SelectedItems[0].Index].Location = textBox3.Text;
    nodes[listView1.SelectedItems[0].Index].Phone_Number = textBox4.Text;
    nodes[listView1.SelectedItems[0].Index].Email_Address = textBox5.Text;
    nodes[listView1.SelectedItems[0].Index].Birth_Date = dateTimePicker1.Value;
    Bitmap bmp = new Bitmap(pictureBox1.Image);
    TypeConverter converter = TypeDescriptor.GetConverter(typeof(Bitmap));
    nodes[listView1.SelectedItems[0].Index].Image = Convert.ToBase64String((byte[])converter.ConvertTo(bmp, typeof(byte[])));
    nodes[listView1.SelectedItems[0].Index].Notes = textBox6.Text;
    listView1.SelectedItems[0].Text = textBox1.Text;
  }
  catch
  {
  }
}


这篇关于如何将图像从pictureBox保存并加载到xml,然后从xml保存到picturebox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆