Image compress

Do you have code sample on how to use the new feature WORDSNET-2110

Thanks

Hi Akram,

Thanks for your inquiry. Using the following three properties of DownsampleOptions class, you can instruct Aspose.Words to Downsample to X dpi for images which are above Y dpi during rendering to PDF.

DownsampleOptions.DownsampleImages property is used to specify whether images should be down sampled or not.
DownsampleOptions.Resolution property is used to specify the resolution in pixels per inch which the images should be down sampled to.
DownsampleOptions.ResolutionThreshold property is used specify the threshold resolution in pixels per inch. If resolution of an image in the document is less than threshold value, the down sampling algorithm will not be applied. A value of 0 means the threshold check is not used and all images that can be reduced in size are down sampled.

Here is the sample usage:

Document doc = new Document(MyDir + @"in.docx");
PdfSaveOptions options = new PdfSaveOptions();
options.DownsampleOptions.DownsampleImages = true;
options.DownsampleOptions.ResolutionThreshold = 200;
options.DownsampleOptions.Resolution = 96;
doc.Save(MyDir + @"out.pdf", options);

I hope, this helps.

Best regards,

is there a way to compress the images in the word document itself. i.e. reduce the size of the word document.

Hi Akram,

Thanks for your inquiry. I think, you can reduce the size of images by using the following code:

Document doc = new Document(MyDir + @"MyReport_20120227021439.doc");
foreach(Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    using(MemoryStream imageStream = new MemoryStream(shape.ImageData.ImageBytes))
    using(Image image = Image.FromStream(imageStream))
    {
        Size newSize = new Size(image.Width, image.Height);
        using(Bitmap newImage = new Bitmap(newSize.Width, newSize.Height))
        {
            using(Graphics g = Graphics.FromImage(newImage))
            {
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.InterpolationMode = InterpolationMode.Low;
                g.PixelOffsetMode = PixelOffsetMode.HighSpeed;
                g.DrawImage(image, new Rectangle(0, 0, newSize.Width, newSize.Height));
                using(MemoryStream rasterImageStream = new MemoryStream())
                {
                    image.Save(rasterImageStream, ImageFormat.Png);
                    shape.ImageData.SetImage(newImage);
                }
            }
        }
    }
}
doc.Save(MyDir + @"out.pdf");

Please note that this method will also reduce the quality of images. If we can help you with anything else, please feel free to ask.

Best regards,