public void LoadTypefaceFromDirectoryAsync(string typefaceDirectory, bool includeSubDirectories = true)
        {
            TypefaceCollection collection = null;

            BackgroundWorker worker = new BackgroundWorker();

            worker.DoWork += new DoWorkEventHandler(delegate(object sender, DoWorkEventArgs e)
            {
                collection = LoadTypefaceFromDirectory(typefaceDirectory, includeSubDirectories);
            });

            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object sender, RunWorkerCompletedEventArgs e)
            {
                OnLoadTypefacesFromDirectoryCompleted(collection);
            });

            worker.RunWorkerAsync();
        }
Пример #2
0
        /// <summary>
        /// Loads the typeface from directory.
        /// </summary>
        /// <param name="typefaceDirectory">The typeface directory.</param>
        /// <param name="includeSubDirectories">if set to <c>true</c> [include sub directories].</param>
        /// <returns></returns>
        /// <exception cref="ArgumentException">fontDirectory must be specified!</exception>
        /// <exception cref="DirectoryNotFoundException">Directory does not exist! " + typefaceDirectory</exception>
        public TypefaceCollection LoadTypefaceFromDirectory(string typefaceDirectory, bool includeSubDirectories = true)
        {
            if (string.IsNullOrEmpty(typefaceDirectory))
            {
                throw new ArgumentException("fontDirectory must be specified!");
            }

            if (!Directory.Exists(typefaceDirectory))
            {
                throw new DirectoryNotFoundException("Directory does not exist! " + typefaceDirectory);
            }

            TypefaceCollection endResult = new TypefaceCollection();

            SearchOption options = (includeSubDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

            string[] typefaceFiles = Directory.GetFiles(typefaceDirectory, "*.ttf", options);

            foreach (var typefaceFile in typefaceFiles)
            {
                try
                {
                    endResult.AddTypefaceToFile(typefaceFile);
                }
                catch (Exception)
                {
                }
            }

            typefaceFiles = Directory.GetFiles(typefaceDirectory, "*.otf", options);

            foreach (var typefaceFile in typefaceFiles)
            {
                try
                {
                    endResult.AddTypefaceToFile(typefaceFile);
                }
                catch (Exception)
                {
                }
            }

            return(endResult);
        }