Click here to Skip to main content
16,017,373 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
where as strin []bb = Directory.GetDirectories("c:\images\", "*.*", SearchOption.AllDirectories);

the problem is that it generate thumbnails but it ignore the folder strudcture, suppose
folder a->sub folder b->sub folder c,
so the result will be
folder a,
folder b,
folder c.
and it should be the same as the original folder .

<pre lang="cs">public void CreateThumbnail(string[] bb, double wid, double hght, bool Isprint)

        {

            
            string saveAt = "i:\\check\\a test\\";

            foreach (string path in bb)
            {
                var directory = new DirectoryInfo(path);

                string outputPath = Path.Combine(saveAt, directory.Name);
                foreach (FileInfo f in directory.GetFiles("*.*", SearchOption.AllDirectories))

                {
                    if (f.DirectoryName != directory.FullName)
                    {
                        outputPath = Path.Combine(saveAt, directory.Name, f.Directory.Name);

                    }
                    if (!Directory.Exists(outputPath))
                    {
                        Directory.CreateDirectory(outputPath);

                    }

                    using (Image imagesize = Image.FromFile(f.FullName))

                    using (Bitmap bitmapNew = new Bitmap(imagesize))

                    {
                        double maxWidth = wid;
                        double maxHeight = hght;
                        int w = imagesize.Width;

                        int h = imagesize.Height;
                        // Longest and shortest dimension
                        int longestDimension = (w > h) ? w : h;

                        int shortestDimension = (w < h) ? w : h;
                        // propotionality
                        float factor = ((float)longestDimension) / shortestDimension;

                        // default width is greater than height
                        double newWidth = maxWidth;
                        double newHeight = maxWidth / factor;

                        // if height greater than width recalculate
                        if (w < h)
                        {
                            newWidth = maxHeight / factor;

                            newHeight = maxHeight;
                        }

                        string fileName = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(f.Name) + ".jpeg");

                        bitmapNew.GetThumbnailImage((int)newWidth, (int)newHeight, () => false, IntPtr.Zero)

                            .Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);



                    }

                }
            }
        }

Posted
Comments
Eduard Keilholz 22-Mar-11 9:09am    
What is your question?
bachasafyan 22-Mar-11 9:17am    
the question that if i have suppose 3 subfolder in a folder a, and i generate thumbnails in another folder let say d. so the folder structure must remain the same in the d as well like, a must have 3 sub folder, but this doesnt happen, the 3 subfolder are showed as parent folder.

1 solution

For this problem, you should use a recursive method like this:

C#
public void ProcessDirectory(string directory)
{
    var files = Directory.GetFiles(directory);
    foreach (var fileName in files)
    {
        /* do the file processing with the filename */
    }
    var subDirectories = Directory.GetDirectories(directory);
    if (subDirectories.Length > 0)
    {
        foreach (var subDirectory in subDirectories)
        {
            ProcessDirectory(subDirectory);
        }
    }
}


and only get the files for the current directory (and cycle over all sub-directories).

Hope this helps.

Best regards,
Stops
 
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