コード例 #1
0
        /// <summary>
        /// Returns the first image from the specified location
        /// </summary>
        /// <param name="sPath">The location to find an image</param>
        /// <param name="DisplayErrors">Whether to show any error messages</param>
        /// <returns></returns>
        public static Bitmap LoadImage(string sPath, bool DisplayErrors = true)
        {
            Bitmap bmp = null;

            if (Directory.Exists(sPath))
            {
                string[] sFiles = new string[0];
                if ((sFiles = Ext.GetFiles(sPath, SearchOption.TopDirectoryOnly)).Length > 0)
                {
                    sPath = sFiles[0];
                }
            }

            if (Ext.Accessible(sPath, Permissions: FileIOPermissionAccess.Read) == Ext.PathType.ValidFile)
            {
                if (Ext.IsArchive(sPath))
                {
                    using (SCA.IArchive scArch = SCA.ArchiveFactory.Open(sPath)) {
                        if (!Ext.IsArchiveAccessible(scArch))
                        {
                            return(bmp);
                        }

                        int iCount = scArch.Entries.Count();
                        if (iCount > 0)
                        {
                            //account for terrible default zip-sorting
                            int iFirst = 0;
                            SCA.IArchiveEntry[] scEntries = scArch.Entries.ToArray();

                            List <string> lEntries = new List <string>(iCount);
                            for (int i = 0; i < iCount; i++)
                            {
                                if (Ext.ImageTypes.Contains(Path.GetExtension(scEntries[i].Key).ToLower()))
                                {
                                    lEntries.Add(scEntries[i].Key);
                                }
                            }
                            if (lEntries.Count > 0)
                            {
                                lEntries.Sort(new TrueCompare());
                                for (; iFirst < iCount; iFirst++)
                                {
                                    if (scEntries[iFirst].Key.Length == lEntries[0].Length)
                                    {
                                        if (scEntries[iFirst].Key.Equals(lEntries[0]))
                                        {
                                            break;
                                        }
                                    }
                                }

                                try {
                                    using (Stream ms = scEntries[iFirst].OpenEntryStream()) {
                                        bmp = new Bitmap(ms);
                                    }
                                } catch (Exception exc) {
                                    SQL.LogMessage(exc, SQL.EventType.HandledException, sPath + "\\" + scEntries[iFirst].Key);

                                    if (DisplayErrors)
                                    {
                                        xMessage.ShowError("The following file could not be loaded:\n" + scEntries[iFirst].Key);
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    try {
                        bmp = new Bitmap(sPath);
                    } catch (Exception exc) {
                        SQL.LogMessage(exc, SQL.EventType.HandledException, sPath);

                        if (DisplayErrors)
                        {
                            xMessage.ShowError("The following file could not be loaded:\n" + sPath);
                        }
                    }
                }
            }

            return(bmp);
        }