Saving files with original settings

Questions, comments and suggestions concerning VintaSoft Imaging .NET SDK.

Moderator: Alex

Post Reply
DanielLW
Posts: 22
Joined: Tue Sep 01, 2015 2:42 pm

Saving files with original settings

Post by DanielLW »

Hello,

is it possible to save images previously loaded into an ImageCollection (for example calling ImageViewer.Images.Add) to another file using the original image settings?

For example, I've loaded a jpeg file into the ImageViewer and rotated it. Now i want to save the jpeg file to another file name using the original quality and subsampling values. Can you explain how this can be accomplished?

Thank you.

Best regards,
Daniel
Yuri
Posts: 64
Joined: Wed Jul 23, 2008 2:47 pm

Re: Saving files with original settings

Post by Yuri »

Hi Daniel,

In current version is not possible to get and set quality params and subsampling for JPEG images. The functionality will be available in coming version 8.3, expected until the end of this year.

--
Sincerely,
Yuri
DanielLW
Posts: 22
Joined: Tue Sep 01, 2015 2:42 pm

Re: Saving files with original settings

Post by DanielLW »

Hello Yuri,

thank you for your answer. JPEG was just an example, my question targets the saving process in general. Another example, i have loaded a TIFF-file, changed something (rotation), and want to save it using the original image settings, like compression, color depth, and so on. Is there a (simple) method to do this?
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: Saving files with original settings

Post by Alex »

Hello Daniel,

Here is an example that shows how to load image from TIFF file, rotate image and save it back with "original" encoding settings:

Code: Select all

/// <summary>
/// Loads image(s) from source TIFF file,
/// rotates the first image,
/// saves image(s) to a new TIFF file with encoding settings from source TIFF file.
/// </summary>
/// <param name="inFilePath">Path to a source TIFF file.</param>
/// <param name="outFilePath">Path to a destination TIFF file.</param>
static void ChangeAndSaveTiffFile(string inFilePath, string outFilePath)
{
    // create image collection
    using (ImageCollection images = new ImageCollection())
    {
        // add images from TIFF file to the image collection
        images.Add(inFilePath, false);

        // rotate the first image
        images[0].Rotate(90);

        // create TIFF encoder
        using (TiffEncoder encoder = new TiffEncoder())
        {
            // if source and destination paths are the same
            if (inFilePath == outFilePath)
                // specify that image collection must change source after saving
                encoder.SaveAndSwitchSource = true;

            // subscribe to the image saving event
            encoder.ImageSaving += new EventHandler<ImageSavingEventArgs>(EncoderImageSaving);
            // save images synchronously
            images.SaveSync(outFilePath, encoder);
            // unsubscribe from the image saving event
            encoder.ImageSaving -= EncoderImageSaving;
        }

        // clear image collection and dispose images
        images.ClearAndDisposeItems();
    }
}

/// <summary>
/// Handler of the image saving event.
/// </summary>
static void EncoderImageSaving(object sender, ImageSavingEventArgs e)
{
    // get TIFF encoder
    TiffEncoder encoder = (TiffEncoder)sender;
    // get TIFF encoder settings from source TIFF image
    encoder.Settings = GetTiffEncoderSettings(e.Image.Metadata);
}

/// <summary>
/// Gets the TIFF encoder settings from metadata of TIFF image.
/// </summary>
/// <param name="metadata">The metadata.</param>
static TiffEncoderSettings GetTiffEncoderSettings(VintasoftImageMetadata metadata)
{
    // create TIFF encoder settings
    TiffEncoderSettings settings = new TiffEncoderSettings();
    // get page metadata of TIFF file
    TiffPageMetadata tree = metadata.MetadataTree as TiffPageMetadata;
    // if metadata is TIFF image metadata
    if (tree != null)
    {
        // get compression of TIFF file
        settings.Compression = tree.Compression;
        settings.UseTiles = false;
        settings.UseStrips = false;
        // find RowsPerStrip tag of TIFF file
        TiffTagMetadata rowsPerStripTag = tree.FindChildNode<TiffTagMetadata>("RowsPerStrip");
        // if tag exists (TIFF image data are stored in strips)
        if (rowsPerStripTag != null)
        {
            // specify that encoder must use strips
            settings.UseStrips = true;
            // set row count in a strip of TIFF file
            settings.RowsPerStrip = Convert.ToInt32(rowsPerStripTag.Value);
        }
        // if tag does NOT exist (TIFF image data are stored in tiles)
        else
        {
            // find TileWidth tag of TIFF file
            TiffTagMetadata tileWidthTag = tree.FindChildNode<TiffTagMetadata>("TileWidth");
            // if tag exists
            if (tileWidthTag != null)
            {
                // find TileLength tag of TIFF file
                TiffTagMetadata tileLengthTag = tree.FindChildNode<TiffTagMetadata>("TileLength");

                // get tile width and height of TIFF file
                int tileWidth = Convert.ToInt32(tileWidthTag.Value);
                int tileHeigth = Convert.ToInt32(tileLengthTag.Value);

                // specify that encoder must use tiles
                settings.UseTiles = true;
                // set size of tile of TIFF file
                settings.TileSize = new Size(tileWidth, tileHeigth);
            }
        }
    }
    return settings;
}
Best regards, Alexander
DanielLW
Posts: 22
Joined: Tue Sep 01, 2015 2:42 pm

Re: Saving files with original settings

Post by DanielLW »

This works, thank you for the example!
DanielLW
Posts: 22
Joined: Tue Sep 01, 2015 2:42 pm

Re: Saving files with original settings

Post by DanielLW »

Hello again,

can you please give me a hint, how this can be accomplished for pdf files? The PdfPageMetaData-class does not contain properties to retrieve the image compression or something like that.
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: Saving files with original settings

Post by Alex »

Hello Daniel,
can you please give me a hint, how this can be accomplished for pdf files? The PdfPageMetaData-class does not contain properties to retrieve the image compression or something like that.
Do you want just rotate image (90, 180 or 270 degrees) and save OR apply custom processing to image and save?

Best regards, Alexander
DanielLW
Posts: 22
Joined: Tue Sep 01, 2015 2:42 pm

Re: Saving files with original settings

Post by DanielLW »

Hello Alex,

both. The user should be able to rotate or do some image processing (change brightness/contrast for example) and save the changes afterwards.

Thank you.
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: Saving files with original settings

Post by Alex »

Hello Daniel,

Here is an example that shows how to render page of PDF document, invert rendered image and save it back with "original" encoding settings:

Code: Select all

/// <summary>
/// Renders page of surce PDF document,
/// inverts the first image,
/// saves image(s) to a new PDF file with encoding settings from source PDF document.
/// </summary>
/// <param name="inFilePath">Path to a source PDF file.</param>
/// <param name="outFilePath">Path to a destination PDF file.</param>
public static void ChangeAndSavePdfFile(string inFilePath, string outFilePath)
{
    // create image collection
    using (ImageCollection images = new ImageCollection())
    {
        // add images from file to the image collection
        images.Add(inFilePath, false);

        // invert the first image
        images[0].Invert();

        // create PDF encoder
        using (PdfEncoder encoder = new PdfEncoder())
        {
            // if source and destination paths are the same
            if (inFilePath == outFilePath)
                // specify that image collection must change source after saving
                encoder.SaveAndSwitchSource = true;

            // subscribe to the image saving event
            encoder.ImageSaving += new EventHandler<ImageSavingEventArgs>(EncoderImageSaving);
            // save images synchronously
            images.SaveSync(outFilePath, encoder);
            // unsubscribe from the image saving event
            encoder.ImageSaving -= EncoderImageSaving;
        }

        // clear image collection and dispose images
        images.ClearAndDisposeItems();
    }
}

/// <summary>
/// Handler of the image saving event.
/// </summary>
static void EncoderImageSaving(object sender, ImageSavingEventArgs e)
{
    // get PDF encoder
    PdfEncoder encoder = (PdfEncoder)sender;
    // get TIFF encoder settings from source TIFF image
    encoder.Settings = GetPdfEncoderSettings(e.Image);
}

/// <summary>
/// Gets the PDF encoder settings from PDF image.
/// </summary>
/// <param name="image">The image.</param>
static PdfEncoderSettings GetPdfEncoderSettings(VintasoftImage image)
{
    // create PDF encoder settings
    PdfEncoderSettings settings = new PdfEncoderSettings();
    settings.Compression = PdfImageCompression.Auto;

    // set document updating mode to "Incremental"
    settings.UpdateMode = PdfDocumentUpdateMode.Incremental;

    //// set document updating mode to "Pack"
    //settings.UpdateMode = PdfDocumentUpdateMode.Pack;

    // get PDF page associated with image
    PdfPage page = PdfDocumentController.GetPageAssociatedWithImage(image);

    // if PDF page is image only
    if (page.IsImageOnly)
    {
        // select image compression
        if (page.BackgroundImage.Compression == PdfCompression.None)
            settings.Compression = PdfImageCompression.None;
        else if ((page.BackgroundImage.Compression & PdfCompression.CcittFax) != 0)
            settings.Compression = PdfImageCompression.CcittFax;
        else if ((page.BackgroundImage.Compression & PdfCompression.Jpeg) != 0)
            settings.Compression = PdfImageCompression.Jpeg;
        else if ((page.BackgroundImage.Compression & PdfCompression.Zip) != 0)
            settings.Compression = PdfImageCompression.Zip;
        else if ((page.BackgroundImage.Compression & PdfCompression.Jpeg2000) != 0)
            settings.Compression = PdfImageCompression.Jpeg2000;
        else if ((page.BackgroundImage.Compression & PdfCompression.Lzw) != 0)
            settings.Compression = PdfImageCompression.Lzw;
        else if ((page.BackgroundImage.Compression & PdfCompression.Jbig2) != 0)
            settings.Compression = PdfImageCompression.Jbig2;

        if ((page.BackgroundImage.Compression & PdfCompression.Zip) != 0)
            settings.Compression |= PdfImageCompression.Zip;
    }

    return settings;
}
Best regards, Alexander
Post Reply