How to Set DPI for Slides when Rendering Them to Graphics2D in Java?

When using aspose slides to get the slide size, we get different size depending on the document. How to we set the slide resolution to render it to our specific size.

For example, In the below code,

Presentation pres = new Presentation("pres.pptx");
IRenderingOptions renderOptions = new RenderingOptions();
Dimension2D slideSize = pres.getSlideSize().getSize();
ISlide slide = pres.getSlides().get_Item(pageNum);

SlideSize returns the width:height as “960.0:540.0” for one document and “720.0:540.0” for another document.
I want to set the resolution to render so that any document can be rendered to the graphics object correctly .
For example,

public void Graphics(Graphics2D graphics)
{
    Presentation pres = new Presentation("pres.pptx");
    IRenderingOptions renderOptions = new RenderingOptions();
    Dimension2D slideSize = pres.getSlideSize().getSize();
    ISlide slide = pres.getSlides().get_Item(pageNum);
    slide.render(renderOptions, graphics); 
}

If the documents are of different size, the rendered graphics object is cut off for some documents or having extra width for smaller width documents. Is there a way to set the dpi or resolution for the slides?

@mirnalini,
Thank you for posting the question.

We have opened the following new ticket(s) in our internal issue tracking system and will consider the question according to the terms mentioned in Free Support Policies.

Issue ID(s): SLIDESJAVA-39412

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@mirnalini,
Our developers have reviewed your requirements. Please try using the following code snippet.

Presentation pres = new Presentation("presentation.pptx");
try {
    java.awt.Dimension desiredSlideSize = new java.awt.Dimension(1820, 1040);

    // Creates a BufferedImage with the slide size
    BufferedImage slideImage = new BufferedImage((int) desiredSlideSize.getWidth(), (int) desiredSlideSize.getHeight(), BufferedImage.TYPE_INT_ARGB);
    java.awt.Graphics graphics = slideImage.createGraphics();

    // Renders the first slide to the Graphics object
    try {
        pres.getSlides().get_Item(0).renderToGraphics(new RenderingOptions(), (Graphics2D) graphics, desiredSlideSize);
    } finally {
        if (graphics != null) graphics.dispose();
    }

    ImageIO.write(slideImage, "PNG", new File("Slide_0.png"));
} finally {
    if (pres != null) pres.dispose();
}

Thank you We will test and let you know.

@mirnalini,
Thank you for using Aspose.Slides. We will be waiting for your feedback.

Hi Andrew, Thanks for providing the above code. But we want to render the slide image to a existing graphics object. We tried the above code with our existing graphics object but we dont see the images rendered correctly.

The below code works perfectly for our need.

public void test(Graphics gr, Presentation pres, float zoomlevel)
{
Dimension2D slideSize = pres.getSlideSize().getSize();
    int width = (int) (slideSize.getWidth() * zoomLevel);
         int height = (int) (slideSize.getHeight() * zoomLevel);

         BufferedImage image = slide.getThumbnail(new Dimension(width, height));
         gr.drawImage(image, null, 0, 0);
}

We wanted to replace the drawimage with rendertographics to avoid memory issues. So tried the below.

public void test(Graphics gr, Presentation pres, float zoomlevel)
{
try
      {
         int width = (int) (slideSize.getWidth() * zoomLevel);
         int height = (int) (slideSize.getHeight() * zoomLevel);

         BufferedImage image = slide.getThumbnail(new Dimension(width, height));
         slide.renderToGraphics(renderOptions, gr, new Dimension(width, height));
      }
}

The above code does not generate the correct output.
We also tried the below code.

 slide.renderToGraphics(renderOptions, gr, zoomLevel, zoomLevel);

Can you please suggest if we can leverage rendertographics with any settings to cater to our needs.
Thanks in advance.

@mirnalini,
Thank you for the code snippets. Unfortunately, we don’t know what was done to the Graphics object (transformations, for example) that you pass to the method. Could you kindly share the simplest standalone project that reproduces the problem?

Attaching the standalone project which reproduces our issue. We have a graphics2D object to which we want to render the slide image.

We want to know what resolution does the renderToGraphics method use when rendering to the graphics object?

aspose.zip (8.1 MB)

In aspose words, we have getSizeinPixels which provides the Dimension required and renderToSize using the below code works perfectly. But we are not able to find similar methods for aspose slides.

Dimension thumbSize = doc.getPageInfo(pageNum).getSizeInPixels(scale, 72);
int imgWidth = (int) (thumbSize.getWidth());
int imgHeight = (int) (thumbSize.getHeight());
doc.renderToSize(pageNum - 1, gr, 0, 0, imgWidth, imgHeight); - gr is the graphics context to which we want to render our document page

Also we see that aspose words has getWidthinPoints for each page. But for aspose slides, we are not able to get the slide width and height. We can only get for the presentation size. If we have slides of varying size, how does aspose set its slide width and height.

@mirnalini,
Thank you for the details. I am working on the issue and will get back to you soon.

@mirnalini,
Thank you for your patience. You are creating images and Graphics2D objects that are 720 x 540, whereas presentation slides are 960 x 540, so the images appear cropped. Please try resizing your slides before rendering like this:

pres.getSlideSize().setSize(720, 540, SlideSizeScaleType.EnsureFit);

Slide Size|Aspose.Slides Documentation

Thank you for the code. We are aware that when we set the size, we can render it correctly which is what we have done in the first render method in our standalone code. Let me rephrase my question.
If i have slides of different height and width, this setSize option when applied to all the slides, we see image cut off when rendering to the same graphics object .
Can you please answer my below question.
In aspose words, we have getSizeinPixels which provides the Dimension required and renderToSize using the below code works perfectly. But we are not able to find similar methods for aspose slides.

Dimension thumbSize = doc.getPageInfo(pageNum).getSizeInPixels(scale, 72);
int imgWidth = (int) (thumbSize.getWidth());
int imgHeight = (int) (thumbSize.getHeight());
doc.renderToSize(pageNum - 1, gr, 0, 0, imgWidth, imgHeight); - gr is the graphics context to which we want to render our document page

Also we see that aspose words has getWidthinPoints for each page. But for aspose slides, we are not able to get the slide width and height. We can only get for the presentation size. If we have slides of varying size, how does aspose set its slide width and height.

@mirnalini,
Aspose.Slides returns the size of slides in points with 72 DPI. You can convert the size to any other size. I offered you the solution above. Unfortunately, I don’t have any additional information.

Thank you for confirming about dpi . Can you also confirm us what is the resolution of the slides when using renderToGraphics method?

Also what happens inside renderToGraphics method when the below method is invoked.
If i have a document of width and height : 720 * 540 and another document of width and height 960 * 720 and both has to be rendered to the same graphics object , what approach we can take?
I see that there is a rendertographics with scale parameters , but we see a image cut off when rendered to graphics object.

slide.renderToGraphics(renderoptions, graphics, new Dimension(width, height));

@mirnalini,
I am working on your questions and will get back to you soon.

@mirnalini,
Unfortunately, I don’t have answers to your questions but we found that you mixed up the method parameters in the sample project. We fixed it and now everything should work fine:

    public static void renderRenderToGraphicstoslideSize(Presentation pres)
    {
        System.out.println("start");
        Dimension2D slideSize = pres.getSlideSize().getSize();
        System.out.println("slide size render1 :" + slideSize.getWidth() + ":" + slideSize.getHeight());
        // Creates a BufferedImage with the slide size
        BufferedImage slideImage = new BufferedImage((int) slideSize.getWidth(), (int) slideSize.getHeight(), BufferedImage.TYPE_INT_RGB);
        int w = (int) slideImage.getWidth();
        int h = (int) slideImage.getHeight();
        java.awt.Graphics2D graphics = slideImage.createGraphics();
        graphics.setColor(Color.red);

        try
        {
            for (int index = 0; index < pres.getSlides().size(); index++)
            {
                ISlide slide =  pres.getSlides().get_Item(index);
                slide.renderToGraphics(new RenderingOptions(), graphics, new Dimension(w, h));
                ImageIO.write(slideImage, "png", new File(folderPath+"render1slide" + index + ".png"));
            }
            System.out.println("stop");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public static void render2RendertoGraphicswithDimension(Presentation pres)
    {
        System.out.println("start");
        Dimension2D slideSize = pres.getSlideSize().getSize();
        System.out.println("slide size render1 :" + slideSize.getWidth() + ":" + slideSize.getHeight());
        // our graphics context is of the below dimension
        BufferedImage slideImage = new BufferedImage(720, 540, BufferedImage.TYPE_INT_RGB);
        int w = (int) slideImage.getWidth();
        int h = (int) slideImage.getHeight();

        java.awt.Graphics2D graphics = slideImage.createGraphics();
        graphics.setColor(Color.red);

        try
        {
            for (int index = 0; index < pres.getSlides().size(); index++)
            {
                ISlide slide =  pres.getSlides().get_Item(index);
                slide.renderToGraphics(new RenderingOptions(), graphics, new Dimension(w, h));
                ImageIO.write(slideImage, "png", new File(folderPath+"render2slide" + index + ".png"));
            }
            System.out.println("stop");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public static void renderGetThumbnail(Presentation pres)
    {
        System.out.println("start");
        Dimension2D slideSize = pres.getSlideSize().getSize();
        System.out.println("slide size render1 :" + slideSize.getWidth() + ":" + slideSize.getHeight());
        // our graphics context is of the below dimension
        BufferedImage slideImage = new BufferedImage(720, 540, BufferedImage.TYPE_INT_RGB);
        int w = (int) slideImage.getWidth();
        int h = (int) slideImage.getHeight();

        java.awt.Graphics2D graphics = slideImage.createGraphics();
        graphics.setColor(Color.red);

        try
        {
            for (int index = 0; index < pres.getSlides().size(); index++)
            {
                ISlide slide =  pres.getSlides().get_Item(index);
                BufferedImage image = slide.getThumbnail(new Dimension(w, h));
                graphics.drawImage(image, null, 0, 0);
                ImageIO.write(slideImage, "png", new File(folderPath+"render3slide" + index + ".png"));
            }
            System.out.println("stop");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

Please also note that the renderToGraphics methods will be removed in version 24.8.

If renderToGraphics method is removed, What will be the alternate we would get? If we need to use BufferedImage, drawing it to graphics object is causing out of memory errors. Can you please provide more information on this?

Also can you please clarify if this is only for slides or for other aspose packages such as words also?

@mirnalini,

Thank you for your questions, I’ve forwarded them to our developers. We will answer you soon, thank you for your patience.

Unfortunately, I don’t have information about other Aspose products. You can ask my colleagues about this on Aspose.Total forum.