Enhance Image Processing with the Modern API
Introduction
Historically, Aspose Slides has a dependency on System.Drawing and has in the public API the following classes from there:
As of version 24.4, this public API is declared deprecated.
Since System.Drawing support in versions .NET6 and above is removed for non-Windows versions (breaking change), Slides has implemented a two-package approach:
- Aspose.Slides.NET - support for .NET6+ for Windows, .NETStandard for Windows/Linux/MacOS, .NETFramework 2+ (Windows).
- has a dependence on System.Drawing.Common.
- Aspose.Slides.NET6.CrossPlatform - Windows/Linux/MacOS version without dependencies.
The inconvenience of Aspose.Slides.NET6.CrossPlatform is that it implements its own version of System.Drawing in the same namespace (to support backward compatibility with the public API). Thus, when Aspose.Slides.NET6.CrossPlatform and System.Drawing from .NET Framework or System.Drawing.Common package are used at the same time, a name conflict occurs unless alias is used.
In order to get rid of dependencies on System.Drawing in the main Aspose.Slides.NET package, we added the so-called “Modern API” - i.e. the API that should be used instead of the deprecated one, whose signatures contain dependencies on the following types from System.Drawing: Image and Bitmap. PrinterSettings and Graphics are declared deprecated and their support is removed from the public Slides API.
In current versions, treat the public API that depends on System.Drawing as legacy/deprecated. Use the Modern API for new code and when migrating existing image-processing workflows.
Modern API
Added the following classes and enums to the public API:
- Aspose.Slides.IImage - represents the raster or vector image.
- Aspose.Slides.ImageFormat - represents the file format of the image.
- Aspose.Slides.Images - methods to instantiate and work with the IImage interface.
Please note that IImage is disposable (it implements the IDisposable interface and its use should be wrapped in using or disposed in another convenient way).
Use GetImage to render a single slide or shape. Use GetImages to render several presentation slides. Use Images methods to load images, AddImage with IImage to add them to a presentation, and ReplaceImage with IImage to update an existing presentation image.
A typical scenario of using the new API may look as follows:
using (Presentation pres = new Presentation())
{
IPPImage ppImage;
// instantiate a disposable instance of IImage from the file on the disk.
using (IImage image = Images.FromFile("image.png"))
{
// create a PowerPoint image by adding an instance of IImage to the presentation's images.
ppImage = pres.Images.AddImage(image);
}
// add a picture shape on the slide #1
pres.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, ppImage);
// get an instance of the IImage representing slide #1.
using (var slideImage = pres.Slides[0].GetImage(new Size(1920, 1080)))
{
// save the image on the disk.
slideImage.Save("slide1.jpeg", ImageFormat.Jpeg);
}
}
Replacing Old Code with Modern API
For ease of transition, the interface of the new IImage repeats the separate signatures of the Image and Bitmap classes. In general, you will just need to replace the call to the old method using System.Drawing with the new one.
Getting a Slide Thumbnail
Legacy/deprecated API:
using (Presentation pres = new Presentation("pres.pptx"))
{
pres.Slides[0].GetThumbnail().Save("slide1.png");
}
Modern API:
using (Presentation pres = new Presentation("pres.pptx"))
{
pres.Slides[0].GetImage().Save("slide1.png");
}
Getting a Shape Thumbnail
Legacy/deprecated API:
using (Presentation pres = new Presentation("pres.pptx"))
{
pres.Slides[0].Shapes[0].GetThumbnail().Save("shape.png");
}
Modern API:
using (Presentation pres = new Presentation("pres.pptx"))
{
pres.Slides[0].Shapes[0].GetImage().Save("shape.png");
}
Getting a Presentation Thumbnail
Legacy/deprecated API:
using (Presentation pres = new Presentation("pres.pptx"))
{
var bitmaps = pres.GetThumbnails(new RenderingOptions(), new Size(1980, 1028));
try
{
for (var index = 0; index < bitmaps.Length; index++)
{
Bitmap thumbnail = bitmaps[index];
thumbnail.Save($"slide{index}.png", ImageFormat.Png);
}
}
finally
{
foreach (Bitmap bitmap in bitmaps)
{
bitmap.Dispose();
}
}
}
Modern API:
using (Presentation pres = new Presentation("pres.pptx"))
{
var images = pres.GetImages(new RenderingOptions(), new Size(1980, 1028));
try
{
for (var index = 0; index < images.Length; index++)
{
IImage thumbnail = images[index];
thumbnail.Save($"slide{index}.png", ImageFormat.Png);
}
}
finally
{
foreach (IImage image in images)
{
image.Dispose();
}
}
}
Adding a Picture to a Presentation
Legacy/deprecated API:
using (Presentation pres = new Presentation())
{
IPPImage ppImage;
using (Image image = Image.FromFile("image.png"))
{
ppImage = pres.Images.AddImage(image);
}
pres.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, ppImage);
}
Modern API:
using (Presentation pres = new Presentation())
{
IPPImage ppImage;
using (IImage image = Aspose.Slides.Images.FromFile("image.png"))
{
ppImage = pres.Images.AddImage(image);
}
pres.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, ppImage);
}
Deprecated Methods/Properties and Their Replacement in Modern API
Presentation
| Method Signature | Replacement Method Signature |
|---|---|
| public Bitmap[] GetThumbnails(IRenderingOptions options) | GetImages(IRenderingOptions options) |
| public Bitmap[] GetThumbnails(IRenderingOptions options, int[] slides) | GetImages(IRenderingOptions options, int[] slides) |
| public Bitmap[] GetThumbnails(IRenderingOptions options, float scaleX, float scaleY) | GetImages(IRenderingOptions options, float scaleX, float scaleY) |
| public Bitmap[] GetThumbnails(IRenderingOptions options, int[] slides, float scaleX, float scaleY) | GetImages(IRenderingOptions options, int[] slides, float scaleX, float scaleY) |
| public Bitmap[] GetThumbnails(IRenderingOptions options, Size imageSize) | GetImages(IRenderingOptions options, Size imageSize) |
| public Bitmap[] GetThumbnails(IRenderingOptions options, int[] slides, Size imageSize) | GetImages(IRenderingOptions options, int[] slides, Size imageSize) |
| public void Save(string fname, SaveFormat format, HttpResponse response, bool showInline) | No Modern API replacement |
| public void Save(string fname, SaveFormat format, ISaveOptions options, HttpResponse response, bool showInline) | No Modern API replacement |
| public void Print() | No Modern API replacement |
| public void Print(PrinterSettings printerSettings) | No Modern API replacement |
| public void Print(string printerName) | No Modern API replacement |
| public void Print(PrinterSettings printerSettings, string presName) | No Modern API replacement |
Shape
| Method Signature | Replacement Method Signature |
|---|---|
| public Bitmap GetThumbnail() | GetImage |
| public Bitmap GetThumbnail(ShapeThumbnailBounds bounds, float scaleX, float scaleY) | GetImage(ShapeThumbnailBounds bounds, float scaleX, float scaleY) |
Slide
| Method Signature | Replacement Method Signature |
|---|---|
| public Bitmap GetThumbnail(float scaleX, float scaleY) | GetImage(float scaleX, float scaleY) |
| public Bitmap GetThumbnail() | GetImage |
| public Bitmap GetThumbnail(IRenderingOptions options) | GetImage(IRenderingOptions options) |
| public Bitmap GetThumbnail(Size imageSize) | GetImage(Size imageSize) |
| public Bitmap GetThumbnail(ITiffOptions options) | GetImage(ITiffOptions options) |
| public Bitmap GetThumbnail(IRenderingOptions options, float scaleX, float scaleY) | GetImage(IRenderingOptions options, float scaleX, float scaleY) |
| public Bitmap GetThumbnail(IRenderingOptions options, Size imageSize) | GetImage(IRenderingOptions options, Size imageSize) |
| public void RenderToGraphics(IRenderingOptions options, Graphics graphics) | No Modern API replacement |
| public void RenderToGraphics(IRenderingOptions options, Graphics graphics, float scaleX, float scaleY) | No Modern API replacement |
| public void RenderToGraphics(IRenderingOptions options, Graphics graphics, Size renderingSize) | No Modern API replacement |
Output
| Method Signature | Replacement Method Signature |
|---|---|
| public IOutputFile Add(string path, Image image) | Add(string path, IImage image) |
ImageCollection
| Method Signature | Replacement Method Signature |
|---|---|
| IPPImage AddImage(Image image) | AddImage(IImage image) |
ImageWrapperFactory
| Method Signature | Replacement Method Signature |
|---|---|
| IImageWrapper CreateImageWrapper(Image image) | CreateImageWrapper(IImage image) |
PPImage
| Method/Property Signature | Replacement Method Signature |
|---|---|
| void ReplaceImage(Image newImage) | ReplaceImage(IImage newImage) |
| Image SystemImage { get; } | IImage Image { get; } |
PatternFormat
| Method Signature | Replacement Method Signature |
|---|---|
| Bitmap GetTileImage(Color background, Color foreground) | GetTile(Color background, Color foreground) |
| Bitmap GetTileImage(Color styleColor) | GetTile(Color styleColor) |
IPatternFormatEffectiveData
| Method Signature | Replacement Method Signature |
|---|---|
| Bitmap GetTileImage(Color background, Color foreground) | GetTileIImage(SlidesImage image) |
API Support for Graphics and PrinterSettings
The Graphics class is not supported for cross-platform versions of .NET6 and higher. In Aspose Slides, use the Modern API image-rendering methods instead of the API that renders to Graphics: ISlide
- public void RenderToGraphics(IRenderingOptions options, Graphics graphics)
- public void RenderToGraphics(IRenderingOptions options, Graphics graphics, float scaleX, float scaleY)
- public void RenderToGraphics(IRenderingOptions options, Graphics graphics, Size renderingSize)
Also, the API that is related to printing through PrinterSettings has no direct Modern API replacement:
- public void Presentation.Print
- public void Print(PrinterSettings printerSettings)
- public void Print(string printerName)
- public void Print(PrinterSettings printerSettings, string presName)
FAQ
Why was Graphics dropped?
Support for Graphics is deprecated in the public API to unify work with rendering and images, eliminate ties to platform-specific dependencies, and switch to a cross-platform approach with IImage. Use GetImage or GetImages instead of rendering to Graphics.
What is the practical benefit of IImage compared to Image/Bitmap?
IImage unifies working with both raster and vector images, simplifies saving to various formats via ImageFormat, reduces dependence on System.Drawing, and makes code more portable across environments.
Will the Modern API affect the performance of generating thumbnails?
Switching from GetThumbnail to GetImage does not worsen scenarios: the new methods provide the same capabilities for producing images with options and sizes, while retaining support for rendering options. The specific gain or drop depends on the scenario, but functionally the replacements are equivalent.