Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have some images that i saved in my program along with coordinates that have been marked from the images. The coordinates are saved in a text file with the name of the image. I want to be able to select the image in a listbox and check if there is a text file with the same name as the selected image and then display the coordinate from the text file into another listbox.
Here is the code i have written:

C#
string path = Application.StartupPath;
                   string pathString = System.IO.Path.Combine(path, "output Directory");
                   string myFile = lstImages.SelectedItem.ToString() + ".artery";
                   pathString = System.IO.Path.Combine(pathString, myFile);

                   //Read and dispalay the data from file it it exist
                   if (System.IO.File.Exists(lstImages.SelectedItem.ToString()))
                   {
                       using(StreamReader sr = new StreamReader(myFile))
                       {
                           string line;
                           while((line = sr.ReadLine()) !=null)
                           {
                               lstArtery.Items.Add(line);
                           }
                       }
                   }


This is the code that fetches the image and displays it in the listbox. This code is working fine. But i have not been able to display the content of the text file when the image is selected

C#
//fetch and display the images selected in the list box
                   string filename = System.IO.Path.Combine(parentDir, lstImages.SelectedItem.ToString());
                   //picImageDisplay.Image = System.Drawing.Image.FromFile(filename);
                   //picImageDisplay.Image = (Bitmap).FromFile(filename);
                   Bitmap myImage = new Bitmap(System.Drawing.Image.FromFile(filename));
                   picImageDisplay.Image = myImage;


please i need help to be able to complete my project
Posted
Comments
Kenneth Haugland 30-Apr-15 11:14am    
Any chance that you can save it in a XML file instead? Should make it easier to deal with the file reading etc, as all the information will be in one place?
Member 9599225 30-Apr-15 11:19am    
@Kenneth Haughland. Thank you for your response I appreciate it. How will I be able to save it to XML file. I am new to c# programming. This is my first project
Kenneth Haugland 30-Apr-15 11:36am    
Here is a simple way of storing and loading a class into a XML file format:
https://support.microsoft.com/en-us/kb/316730
Abdul Samad KP 30-Apr-15 11:27am    
I think there is a mistake in your code for reading the text file
change
if (System.IO.File.Exists(lstImages.SelectedItem.ToString()))
to
if (System.IO.File.Exists(myFile))
Member 9599225 30-Apr-15 11:34am    
@Abdul Samad KP. I appreciate your input. I tried your suggestion but i have not been able to resolve the issue still.

1 solution

Thank you all for your contribution. I have solved the problem myself. I really appreciate your time and effort.
Here is the solution:

C#
/display the coordinates of the selected file
                    string path = Application.StartupPath;
                    string pathString = System.IO.Path.Combine(path, "output Directory");
                    // string myFile = lstImages.SelectedItem.ToString() + ".artery";
                    string myFile = System.IO.Path.Combine(pathString, lstImages.SelectedItem.ToString() + ".artery" );
                    pathString = System.IO.Path.Combine(pathString, myFile);

                    //Read and dispalay the data from file it it exist
                    if (System.IO.File.Exists(pathString))
                    {
                        using (StreamReader sr = new StreamReader(pathString))
                        {
                            lstArtery.Items.Add("Artery");
                            string line;
                            while ((line = sr.ReadLine()) != null)
                            {
                                lstArtery.Items.Add(line);
                            }
                        }
                    }
 
Share this answer
 

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