Inserting string from XML file to System.Drawing.Image

What am I doing wrong I simply want to add the "im" field at the end of the path, so that the correct jpg is shown.  I've created filestreams and everything works fine for the the text fields.  The images are located in the "Program Files\SmartDeviceApplication1" directory, as is the XML file.  If I type in the actual name of an image, it works, but of course the image is shown for every entry.  (So I know the directory path is correct)   Any help is appreciated!  Martina

 

 Snippet XML file:

<im>kopf_ap.jpg</im>

 

Snippet Code (C#)

 

picbox.DataBindings.Add("Image",dt,"im");

Bitmap bmp= new Bitmap("\\Program Files\\SmartDeviceApplication1\\");

this.picbox.Image = bmp +"im";



Answer this question

Inserting string from XML file to System.Drawing.Image

  • zaabdullah

    In the simplest sense, I want to do this:
    \\Program Files\\SmartDeviceApplication1\\<im>
    so that it reads:
    \\Program Files\\SmartDeviceApplication1\\kopf_ap.jpg

    And that the picturebox shows the path of a particular ID.  The images are located in the SmartDeviceApplication1 directory.  I do not want to be able to write to the xml file, nor do I want to save it.  I just want to read and show the contents of the document.

     I used the example provided by Microsoft QuickStarts page as a guide:  http://samples.gotdotnet.com/quickstart/CompactFramework/doc/adonetdatabinding.aspx  I am new to C# programming.

    Here's the beginning of my xml doc...

    < xml version="1.0" encoding="UTF-8" >

    <dataroot xmlns="http://www.w3.org/2000/10/fragenkatalog.xsd">

    <fragenkatalog>

    <ca_id>4</ca_id>

    <ca_titel>head ap</ca_titel>

    <ca_bildurl1>kopf_ap.jpg</ca_bildurl1>

    <ca_disabled>0</ca_disabled>

    <ca_alter>999</ca_alter>

    <ca_sex>unknown sex</ca_sex>

    </fragenkatalog>

     

    A snippet of my .cs document.......

    public ADONETDataBinding()

    {

    InitializeComponent();

    // Create a new dataset.

    fragenkatalogDS = new DataSet();

    // Set the Locale for the DataSet,

    // using the current culture as the default

    fragenkatalogDS.Locale = CultureInfo.CurrentCulture;string xmlFileName;

    //

    // Set file names and create file streams.

    // The XSD, XML, and EXE must be in the same directory.

    xmlFileName = "\\Program Files\\SmartDeviceApplication1\\fragenkatalog.xml";

    xsdFileName = "\\Program Files\\SmartDeviceApplication1\\fragenkatalog.xsd";

    FileStream FsXML = new FileStream(xmlFileName,FileMode.Open);

    FileStream FsXSD = new FileStream(xsdFileName,FileMode.Open);

    // Load the schema into the DataSet.

    XmlTextReader xtrXSD = new XmlTextReader(FsXSD);

    fragenkatalogDS.ReadXmlSchema(xtrXSD);

    xtrXSD.Close();

    FsXSD.Close();

    // Load the data into the DataSet.

    XmlTextReader xtrXML = new XmlTextReader(FsXML);

    fragenkatalogDS.ReadXml(xtrXML);

    xtrXML.Close();

    FsXML.Close();

    // Get a DataTable to conveniently use for binding.

    DataTable dt = fragenkatalogDS.Tables["fragenkatalog"];

    //Bind the list box to abbreviations of the states.

    ca_id.DataSource = dt;

    ca_id.DisplayMember = "ca_titel";

    //Bind the label to show the name of the state.

    ca_titel.DataBindings.Add("Text",dt,"ca_titel");

    ca_alter.DataBindings.Add("Text",dt,"ca_alter");

    ca_sex.DataBindings.Add("Text",dt,"ca_sex");

    ca_bildurl.DataBindings.Add("Image",dt,"ca_bildurl1");

    }

    /// <summary>

    ///

    /// Clean up any resources being used.

    /// </summary>

    protected override void Dispose( bool disposing )

    {

    base.Dispose( disposing );

    }

     

     

    //

    // ca_bildurl

    //

    this.ca_bildurl.Location = new System.Drawing.Point(8, 64);

    this.ca_bildurl.Size = new System.Drawing.Size(150, 180);

    this.ca_bildurl.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;

    this.ca_bildurl.Image = new Bitmap ("\\Program Files\\SmartDeviceApplication1\\" + "ca_bildurl1");

     

     

    Thanks a lot for helping me.  I've been having such a hard time with getting over this step.   

     

    Martina.


  • Raffaele Rialdi

    Its not really clear what you are trying to accomplish. Could you post more context

    Are you trying to build your data source from XML like the following ...

    XmlDocument xmlDoc = new XmlDocument();

    xmlDoc.LoadXml(@"<docRoot><im>this.jpg</im><im>that.jpg</im></docRoot>");

    DataTable dt = new DataTable("Images");

    dt.Columns.Add("im");

    foreach(XmlNode imNode in xmlDoc.DocumentElement.ChildNodes)

    {

    DataRow dr = dt.NewRow();

    dr["im"] = imNode.InnerText;

    }

    pictureBox1.DataBindings.Add("Image",dt,"im");

    If so, what do you hope to do with the data source Your use of the im field in the property set on picture box doesn't seem to fit.

     



  • Ivan Selchenkov

    Hey Jeff!

    THANK YOU!  It worked!  I can at least display the first image.  I'm now working on how to move to the next image when I click on the next ID...

     

    I'm so excited!  Thanks a lot for your help. I really appreciate it!

     

    Martina


  • douglasp

    Danke, Jeff!

     

     

    I'm not from Germany, but lived there for about 10 years.  I guess you could say I'm sort of a mutt. 

     

    I will definitely try this out.  I appreciate your help already.  I see what you're saying, and will try it out and let you know. 

     

    Thanks again!

     

    Martina


  • Warren13

    For all who are interested:

    Here's my XML code:

    <fragenkatalog>

    <ca_id >4</ ca_id>

    <ca_titel>head ap</ca_titel>

    <ca_bildurl1>kopf_ap.jpg</ca_bildurl1>

    <ca_disabled>0</ca_disabled>

    <ca_alter>999</ca_alter>

    <ca_sex>n</ca_sex>

    </fragenkatalog>



    And the cs code


    public ADONETDataBinding()

    {


    InitializeComponent();

    fragenkatalogDS = new DataSet();

    fragenkatalogDS.Locale = CultureInfo.CurrentCulture;string xmlFileName;

    xmlFileName = "\\Program Files\\SmartDeviceApplication1\\fragenkatalog.xml";

    xsdFileName = "\\Program Files\\SmartDeviceApplication1\\fragenkatalog.xsd";

    FileStream FsXML = new FileStream(xmlFileName,FileMode.Open);

    FileStream FsXSD = new FileStream(xsdFileName,FileMode.Open);


    XmlTextReader xtrXSD = new XmlTextReader(FsXSD);

    fragenkatalogDS.ReadXmlSchema(xtrXSD);

    xtrXSD.Close();

    FsXSD.Close();

    XmlTextReader xtrXML = new XmlTextReader(FsXML);

    fragenkatalogDS.ReadXml(xtrXML);

    xtrXML.Close();

    FsXML.Close();

    DataTable dt = fragenkatalogDS.Tables["fragenkatalog"];

    ca_id.DataSource = dt;
    ca_id.DisplayMember = "ca_titel";

    //Thanks MSDN Forums (Jeff, Ilya!)
    string root = "\\Program Files\\SmartDeviceApplication1\\";
    DataRowView drv = (DataRowView)ca_id.SelectedItem;

    ca_bildurl.Image = new Bitmap(System.IO.Path.Combine(root,drv.Row["ca_bildurl1"].ToString()));
    ca_titel.DataBindings.Add("Text",dt,"ca_titel");
    ca_alter.DataBindings.Add("Text",dt,"ca_alter");
    ca_sex.DataBindings.Add("Text",dt,"ca_sex");
    burl.DataBindings.Add("Text",dt,"ca_bildurl1");


  • AntonieB123

    Martina, are you from Germany Deutsche ist meine zweite Sprache.

    Unfortunately, that's about all I remember of it, outside of saying I don't speak much German.

    In any case, when I look at the sample you mention, and some of your other posts concerning loading the image, I don't think direct data binding like the sample will work for you.

    The issue is that the binding wants something of the proper type to fit in the specified property of the bound control. You are giving a string location to an image property at bind-time. Instead, I think you should subscribe to the SelectedIndexChanged event on your listbox, and then look in the datatable for the bildurl to append to the graphics folder, just as you show in the additional snippet...

    this.ca_bildurl.Image = new Bitmap ("\\Program Files\\SmartDeviceApplication1\\" + "ca_bildurl1");

    But, be sure to append the variable, not the quoted name of the datatable column.

    It might look something like this...

    private void ca_id_SelectedIndexChanged(object sender, System.EventArgs e)

    {

    string imageRoot = @"c:\Program Files\SmartDeviceApplication1Files\SmartDeviceApplication1";

    DataRowView drv = (DataRowView)ca_id.SelectedItem;

    ca_bildurl.Image = new Bitmap(System.IO.Path.Combine(imageRoot,drv.Row["bildurl1"].ToString()));

    }



  • Inserting string from XML file to System.Drawing.Image