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.
@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?
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 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:
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,
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?
We want to get the slide image to a graphics object. We tried using renderToGraphics but that was not fitting to the graphics size.
Current we got the thumbnail of the slide with the width and height of the graphics object and used drawImage which perfectly works for us. But the problem is when drawing such huge images we are seeing out of memory exception.
If there is a method which can help us draw the image of the slide object to a graphics object, it would be great for us.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.