Aspose.Words.Examples.CSharp.Programming_Documents.Working_with_Images.Resampler.Resample C# (CSharp) Method

Resample() public static method

Resamples all images in the document that are greater than the specified PPI (pixels per inch) to the specified PPI And converts them to JPEG with the specified quality setting.
public static Resample ( Document doc, int desiredPpi, int jpegQuality ) : int
doc Document The document to process.
desiredPpi int Desired pixels per inch. 220 high quality. 150 screen quality. 96 email quality.
jpegQuality int 0 - 100% JPEG quality.
return int
        public static int Resample(Document doc, int desiredPpi, int jpegQuality)
        {
            int count = 0;

            // Convert VML shapes.
            foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
            {
                // It is important to use this method to correctly get the picture shape size in points even if the picture is inside a group shape.
                SizeF shapeSizeInPoints = shape.SizeInPoints;

                if (ResampleCore(shape.ImageData, shapeSizeInPoints, desiredPpi, jpegQuality))
                    count++;
            }

            return count;
        }

Usage Example

        public static void Run()
        {
            // The path to the documents directory.
            string dataDir     = RunExamples.GetDataDir_WorkingWithImages();
            string fileName    = "Test.docx";
            string srcFileName = dataDir + fileName;

            Console.WriteLine("Loading {0}. Size {1}.", srcFileName, GetFileSize(srcFileName));
            Document doc = new Document(srcFileName);

            // 220ppi Print - said to be excellent on most printers and screens.
            // 150ppi Screen - said to be good for web pages and projectors.
            // 96ppi Email - said to be good for minimal document size and sharing.
            const int desiredPpi = 150;

            // In .NET this seems to be a good compression / quality setting.
            const int jpegQuality = 90;

            // Resample images to desired ppi and save.
            int count = Resampler.Resample(doc, desiredPpi, jpegQuality);

            Console.WriteLine("Resampled {0} images.", count);

            if (count != 1)
            {
                Console.WriteLine("We expected to have only 1 image resampled in this test document!");
            }

            string dstFileName = dataDir + RunExamples.GetOutputFilePath(fileName);

            doc.Save(dstFileName);
            Console.WriteLine("Saving {0}. Size {1}.", dstFileName, GetFileSize(dstFileName));

            // Verify that the first image was compressed by checking the new Ppi.
            doc = new Document(dstFileName);
            Shape  shape    = (Shape)doc.GetChild(NodeType.Shape, 0, true);
            double imagePpi = shape.ImageData.ImageSize.WidthPixels / ConvertUtil.PointToInch(shape.SizeInPoints.Width);

            Debug.Assert(imagePpi < 150, "Image was not resampled successfully.");

            Console.WriteLine("\nCompressed images successfully.\nFile saved at " + dstFileName);
        }