We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How can I downscale images while save as Epub Document

Hi Team,

I like to convert my word document to Epub document using aspose for word .net.
After converting to Epub document all images should have a maximum resolution of 300 DPI; if image resolution is higher than 300 DPI, I want to downscale resolution to 300 DPI.

I tried this code

HtmlSaveOptions saveOptions = new HtmlSaveOptions(SaveFormat.Epub);
saveOptions.ImageResolution = 300;

But this sets resolution of all images to 300 DPI. But I don’t want to upscale the image resolution.

While saving as Epub document, is there any way to downscale image DPI to a particular value.

Any thoughts are highly appreciated.

Thanks,
Arun Chandran C
@cArunchandran

Hi Arun,

Thanks for your inquiry. We are working over your query and will reply you asap.

Hi Arun,

Thanks for your patience. HtmlSaveOptions.ImageResolution property specifies the output resolution for images when exporting to HTML, MHTML or EPUB. Default is 96 dpi. This property sets the image resolution for all images in output html.

Following code example shows how to decrease the resolution of image. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    if (shape.ImageData.HasImage)
    {
        if (shape.ImageData.ImageSize.HorizontalResolution > 300)
        {
            MemoryStream stream = new MemoryStream();
            ImageSaveOptions imageoptions = new ImageSaveOptions(SaveFormat.Jpeg);
            imageoptions.Resolution = 300;
            shape.GetShapeRenderer().Save(stream, imageoptions);
            shape.ImageData.SetImage(stream);
        }
    }
}
Aspose.Words.Saving.HtmlSaveOptions saveOptions = new Aspose.Words.Saving.HtmlSaveOptions(SaveFormat.Epub);
saveOptions.ImageResolution = 300;
doc.Save(MyDir + "Out.Epub", saveOptions);

Hi Tahir,

Thanks for your replay.

I tried your code

Document doc = new Document(MyDir + "in.docx");
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    if (shape.ImageData.HasImage)
    {
        if (shape.ImageData.ImageSize.HorizontalResolution > 300)
        {
            MemoryStream stream = new MemoryStream();
            ImageSaveOptions imageoptions = new ImageSaveOptions(SaveFormat.Jpeg);
            imageoptions.Resolution = 300;
            shape.GetShapeRenderer().Save(stream, imageoptions);
            shape.ImageData.SetImage(stream);
        }
    }
}
Aspose.Words.Saving.HtmlSaveOptions saveOptions = new Aspose.Words.Saving.HtmlSaveOptions(SaveFormat.Epub);
doc.Save(MyDir + "Out.Epub", saveOptions);

But still I got Image with 96 DPI only.
If I add “saveOptions.ImageResolution = 300;” this line of code,Then all image resolution changes to 300.

  1. I don’t want to change the resolution of all images.
  2. I want to downscale image resolution to 300 DPI for the images having DPI greater than 300 only.
    Can you check this two points?

Hi Arun,

Thanks for sharing the detail. Unfortunately, Aspose.Words does not support the requested feature at the moment. However, I have logged this feature request as WORDSNET-12353 in our issue tracking system. You will be notified via this forum thread once this feature is available. This new feature will save the images with original image resolution in output Epub.

We apologize for your inconvenience.

Hi Arun,

Thanks for your patience. In your case, HtmlSaveOptions.ScaleImageToShapeSize = false property will help you to get the required output. So, we are closing WORDSNET-12353 issue.

When ScaleImageToShapeSize is true, the image is scaled by Aspose.Words using high quality scaling during export to HTML. When ScaleImageToShapeSize is false, the image is output with its original size and the browser has to scale it.

Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    if (shape.ImageData.HasImage)
    {
        if (shape.ImageData.ImageSize.HorizontalResolution > 300)
        {
            MemoryStream stream = new MemoryStream();
            ImageSaveOptions imageoptions = new ImageSaveOptions(SaveFormat.Jpeg);
            imageoptions.Resolution = 300;
            shape.GetShapeRenderer().Save(stream, imageoptions);
            shape.ImageData.SetImage(stream);
        }
    }
}
Aspose.Words.Saving.HtmlSaveOptions saveOptions = new Aspose.Words.Saving.HtmlSaveOptions(SaveFormat.Epub);
saveOptions.ScaleImageToShapeSize = false;
doc.Save(MyDir + "Out.Epub", saveOptions);