Exemplo n.º 1
0
        /// <summary>
        /// Gets the list of all bitmaps from this image data object.
        /// Just one bitmap in this case.
        /// </summary>
        /// <returns>A BitmapSourceList containing the bitmap.</returns>
        public override BitmapSourceList GetBitmaps()
        {
            var bitmaps    = new BitmapSourceList();
            var locBbitmap = this.GetBitmap();

            if (locBbitmap != null)
            {
                bitmaps.Add(locBbitmap);
            }

            return(bitmaps);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Load and convert the file specified by <paramref name="fileName"/> to the format specified by <paramref name="targetExt"/>
        /// and/or create a bitmap of the file. The bitmap format is defined by <paramref name="bitmapExt"/>. The result will be stored
        /// in the directory given by <paramref name="targetDir"/>.
        /// </summary>
        /// <param name="fileName">The full qualified filename of the file to process.</param>
        /// <param name="targetDir">The directory where the results should be stored.</param>
        /// <param name="targetExt">The target format to which to convert the given file. If empty, no conversion takes place.</param>
        /// <param name="bitmapExt">If not empty a bitmap of the type specified by this extension is created from the files (image) content.</param>
        /// <param name="bitmapDir">Specifies an additional directory where a copy of the bitmap (if any) should be created.</param>
        /// <returns>A <see cref="bool"/> value indicating the success of the operation.</returns>
        public bool BatchProcess(string fileName, string targetDir, string targetExt, string bitmapExt, string bitmapDir)
        {
            if (string.IsNullOrEmpty(fileName) || !File.Exists(fileName))
            {
                return(false);
            }

            // load the file...
            try
            {
                // identify the loader on the basis of the extension...
                string          extension = Path.GetExtension(fileName);
                ISpecFileLoader loader    = AppContext.SpecFileLoaders.FindSupportingLoader(extension);
                if (loader == null)
                {
                    return(false);
                }

                // find the matching image writer
                IImagingWriter imageWriter = null;
                if (!string.IsNullOrEmpty(targetExt))
                {
                    foreach (var candidate in AppContext.ImagingWriters)
                    {
                        if (candidate != null)
                        {
                            bool found = false;
                            foreach (FileTypeDescriptor fileType in candidate.SupportedFileTypes)
                            {
                                if (fileType != null && fileType.IncludesExtension(targetExt))
                                {
                                    found = true;
                                    break;
                                }
                            }

                            if (found)
                            {
                                imageWriter = candidate;
                                break;
                            }
                        }
                    }
                }

                // find the matching bitmap writer
                IBitmapWriter bitmapWriter = null;
                if (!string.IsNullOrEmpty(bitmapExt))
                {
                    foreach (IBitmapWriter candidate in AppContext.BitmapWriters)
                    {
                        if (candidate != null)
                        {
                            bool found = false;
                            foreach (FileTypeDescriptor fileType in candidate.SupportedFileTypes)
                            {
                                if (fileType != null && fileType.IncludesExtension(bitmapExt))
                                {
                                    found = true;
                                    break;
                                }
                            }

                            if (found)
                            {
                                bitmapWriter = candidate as BitmapWriter;
                                break;
                            }
                        }
                    }
                }

                if (imageWriter == null && bitmapWriter == null)
                {
                    // no appropriate writers found for requested operation
                    return(false);
                }

                // let the loader load
                ISpecFileContent specFileContent = loader.Load(fileName);
                if (specFileContent == null)
                {
                    return(false);
                }

                BaseObjectList baseObjects = specFileContent.GetContent();
                if (baseObjects == null)
                {
                    return(false);
                }

                string outDir   = (!string.IsNullOrEmpty(targetDir) && Directory.Exists(targetDir)) ? targetDir : Path.GetDirectoryName(fileName);
                string filename = Path.GetFileNameWithoutExtension(fileName);

                // loop over the images an process
                foreach (BaseObject baseObject in baseObjects)
                {
                    var imaging = baseObject as Imaging;
                    if (imaging == null)
                    {
                        continue;
                    }

                    // perform image conversion if requested...
                    if (imageWriter != null)
                    {
                        // compose filename
                        string targetFile = filename + " - " + imaging.Name;
                        targetFile = Util.SubstitueInvalidPathChars(targetFile, '-');
                        targetFile = targetFile.Replace('.', '_');
                        targetFile = Path.Combine(outDir, targetFile + targetExt);
                        imageWriter.Write(imaging, targetFile, false);
                    }

                    // create a bitmap if required
                    if (bitmapWriter != null)
                    {
                        BitmapSourceList bitmaps = imaging.GetBitmaps();
                        if (bitmaps != null && bitmaps.Count > 0)
                        {
                            System.Windows.Media.Imaging.BitmapSource bitmap = bitmaps[0];
                            if (bitmap != null)
                            {
                                // compose filename
                                string bitmapFileName = filename + " - " + imaging.Name;
                                bitmapFileName = Util.SubstitueInvalidPathChars(bitmapFileName, '-');
                                bitmapFileName = bitmapFileName.Replace('.', '_');
                                string bitmapFilePath = Path.Combine(outDir, bitmapFileName + bitmapExt);
                                bitmapWriter.Write(bitmap, bitmapFilePath, false);
                                if (!string.IsNullOrEmpty(bitmapDir) && Directory.Exists(bitmapDir))
                                {
                                    bitmapFilePath = Path.Combine(bitmapDir, bitmapFileName + bitmapExt);
                                    bitmapWriter.Write(bitmap, bitmapFilePath, false);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Util.ReportException(e);
                return(false);
            }

            return(true);
        }