Click here to Skip to main content
15,921,959 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have done a sample windows application which will show the images as a slide show in the PictureBox when I click on either Previous button or Next button.
I have created a Image folder in the Project, which contains few images.

Actually the code is working fine, but the problem as ~/Images is not working, so I have dragged and droped the path of the Image folder. It will work fine in my system but it won't work in other system as it contains my systems path. I have tried in many other ways but, it didnt work.
How to solve this? I want the folder to be in the project itself. Or can we do this using Resource file and how to do it. I dont want to use any openfiledialog.

Please check the below code:

public partial class Form1 : Form
    {
        ArrayList alist = new ArrayList();
        int i = 0;
        int filecount = 0;

        public Form1()
        {
            InitializeComponent();
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            //Specify Source folder path here
            System.IO.DirectoryInfo inputDir = new System.IO.DirectoryInfo(@"C:\Users\dss\Desktop\Assignments\Assignments\WinForms Assignments and Documentation\PictureBoxSampleWinApp\PictureBoxSampleWinApp\Images");
            try
            {
                if ((inputDir.Exists))
                {

                    //Get Each jpg files from a source folder and stored in the arraylist
                    System.IO.FileInfo file = null;
                    foreach (System.IO.FileInfo eachfile in inputDir.GetFiles())
                    {
                        file = eachfile;
                        if (file.Extension.ToLower() == ".jpg")
                        {
                            alist.Add(file.FullName);
                            //Store file count here 
                            filecount = filecount + 1;
                        }
                    }
                    //Initally show first image in the picture box
                    pbPictures.Image = Image.FromFile(alist[0].ToString()); 
                    i = 0;
                    btnPrevious.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            //check previous postion image is there or not
            if (i - 1 >= 0)
            {
                pbPictures.Image = Image.FromFile(alist[i - 1].ToString());
                i = i - 1;
                btnNext.Enabled = true;
            }
            else
            {
                btnPrevious.Enabled = false;
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            //check next postion image is there or not
            if (i + 1 < filecount)
            {
                pbPictures.Image = Image.FromFile(alist[i + 1].ToString());
                i = i + 1;
                btnPrevious.Enabled = true;
            }
            else
            {
                btnNext.Enabled = false;
            }
        }
    }



Thanks.
Posted

You can use the Application.ExecutablePath.
which will return The path and executable name for the executable file that started the application.

Put your .exe and image folder in same folder.

strinf imagepath= Application.ExecutablePath;
imagepath=imagepath.Remove(imagepath.LastIndexOf('\\') + 1);

imagepath+=your image folder.
 
Share this answer
 
Comments
fjdiewornncalwe 26-Sep-12 10:14am    
+4. A reasonable solution that would work, but only a four because it for these situations that the configuration environment was designed to deal with. It provides far more flexibility to the application.
You need to use the app.config file to store the image folder path. That way all end users can configure this path as needed.
http://msdn.microsoft.com/en-us/library/ms184658.aspx[^]
 
Share this answer
 
put the following code in web.config file->
-----------------------------------------
C#
<configuration>
  <appsettings>
      <add key="imageAddress" value="../HowToOpenPDF/Styles/Site.css" />
      </appsettings>
</configuration>

and use the following code where you want to access the image with name->
--------------------------------------------------------------------------
ConfigurationManager.AppSettings["imageAddress"].ToString();

or using javaScript on button click:
-------------------------------------
<asp:button runat="server" id="btnConfig" text="Read From Web Config" xmlns:asp="#unknown">
OnClientClick="ReadFromWebConfig()"/>


function ReadFromWebConfig() {
var hk = '&lt;%=ConfigurationManager.AppSettings["imageAddress"].ToString() %&gt;'
alert(hk);
 
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