Example #1
0
        public async Task ProcessFilesAsync(IEnumerable <int> previewDelta, Func <Stream, string, Task> pagePreviewCallback, CancellationToken token)
        {
            var jpgCreateOptions = new JpegOptions();

            var diff = Enumerable.Range(0, _image.Frames.Length);

            diff = diff.Except(previewDelta);

            var t = new List <Task>();

            foreach (var item in diff)
            {
                if (token.IsCancellationRequested)
                {
                    break;
                }
                _image.ActiveFrame = _image.Frames[item];// tiffFrame;
                var pixels = _image.LoadPixels(_image.Bounds);
                //bmpCreateOptions as FileCreateSource saved
                var ms = new MemoryStream();
                jpgCreateOptions.Source = new StreamSource(ms);
                using (var jpgImage =
                           (JpegImage)Image.Create(jpgCreateOptions, _image.Width, _image.Height))
                {
                    //TiffFrame
                    jpgImage.SavePixels(_image.Bounds, pixels);
                    jpgImage.Save();
                }

                var task = pagePreviewCallback(ms, $"{item}.jpg").ContinueWith(_ => ms.Dispose(), token);
                t.Add(task);
            }
            await Task.WhenAll(t);
        }
Example #2
0
        public static void Run()
        {
            Console.WriteLine("Running example ExtractTIFFFramesToBMPImageFormat");
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_ModifyingAndConvertingImages();

            using (TiffImage multiImage = (TiffImage)Image.Load(dataDir + "SampleTiff1.tiff"))
            {
                // Create an instance of int to keep track of frames in TiffImage
                int frameCounter = 0;

                // Iterate over the TiffFrames in TiffImage
                foreach (TiffFrame tiffFrame in multiImage.Frames)
                {
                    multiImage.ActiveFrame = tiffFrame;

                    // Load Pixels of TiffFrame into an array of Colors
                    Color[] pixels = multiImage.LoadPixels(tiffFrame.Bounds);

                    // Create an instance of bmpCreateOptions
                    BmpOptions bmpCreateOptions = new BmpOptions();
                    bmpCreateOptions.BitsPerPixel = 24;

                    // Set the Source of bmpCreateOptions as FileCreateSource by specifying the location where output will be saved
                    bmpCreateOptions.Source = new FileCreateSource(string.Format("{0}\\ConcatExtractTIFFFramesToBMP_out{1}.bmp", dataDir, frameCounter), false);

                    // Create a new bmpImage
                    using (BmpImage bmpImage = (BmpImage)Image.Create(bmpCreateOptions, tiffFrame.Width, tiffFrame.Height))
                    {
                        // Save the bmpImage with pixels from TiffFrame
                        bmpImage.SavePixels(tiffFrame.Bounds, pixels);
                        bmpImage.Save();
                    }
                    frameCounter++;
                }
            }

            Console.WriteLine("Finished example ExtractTIFFFramesToBMPImageFormat");
        }
        ///<Summary>
        /// ConvertImageFormat method to convert image to different image formats
        ///</Summary>
        public Response ConvertImageFormat(string fileName, string folderName, string outputType)
        {
            if (outputType.Equals("gif") || outputType.Equals("bmp") || outputType.Equals("jpg") || outputType.Equals("png") ||
                outputType.Equals("psd") || outputType.Equals("emf") || outputType.Equals("svg") || outputType.Equals("wmf"))
            {
                ImageOptionsBase optionsBase = new BmpOptions();

                if (outputType.Equals("jpg"))
                {
                    optionsBase = new JpegOptions();
                }
                else if (outputType.Equals("png"))
                {
                    optionsBase = new PngOptions();
                }
                else if (outputType.Equals("gif"))
                {
                    optionsBase = new GifOptions();
                }
                else if (outputType.Equals("psd"))
                {
                    optionsBase = new PsdOptions();
                }
                else if (outputType.Equals("emf"))
                {
                    optionsBase = new EmfOptions();
                }
                else if (outputType.Equals("svg"))
                {
                    optionsBase = new SvgOptions();
                }
                else if (outputType.Equals("wmf"))
                {
                    optionsBase = new WmfOptions();
                }
                return(ProcessTask(fileName, folderName, "." + outputType, true, true, delegate(string inFilePath, string outPath, string zipOutFolder)
                {
                    string fileExtension = Path.GetExtension(inFilePath).ToLower();
                    if ((fileExtension == ".tif") || (fileExtension == ".tiff"))
                    {
                        string outfileName = Path.GetFileNameWithoutExtension(fileName) + "_{0}";
                        using (TiffImage multiImage = (TiffImage)Image.Load(inFilePath))
                        {
                            if (multiImage.Frames.Length > 1)
                            {
                                int frameCounter = 0;
                                // Iterate over the TiffFrames in TiffImage
                                foreach (TiffFrame tiffFrame in multiImage.Frames)
                                {
                                    multiImage.ActiveFrame = tiffFrame;
                                    // Load Pixels of TiffFrame into an array of Colors
                                    Color[] pixels = multiImage.LoadPixels(tiffFrame.Bounds);
                                    // Create an instance of JpegOptions

                                    // Set the Source of JpegOptions as FileCreateSource by specifying the location where output will be saved
                                    // Last boolean parameter denotes isTemporal
                                    outPath = zipOutFolder + "/" + outfileName;

                                    optionsBase.Source = new FileCreateSource(string.Format(outPath, frameCounter + 1) + "." + outputType, false);
                                    // Create a new RasterImage of Jpeg type
                                    using (var jpgImage = (RasterImage)Image.Create(optionsBase, tiffFrame.Width, tiffFrame.Height))
                                    {
                                        // Save the JpegImage with pixels from TiffFrame
                                        jpgImage.SavePixels(tiffFrame.Bounds, pixels);
                                        // Resize the Jpeg Image
                                        jpgImage.Resize(100, 100, ResizeType.NearestNeighbourResample);
                                        // Save the results on disk
                                        jpgImage.Save();
                                    }
                                    frameCounter++;
                                }
                            }
                            else
                            {
                                multiImage.Save(outPath, optionsBase);
                            }
                        }
                    }
                    else

                    {
                        using (Image image = Image.Load(inFilePath))
                        {
                            image.Save(outPath, optionsBase);
                        }
                    }
                }));
            }

            return(new Response
            {
                FileName = null,
                Status = "Output type not found",
                StatusCode = 500
            });
        }