/// <summary> /// Adds a new item to the image browser item collection. /// </summary> /// <param name="oBrowserItem">Specifies the item to add to the end of the collection.</param> /// <param name="bAddThumbNail">Specifies whether to create and add a thumbnail to the image list.</param> /// <exception cref="System.ArgumentNullException"></exception> /// <exception cref="System.ArgumentException"></exception> /// <exception cref="System.IO.FileNotFoundException"></exception> public void Add(ImageBrowserItem oBrowserItem, bool bAddThumbNail) { try { if (oBrowserItem == null) throw new ArgumentNullException("oBrowserItem", "The specified parameter cannot be null."); if (m_nFileCount < m_nMaxFiles) { string sDimensions = string.Empty; if (bAddThumbNail) { using (Image oImage = Image.FromFile(oBrowserItem.Path)) { // Make sure the image list gets initialized. if (!m_bThumbNailsInitialized) InitializeImageList(); // Add the thumbnail to the image list. m_oImageList.Images.Add(oBrowserItem.Path, DrawingTools.CreateThumbnail(oImage, m_oImageList.ImageSize, m_bHighQualityThumbnails, m_nThumbnailEffect)); // Append tooltip data. sDimensions = "\nDimensions: " + oImage.Width + " x " + oImage.Height + " Pixels"; } } // Create a new listview item. ListViewItem oItem = new ListViewItem(oBrowserItem.Name, bAddThumbNail ? m_nFileCount : 0); oItem.Name = oBrowserItem.Path; oItem.ToolTipText = oBrowserItem.Name + "\nType: " + oBrowserItem.FileType + "\nSize: " + oBrowserItem.Length + sDimensions; // Add subitems only if thumbnails are not specified. if (!bAddThumbNail) { oItem.SubItems.AddRange(new string[] { oBrowserItem.Created, oBrowserItem.FileType, oBrowserItem.Length }); } // Add the listview item to the list. m_oListViewItems.Add(oItem); // Add the image file path to the file path list. m_sFilePaths.Add(oBrowserItem.Path); // Increase the file counter. ++m_nFileCount; // Fire the item added event. OnItemAdded(this, new ImageBrowserItemEventArgs(m_nFileCount, m_nFileCount - 1, oBrowserItem.Path)); } } catch (OutOfMemoryException) { MessageBox.Show("Error trying to load image. (" + oBrowserItem.Path + ")\n\nThe contents of this file could be damaged or corrupted.", "Load Image Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (ArgumentNullException e) { throw new ArgumentNullException(e.Message, e); } catch (ArgumentException e) { throw new ArgumentException(e.Message, e); } catch (FileNotFoundException e) { throw new FileNotFoundException(e.Message, e); } }
/// <summary> /// Loads all of the image files with the specified extensions from the specified directory. /// </summary> /// <param name="sDirectory">Specifies the directory to load.</param> /// <param name="nType">Specifies the image types to load.</param> /// <param name="bLoadThumbNails">Specifies whether to load the thumbnail images.</param> /// <exception cref="System.ArgumentNullException"></exception> /// <exception cref="System.ArgumentException"></exception> /// <exception cref="System.UnauthorizedAccessException"></exception> /// <exception cref="System.IO.IOException"></exception> /// <exception cref="System.Security.SecurityException"></exception> public void LoadImageFiles(string sDirectory, ImageFileType nType, bool bLoadThumbNails) { if (string.IsNullOrEmpty(sDirectory)) return; try { // Fire the load started event. OnLoadStarted(this, new ImageBrowserEventArgs()); DirectoryInfo oDirectory = new DirectoryInfo(sDirectory); if (oDirectory.Exists) { // Update the selected directory path. m_sSelectedDirectoryPath = sDirectory; // Initialize the image list if specified. if (bLoadThumbNails) InitializeImageList(); bool bBreak = false; string[] sExtensions = FileTools.GetFileExtensions(nType); ImageBrowserItem oItem = new ImageBrowserItem(); var allfiles = oDirectory.GetFiles().OrderBy(p=>p.Name); foreach (FileInfo oFile in allfiles) { foreach (string sExtension in sExtensions) { if ((oFile.Length <= m_lMaxFileLength) && (sExtension.Equals(oFile.Extension, StringComparison.OrdinalIgnoreCase))) { oItem.Name = Path.GetFileNameWithoutExtension(oFile.Name); oItem.Path = oFile.FullName; oItem.FileType = FileTools.GetFileExtensionDescription(sExtension); oItem.Created = oFile.CreationTime.ToShortDateString() + " - " + oFile.CreationTime.ToShortTimeString(); oItem.Length = FileTools.ConvertBytes(oFile.Length); // Add the item to the list. Add(oItem, bLoadThumbNails); // Break from the inner loop if the max file count has been reached. if (m_nFileCount >= m_nMaxFiles) { bBreak = true; break; } } } // Break from outer loop, if specified. if (bBreak) break; } } // Fire the load finished event. OnLoadFinished(this, new ImageBrowserEventArgs()); } catch (ArgumentNullException e) { throw new ArgumentNullException(e.Message, e); } catch (ArgumentException e) { throw new ArgumentException(e.Message, e); } catch (UnauthorizedAccessException e) { throw new UnauthorizedAccessException(e.Message, e); } catch (IOException e) { throw new IOException(e.Message, e); } catch (System.Security.SecurityException e) { throw new System.Security.SecurityException(e.Message, e); } }