Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4083398
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:18:29+00:00 2026-05-20T18:18:29+00:00

i am trying to create thumbnails. my path to original folder is: suppose I:\my

  • 0

i am trying to create thumbnails. my path to original folder is: suppose I:\my images**, and i want to generate it to **i:\new images. i got two problem, first problem is that if my images folder contains subfolder then in the new images it should also be in the sub folder not as a parent folder .

  • second i got an Error.**A generic error occurred in GDI+.

    3rd i get this error: Out of memory.**

its a csharp console application

at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at ConsoleApplication1.Program.CreateThumbnail(String[] b, Double wid, Double hght, Boolean Isprint)

public void CreateThumbnail(string[] b, double wid, double hght, bool Isprint)
{
    string[] path;
    path = new string [64];
    path = b;
    string saveath = "i:\\check\\a test\\";
    for (int i = 0; i < b.Length; i++)
    {
        DirectoryInfo dir = new DirectoryInfo(path[i]);
        string dir1 = dir.ToString();
        dir1 = dir1.Substring(dir1.LastIndexOf("\\"));

        FileInfo[] files1 = dir.GetFiles();

        foreach (FileInfo f in files1)
        {
            string gh = f.ToString();
            try
            {
                System.Drawing.Image myThumbnail150;
                System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                System.Drawing.Image imagesize = System.Drawing.Image.FromFile(f.FullName);
                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;
                }
                myThumbnail150 = bitmapNew.GetThumbnailImage((int)newWidth, (int)newHeight, myCallback, IntPtr.Zero);

                string ext = Path.GetExtension(f.Name);

                if (!Directory.Exists(saveath + dir1))
                {
                    Directory.CreateDirectory(saveath + dir1);
                    myThumbnail150.Save(saveath + dir1 + "\\" + f.Name.Replace(ext, ".Jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                else if(Directory.Exists(saveath+dir1))
                {
                    myThumbnail150.Save(saveath + dir1+" \\"+ f.Name.Replace(ext, ".Jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("something went wrong" + ex.ToString());
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:18:29+00:00Added an answer on May 20, 2026 at 6:18 pm

    I have just refactored a bit the code and now it works (on my machine):

    private static void CreateThumbnail(string[] b, double wid, double hght, bool Isprint)
    {
        string saveAt = "D:\\check";
        foreach (string path in b)
        {
            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);
                }
            }
        }
    }
    

    I have to say few things about your old code:

    • use foreach when possible;
    • avoid the first three lines, they are useless;
    • avoid unused variables;
    • keep your code as clean as possible;
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a process to automatically generate thumbnails of the images uploaded
I am trying to create two thumbnails of an image uploaded by user. I
I am trying to load my computer folder images into a wall of thumbnails.
I'm trying to iterate through folders with images to create thumbnails using ImageMagic, and
Trying to create a new Dedicated Cache Role in Windows Azure but get the
I'm trying to load images from the specific folder in sd card.i saved images
I’m trying to create thumbnails for video files: - (UIImage*) thumbnailForVideoAtURL: (NSURL*) videoURL {
I am trying to upload various images into a dynamically created folder on my
I'm trying for some time to create thumbnails from video without success. In fact
I'm trying to generate thumbnails of several pages that will be displayed through a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.