Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The system will display one image on PictureBox from the Upload folder(contains multiple images). After the user clicks on the pass button then it will move the current image that shows in the Picturebox from the Upload Folder to the Pass folder and it will go to the next images. When I clicked on the pass button there have no images move to the pass folder. May I know how to fix this thanks?


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public partial class Form4 : Form
    {
        DirectoryInfo latestSession = new DirectoryInfo(@"Upload\").GetDirectories().OrderByDescending(d => d.LastWriteTimeUtc).First();
        public Form4()
        {
            InitializeComponent();
        }

        //proceed to next image after pressing pass / fail button
        public void incrementImage()
        {
            nCurrentItem++;
            firstImage++;

            if (nCurrentItem > nTotalNumber)
                nCurrentItem = nTotalNumber;

            else if (nCurrentItem < nTotalNumber)
                using (var bmpTemp = new Bitmap(ImageFilenames[nCurrentItem]))
                {
                    PicBox.Image = new Bitmap(bmpTemp);
                }

            imgCounter.Text = firstImage.ToString() + " / " + nTotalNumber.ToString();
        }
        

        //load images
        int nTotalNumber = 0;
        int nCurrentItem = 0;
        int firstImage = 1;
        List<string> ImageFilenames = new List<string>();
        string uploadDir = Path.Combine(@"Upload\");
        

        private void uploadBtn_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog open = new OpenFileDialog())
            {
                open.Multiselect = true;

                //set the initial directory to last modified folder
                open.InitialDirectory = latestSession.ToString();
                open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp;*.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
                if (open.ShowDialog() == DialogResult.OK)
                {
                    string sFileName = open.FileName;
                    ImageFilenames = open.FileNames.ToList();

                    using (var bmpTemp = new Bitmap(ImageFilenames[0]))
                    {
                        PicBox.Image = new Bitmap(bmpTemp);
                    }

                    //stretch images to the size of picbox
                    PicBox.SizeMode = PictureBoxSizeMode.StretchImage;
                }

            }
            if (ImageFilenames.Count > 0)
            {
                nTotalNumber = ImageFilenames.Count;
                ImageUploadCounter.Text = nTotalNumber.ToString();
                imgCounter.Text = firstImage.ToString() + " / " + nTotalNumber.ToString();

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form6 fm = new Form6();
            fm.Show();
            this.Hide();
        }

        private void PassBtn_Click(object sender, EventArgs e)
        {
            string passFolder = Path.Combine(latestSession.ToString(), "Pass");

            try
            {
                File.Move(ImageFilenames[nCurrentItem], Path.Combine(passFolder, ImageFilenames[nCurrentItem]), true);
                incrementImage();
            }
            catch(Exception ex) { }

        }
    }
}


What I have tried:

I have tried the bitmap and image disposed function also can't get the solution.
Posted
Updated 6-Oct-21 21:43pm
v2

We can't tell - it requires your code running on your system with your files to get any idea of what is actually happening.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

BTW: Please stop calling ToString so liberally - passFolder is declared as a string, so callign ToString on it is irrelevant and does nothing useful at all. Same thing for latestSession, except that may be the source of your problem, as DirectoryInfo.ToString doesn't return the same information as rthe FullName property does: DirectoryInfo.ToString Method (System.IO) | Microsoft Docs[^]
Calling ToString on strings makes your code look childish, calling it on other classes directly may not return exactly what you expect. FOr example, calling ToString on any collection returns the string name of that collection, not the content converted to a string ...
 
Share this answer
 
Do not do this:
C#
catch(Exception ex) { }

It means any exceptions are silently ignored so you have no idea if there is a problem in your code. Exceptions exist to help you, so make use of them.
 
Share this answer
 
Comments
Vektor 2021 7-Oct-21 3:48am    
I removed but still can't pass the images to the folder.
Richard MacCutchan 7-Oct-21 4:05am    
Then you need to run the code in your debugger. We cannot guess what happens when it runs, since it depends on the actual data that you are processing. And as OriginalGriff already mentioned, you need to get rid of all those calls to ToString where they are not required, or may even pass the wrong infomation. Microsoft even give you a warning in this case: DirectoryInfo.ToString Method (System.IO) | Microsoft Docs[^].

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