Page 1 of 1

Console: Change resolution of all images in multipage TIFF file.

Posted: Tue May 25, 2021 10:23 am
by Alex
Here is C# code snippet that allows to change resolution of all images in TIFF file and save changes to the source TIFF file:

Code: Select all

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // open file stream
            using (System.IO.FileStream fs = new System.IO.FileStream("multipage.tif", System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite))
            {
                // create image collection
                using (Vintasoft.Imaging.ImageCollection images = new Vintasoft.Imaging.ImageCollection())
                {
                    // add images from file stream to the image collection
                    images.Add(fs);

                    // for each image in image collection
                    for (int i = 0; i < images.Count; i++)
                    {
                        // change image resolution
                        images[i].Resolution = new Vintasoft.Imaging.Resolution(200, 200);
                    }

                    // create TIFF encoder
                    Vintasoft.Imaging.Codecs.Encoders.TiffEncoder encoder = new Vintasoft.Imaging.Codecs.Encoders.TiffEncoder();
                    // specify that TIFF encoder must save image collection to the source file and switch image collection to the saved file
                    encoder.SaveAndSwitchSource = true;

                    // save image collection to the source TIFF file
                    images.SaveSync(fs, encoder);
                }
            }
        }
    }
}