How can i create many directories according to the file resolution and the numericUpDown value? [closed]

StackOverflow https://stackoverflow.com/questions/23634941

  •  21-07-2023
  •  | 
  •  

Question

In this case each time i change the numericUpDown value it will create a new directory. The numericUpDoown set to minimum 10 and maximum 502. In this case the file i'm using is 512x512 resolution.

What i want to do is that if the numericUpdown for example is set to value 10 and i will click on a confirm button it will create many directories each directory name should be like this:

SecondProcess_5132014-559-502x502

Next directory will be created will have this name:

SecondProcess_5132014-549-492x492

Next directory

SecondProcess_5132014-539-482x482

And if the value in the numericUpDown is now 20 the the first directory will be:

SecondProcess_5132014-559-492x492

Then

SecondProcess_5132014-559-472x472

So i have now this code but i want to create all this directories automatic:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            numeric = (int)numericUpDown1.Value;
            path = Path.Combine(@"c:\temp\newimages",
    String.Concat("SecondProcess_", DateTime.Now.ToString("MMddyyyy-HHmmss")));
                Directory.CreateDirectory(path);            
        }

        private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bgw = (BackgroundWorker)sender;
            if (bgw.CancellationPending == true)
            {
            }
            else
            {
                Image img;
                DirectoryInfo di1;
                FileInfo[] fi1;
                for (int i = 0; i < myGifList.Count; i++)
                {
                    Image image = Image.FromFile(myGifList[i]);
                    int imageWidth = image.Width;
                    int imageHeight = image.Height;
                    img = resizeImage(imageWidth - numeric, imageHeight - numeric, myGifList[i]);
                    img.Save(path + "\\" + Path.GetFileName(myGifList[i]), System.Drawing.Imaging.ImageFormat.Gif);
                }
                di1 = new DirectoryInfo(path);
                fi1 = di1.GetFiles("*.gif");
                List<string> newImages = new List<string>();
                for (int i = 0; i < fi1.Length; i++)
                {
                    newImages.Add(fi1[i].FullName);
                }
                unfreez.MakeGIF(newImages, path + "\\" + "animated.gif", 8, true);
            }
        }

        private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

        private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            pictureBoxImage(path + "\\" + "animated.gif");
            label10.Text = FileSize(path + "\\" + "animated.gif");
            label12.Text = ImageResolution(path + "\\" + "animated.gif");
            label7.Visible = true;
            label7.ForeColor = Color.Green;
            label7.Text = "Operation Have Been Completed";
            button1.Enabled = false;
            progressBar2.EndColor = Color.FromArgb(0, 211, 040);
            numericUpDown1.Enabled = true;
            button3.Enabled = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            numericUpDown1.Enabled = false;
            button3.Enabled = false;
            backgroundWorker2.RunWorkerAsync();
        }

What it does is each time i change the numericUpDown value if it's 10 and i click the button it will create a directory and put there all images and the animated gif i created. If i change the numericupdown value to 20 and click the button it will create another directory with images and animated gif.

But i want is to have another button if i will click on him and the vlaue is 10 so automatic it will create the number of directories according to the file resolution with the backgroundworker.

How can i do it ?

EDIT

I tried to do it in this button click event:

private void button4_Click(object sender, EventArgs e)
        {
            Image img = Image.FromFile(previewFileName);
            int width = img.Width;
            int height = img.Height;
            int dirsnumbers = width / numeric;
            for (int i = 0; i < dirsnumbers; i++)
            {
                path = Path.Combine(@"c:\temp\newimages",
        String.Concat("SecondProcess_", DateTime.Now.ToString("MMddyyyy-HHmmss")) + 
                     "-" + "Width = " + (width - numeric) + " Height = " + (height - numeric) );
                Directory.CreateDirectory(path);
            }
        }

In this example numeric = 10 and width = 512 So it supposed to create 51 directories but instead it created only one directory.

Another problem is that 512/10 = 51.2 i can't create 51.2 directories so how do i check if it's 51.2 or 51.3 or 51.9 to tell it to create only 51 directories ?

EDIT**

private void button4_Click(object sender, EventArgs e)
        {
            Image img = Image.FromFile(previewFileName);
            int width = img.Width;
            int height = img.Height;
            double res = width / numeric;
            int dirsnumbers = (int)Math.Floor(res);
            for (int i = 0; i <= dirsnumbers; i++)
            {
                width = width - numeric;
                height = height - numeric;
                path = Path.Combine(@"c:\temp\newimages",
        String.Concat("SecondProcess_", DateTime.Now.ToString("MMddyyyy-HHmmss")) +
                     "-" + "Width = " + (width - numeric) + " Height = " + (height - numeric));
                if (width > 0 && height > 0)
                {
                    Directory.CreateDirectory(path);
                }
            }
        }

What i did is added in the loop this two lines:

width = width - numeric;
height = height - numeric;

But for some reason i don't understand why all the directories that created are ok but only one is strange:

SecondProcess_05132014-193441-Width = -8 Height = -8

I can't figure out where this minus 8 are coming from. They are between two other directories:

SecondProcess_05132014-193441-Width = 2 Height = 2 and SecondProcess_05132014-193441-Width = 12 Height = 12

Maybe this -8 are got something with this line:

int dirsnumbers = (int)Math.Floor(res);

With the Math.Floor ?

Was it helpful?

Solution

From your last 2 lines of your question that is -

Another problem is that 512/10 = 51.2 i can't create 51.2 directories so how do i check if it's 51.2 or 51.3 or 51.9 to tell it to create only 51 directories ?

For that you have to use - Math.Floor()

I found Rest of the thing is correct and so now your code look like this - You can check it in Dotnet Fiddle also.

private void button4_Click(object sender, EventArgs e)
{
            Image img = Image.FromFile(previewFileName);
            int width = img.Width;
            int height = img.Height;
            double res = width / numeric;
            int dirsnumbers = (int)Math.Floor(res);
            for (int i = 0; i <= dirsnumbers; i++)
        {
            path = Path.Combine(@"c:\temp\newimages",
            String.Concat("SecondProcess_", DateTime.Now.ToString("MMddyyyy-HHmmss")) +
                 "-" + "Width = " + (width - numeric) + " Height = " + (height - numeric));
            if (width > 0 && height > 0)
            {
                Directory.CreateDirectory(path);
            }

            width = width - numeric;
            height = height - numeric;
        }
}

OTHER TIPS

in another thread (background worker) loop over a mkdir with a formatted string with the appropriate name.

mkdir("./some/path/string");

You have access to the file resolution, so you can regex that information, and make an int from it. From there you can find which is the max of the two resolutions. Then loop over the max and decrement both, creating a new directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top