Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display Image from XML file (where image data stored in xml in byte format) in window form.
And i'm using below code.
C#
try
{
    byte[] imageData = null;
    int id = 6909;
    Image newImage;
 
    XDocument xDoc = XDocument.Load("Imageurl.xml");
    var q = from c in xDoc.Descendants("a")
            where c.Element("ID").Value == id.ToString()
            select new
                {
                 imageurl= c.Element("imgurl").Value

                };
    foreach (var a in q)
    {  
       imageData = Encoding.UTF8.GetBytes(a.imageurl);                 

        break;
    }
    
    //Read image data into a memory stream
    using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
    {
        ms.Write(imageData, 0, imageData.Length);

        //Set image variable value using memory stream.
        newImage = Image.FromStream(ms, true);
    }

    //set picture
    pictureBox1.Image = newImage;    
}
catch (Exception obj)
{
    MessageBox.Show(obj.ToString());
   
}

but it's given Parameter is not valid error message
Please help me
Posted
Updated 28-Oct-13 1:37am
v3
Comments
Marvin Ma 28-Oct-13 6:11am    
Try to Deserialize the XML document and convert the byte[] to image.

1 solution

Hi,
this worked for me:
C#
private void Form1_Load(object sender, EventArgs e)
{
    string s = "/9j/4AAQSkZJRgABAgEBLAEsAAD ..."; // XML photo as string
  pictureBox1.Image = Base64ToImage(s);

}
static Image Base64ToImage(string base64String)
{
    byte[] imageBytes = Convert.FromBase64String(base64String);
    MemoryStream ms = new MemoryStream(imageBytes);
    return Image.FromStream(ms, true);
}
 
Share this answer
 
Comments
Niraj K Verma 28-Oct-13 8:26am    
Thank you Sir

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