Click here to Skip to main content
15,877,637 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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[^]
Posted
Updated 24-Jan-15 19:55pm
v3
Comments
Zoltán Zörgő 24-Jan-15 15:42pm    
Any progress?
Zoltán Zörgő 25-Jan-15 1:13am    
Dude, you have most of the code, I have provided you the tool to store and retrieve bitmap as text. You already store and retrieve text in the XML, at this point it is not different - just use it where it belongs. That was the harder part. Try to figure out something on your own. Don't expect us to do your work. Assigning the bitmap as source for a PictureBox is really the basic...
daddy356 25-Jan-15 1:59am    
Thanks anyway
daddy356 26-Jan-15 16:19pm    
I feel like an idiot, A hint on where it goes

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();
}
 
Share this answer
 
v2
C#
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
  {
  }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900