I select Multiple images from file and show their thumbnails in the panel. here is code i write for this::

//files is string type array Containing the image Location
//left, top, picWidth, picHeight are int variables.

           foreach(string file in files)
           {
                   Bitmap bit = new Bitmap(file);
                   ratio = (double)bit.Width / (double)picWidth;
                   PictureBox picBox = new PictureBox();
                   picBox.Name = file;

                   if (left + picWidth > this.Panel2.Width)
                   {
                       left = 10;
                       top += picHeight + 10;
                   }
                 
                   picBox.Size = new Size(picWidth, picHeight);
                  
picBox.Image = bit.GetThumbnailImage(picWidth,(int)((double) bit.Height / ratio), null, System.IntPtr.Zero);
                   picBox.SizeMode = PictureBoxSizeMode.Zoom;
                   
                   picBox.Location = new Point(left, top);
                   this.Panel2.Controls.Add(picBox);
                   picBox.BorderStyle = BorderStyle.FixedSingle;
                   
                   Application.DoEvents();
                   left += picWidth + 10;                          
            }

Sir, My Question is that how can i enhance the speed of my code. Is there any way that i use the treads and it increase the speed of making and showing of thumbnails. or any other way??
i am very thankful to you in advnace..

Dani AI

Generated

For : the biggest wins are not just “more threads” but doing the CPU/IO work off the UI thread, bounding how many images you process at once, avoiding file locks, and disposing images correctly. pointed in the right direction — below is a safe, practical pattern that keeps the UI responsive, avoids excessive memory use, and produces thumbnails quickly.

Do the heavy work on background threads (read file, decode, resize) but only touch WinForms controls on the UI thread. Limit concurrency (SemaphoreSlim or ParallelOptions.MaxDegreeOfParallelism) so you do not thrash the disk or run out of memory. Avoid Image.FromFile (it locks files). Always Dispose source images and any previous PictureBox.Image you replace. For many thumbnails consider virtualization (virtual ListView/owner-draw + on-demand load) or a disk cache keyed by path+lastWriteTime so thumbnails are reused.

Example (modern, safe approach — creates thumbnail bytes in background, marshals only final UI creation to the UI thread):

private async Task LoadThumbnailsAsync(string[] files, int w, int h, Control host)
{
    var sem = new SemaphoreSlim(Math.Max(1, Environment.ProcessorCount - 1));
    var tasks = files.Select(async path =>
    {
        await sem.WaitAsync();
        try
        {
            byte[] thumbBytes;
            using (var fs = File.OpenRead(path))
            using (var src = Image.FromStream(fs))
            using (var thumb = new Bitmap(w, h))
            using (var g = Graphics.FromImage(thumb))
            {
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(src, 0, 0, w, h);
                using (var ms = new MemoryStream())
                {
                    thumb.Save(ms, ImageFormat.Jpeg);
                    thumbBytes = ms.ToArray();
                }
            }
            host.Invoke((Action)(() =>
            {
                var pb = new PictureBox { Width = w, Height = h, SizeMode = PictureBoxSizeMode.Zoom };
                pb.Image = Image.FromStream(new MemoryStream(thumbBytes));
                host.Controls.Add(pb);
            }));
        }
        finally { sem.Release(); }
    }).ToArray();
    await Task.WhenAll(tasks);
}

Troubleshooting tips: remove Application.DoEvents(); it masks reentrancy and is rarely needed when you use true background work. If you still see UI stalls, reduce MaxDegreeOfParallelism to 1–2. When removing picture boxes, Dispose their Image first to free GDI handles. If many images are shown, switch from many PictureBox controls to a virtualized view for far better memory and speed.

Recommended Answers

All 3 Replies

Yes .. just move the code you have off in to a thread and away you go. You would want to use the thread pool and and feed file names to your thread starter since you have a fixed set of logic to run on each file.

Yes .. just move the code you have off in to a thread and away you go. You would want to use the thread pool and and feed file names to your thread starter since you have a fixed set of logic to run on each file.

Sknake, I have no idea about thread pool. can you give some explanation and give some code snapshoot.

Here is an example application I did that simulates a long database read in another thread while not blocking the main thread. You can use this concept and get 3 threads started that process images while the main thread kicks back and waits for them to wrap up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.