Picture
Contents
[
Hide
]
This article demonstrates how to insert and access pictures from in-memory images using Aspose.Slides for Android via Java. The examples below create an image in memory, place it on a slide, and then retrieve it.
Add a Picture
This code generates a small bitmap, converts it to a stream, and inserts it as a picture frame on the first slide.
public static void addPicture() throws IOException {
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
// Create a simple in-memory image.
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas graphics = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.valueOf(144, 238, 144).toArgb());
paint.setStyle(Paint.Style.FILL);
graphics.drawRect(0, 0, 100, 100, paint);
// Convert the bitmap to a byte array.
ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG,100, bitmapStream);
byte[] pngBytes = bitmapStream.toByteArray();
// Add the image to the presentation.
IPPImage image = presentation.getImages().addImage(new ByteArrayInputStream(pngBytes));
// Insert a picture frame showing the image on the first slide.
slide.getShapes().addPictureFrame(ShapeType.Rectangle, 50, 50, bitmap.getWidth(), bitmap.getHeight(), image);
presentation.save("picture.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
}
Access a Picture
This example ensures a slide contains a picture frame and then accesses the first one it finds.
public static void accessPicture() throws IOException {
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG,100, bitmapStream);
byte[] pngBytes = bitmapStream.toByteArray();
IPPImage image = presentation.getImages().addImage(new ByteArrayInputStream(pngBytes));
slide.getShapes().addPictureFrame(ShapeType.Rectangle, 0, 0, 40, 40, image);
IPictureFrame pictureFrame = null;
for (IShape shape : slide.getShapes()) {
if (shape instanceof IPictureFrame) {
pictureFrame = (IPictureFrame) shape;
break;
}
}
} finally {
presentation.dispose();
}
}