Someone could suggest me which properties i should use for It?tk
Loading
Someone could suggest me which properties i should use for It?tk
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Jaish MathewsRank #225 · 11K reputationPosted Dec 29, 2024, 3:27 AM EST
To save images from a folder into a list of image objects in C#, you can use the
System.Drawing.Imageclass or theSystem.Windows.Media.Imaging.BitmapImageclass, depending on your application's requirements (e.g., Windows Forms, WPF).Here's a step-by-step guide: This may not provide the exact solution but will serve as a helpful guide.
Steps:
Directory.GetFilesto get all image file paths in the folder.Code Example:
using System; using System.Collections.Generic; using System.Drawing; // For System.Drawing.Image using System.IO; class Program { static void Main() { string folderPath = @"C:\Your\Image\Folder"; // Specify your folder path here List imageList = LoadImagesFromFolder(folderPath); Console.WriteLine($"Loaded {imageList.Count} images."); } static List LoadImagesFromFolder(string folderPath) { List images = new List(); if (!Directory.Exists(folderPath)) { Console.WriteLine("Folder does not exist."); return images; } // Get all image file paths string[] imageFiles = Directory.GetFiles(folderPath, "*.*", SearchOption.TopDirectoryOnly) .Where(f => f.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".png", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".gif", StringComparison.OrdinalIgnoreCase)) .ToArray(); foreach (string file in imageFiles) { try { // Load image and add to list Image img = Image.FromFile(file); images.Add(img); } catch (Exception ex) { Console.WriteLine($"Error loading image {file}: {ex.Message}"); } } return images; } }Key Properties/Methods to Use:
Directory.GetFiles:Image.FromFile:Imageobject.List:Exception Handling:
Image.FromFilein atry-catchblock to handle cases where the file is not a valid image.Alternative for WPF (Using
BitmapImage):If you're working in a WPF application, use
System.Windows.Media.Imaging.BitmapImage:using System; using System.Collections.Generic; using System.IO; using System.Windows.Media.Imaging; class Program { static void Main() { string folderPath = @"C:\Your\Image\Folder"; List imageList = LoadImagesFromFolder(folderPath); Console.WriteLine($"Loaded {imageList.Count} images."); } static List LoadImagesFromFolder(string folderPath) { List images = new List(); if (!Directory.Exists(folderPath)) { Console.WriteLine("Folder does not exist."); return images; } string[] imageFiles = Directory.GetFiles(folderPath, "*.*", SearchOption.TopDirectoryOnly) .Where(f => f.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".png", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".gif", StringComparison.OrdinalIgnoreCase)) .ToArray(); foreach (string file in imageFiles) { try { BitmapImage img = new BitmapImage(new Uri(file, UriKind.Absolute)); images.Add(img); } catch (Exception ex) { Console.WriteLine($"Error loading image {file}: {ex.Message}"); } } return images; } }Tips:
System.Drawing.Imagefor Windows Forms or Console Applications.System.Windows.Media.Imaging.BitmapImagefor WPF applications.usingblocks or callDisposeonImageobjects when they're no longer needed.