DEV Community


Clarifying image creation in Aspose.Imaging

nnevod profile image Dmitry Matuzko ・3 min read

As it was stated in previous articles, Aspose.Imaging not only allows loading, manipulation and export of images, but it also allows creation of images from scratch. The creation process, however, might not be very obvious, so I'll describe it here in a few more words.

To create a new image in Aspose.Imaging, use Image's Create method. It accepts three parameters - an instance of a descendant of ImageOptions (i.e. PngOptions, BmpOptions etc.), and width and height in pixels. The ImageOptions' descendant instance is the same that is ordinarily used to export image - as that ImageOptions specifies what's needed for a new image - pixel format, etc. In this case it would just produce clean white file, instead of filling it with content from another image. That's why width and height are specified separately - as exported image's dimensions are inferred from its content, and when creating new image, we have no content yet. Take a look at the example and don't skip the following paragraph!

using (FileStream stream = new FileStream("newImage.png", FileMode.Create))
    // So we want to create a PNG image
    PngOptions saveOptions = new PngOptions();
    // Here you can set whatever properties of saveOptions you need

    // Set the Source for PngOptions and create an instance of Image
    saveOptions.Source = new StreamSource(stream);               
    using (Image image = Image.Create(saveOptions, 100, 100))
        // Create and initialize an instance of Graphics class and clear Graphics surface
        Graphics graphic = new Graphics(image);

        // Draw an arc shape by specifying the Pen object having red black color and coordinates, height, width,    start & end angles                 
        int width = 100;
        int height = 200;
        int startAngle = 45;
        int sweepAngle = 270;

        // Draw arc to screen and save all changes.
        graphic.DrawArc(new Pen(Color.Black), 0, 0, width, height, startAngle, sweepAngle);
        // Note you can just call Save() without parameters, as source is bound to image
    // Now close the stream.
Enter fullscreen mode Exit fullscreen mode

Note that besides all the typical properties you specify when exporting an image, here we also specify Source property. That's how we specify where the newly created image should be stored and this property must be set with a Source created from a valid writable Stream to create an image.

There is also a helper method that allows you to skip creating Stream by yourself, the FileCreateSource method in the FileCreateSource class. Take a look at using it:

ImageOptions.Source = new FileCreateSource("myNewImage.png", false);
Enter fullscreen mode Exit fullscreen mode

Note the second parameter, it allows creation of temporary files. If you set it to true, the file will be created as temporary one. Nonetheless, you should dispose the stream after you used it.

Note also how we can just apply changes to the image by calling parameterless Save() in the first example - this is exactly because Source is specified and image is bound to a file. This is in contrast to loading an existing image, which is virtually copied and has to be saved to an explicitly provided file.

That's all for now, stay tuned!

For more examples please visit the Aspose.Imaging GitHub page. There's also Twitter and Facebook pages for news on Aspose.Imaging.

Discussion (0)

Forem Open with the Forem app