loading...

Introduction to Aspose.Imaging

nnevod profile image Dmitry Matuzko ・7 min read

Aspose.Imaging is a software library for .Net and Java that provides an API for conversion of raster images between different formats and also allows their modification and creation. There is also a version for Sharepoint. It requires no additional software to work.

While Aspose.Imaging is a commercial library, it allows free evaluation, which offers all the functionality and can readily be used for experimenting, but only loads files of limited complexity and watermarks exported images. Besides paying upfront, one can use metered, pay-per-use license.

I'm going to write a series of articles covering key points of working with Aspose.Imaging using Aspose.Imaging for .NET, starting from the most basic things such as loading an image and saving it to a different format to more specific image manipulations.

In this article I will describe the following operations that can be performed with Aspose.Imaging:

  1. Simple loading and saving of an image
  2. Creating image
  3. Drawing on an image

Simple loading and saving of an image

Take a look at the example code:

            // 1 Load an image through file path location or stream
            using (Image image = Image.Load( "Sample1.png"))
            {
                //2 Create an instance of TiffOptions while specifying desired format Passsing TiffExpectedFormat.TiffJpegRGB will set the compression to Jpeg and BitsPerPixel according to the RGB color space.
                TiffOptions options = new TiffOptions(TiffExpectedFormat.TiffJpegRgb);
                //3 Save the image with specified options
                image.Save(dataDir + "SampleTiff_out.tiff", options);
            }

The process of loading and saving images consists of three steps, correspondingly numbered in comments in example:
1) Load the image
2) Set up processing options
3) Save the image using specified processing options.

Let's take a bit deeper look into the steps.
1) Loading is the simplest step, just call static Load method of Image class specifying path to your image file or a Stream of your image data. The image format will be determined automatically from its contents.
2) The main step where all your code will be. Let's skip to the simple third step and return here later.
3) Call Save method on your Image instance, specifying output path or Stream and processing options as in instance of a descendant of ImageOptionsBase class. The output image format will be determined from particular type of processing options object and particular format's settings will be derived from its' corresponding fields. As such, you see that everything is really done in step 2.

So, take a deeper dive into step 2.
There are several classes that descend from ImageOptionsBase and can be used to set up output image format: BmpOptions, GifOptions, Jpeg2000Options, JpegOptions, PngOptions, PsdOptions, SvgOptions, WebPOptions, TiffOptions. Each of these classes provides setup for saving image to corresponding format. Most of them are absolutely straightforward to use - just create an instance using parameterless constructor and you're done, any other setup is optional. Only two formats are different: with TiffOptions, you have to specify compression format and bit depth for constructor by providing a value from TiffExpectedFormat enum, and with SvgOptions you have to also set your SvgOptions instance's VectorRasterizaionOptions property with an instance of SvgRasterizationOptions class and set that SvgRasterizationOptions instance's PageHeight and PageWidth properties. As SVG is a vector format, it has concept of page dimension, not just a matrix of X*Y pixels. The raster image embedded into SVG page is then will be scaled during display to match specified page size.

That is enough for the basic loading and saving of an image, you don't need anything more. Just check out the complete example with specified namespaces etc. below.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.Sources;

namespace ImagingExample
{
    class Program
    {
        static void Main(string[] args)
        {
            using (Image image = Image.Load("Sample1.png"))
            {
                //2 Create an instance of TiffOptions while specifying desired format Passsing TiffExpectedFormat.TiffJpegRGB will set the compression to Jpeg and BitsPerPixel according to the RGB color space.
                TiffOptions options = new TiffOptions(TiffExpectedFormat.TiffJpegRgb);
                //3 Save the image with specified options
                image.Save( "SampleTiff_out.tiff", options);
            }
        }
    }
}



Just create a new console application project for .Net framework and replace the code in Program.cs with that one.

Creating an image.

With Aspose.Imaging you can not only load an existing image, but also create it anew and fill it in several different ways. Take a look at the example:

            // 1. Create image options
            BmpOptions ImageOptions = new BmpOptions();
            ImageOptions.BitsPerPixel = 24;

            // 2. Create an instance of FileCreateSource and assign it to Source property
            ImageOptions.Source = new FileCreateSource(dataDir + "DrawImagesUsingCoreFunctionality_out.bmp", false);

            // 3. Create an instance of RasterImage and Get the pixels of the image by specifying the area as image boundary
            using (RasterImage rasterImage = (RasterImage)Image.Create(ImageOptions, 500, 500))
            {
                //4. Create the contents of the image
                Color[] pixels = rasterImage.LoadPixels(rasterImage.Bounds);
                for (int index = 0; index < pixels.Length; index++)
                {
                    // Set the indexed pixel color to yellow
                    pixels[index] = Color.Yellow;
                }

                //4.5 Apply the pixel changes to the image 
                rasterImage.SavePixels(rasterImage.Bounds, pixels);
                //5. And save all changes.
                rasterImage.Save();
            }


`
Image creation begins from creating an instance of one of the aforementioned ImageOptionsBase descendant classes.

Then you have to specify a file associated with image, as we don't load an image from existing source.

In third step you create the RasterImage instance using created options and specifying image width and height in pixels. RasterImage allows raster manipulation.

Fourth step is where you do any modification of image you want to do. In the example, a way to perform direct per-pixel color manipulation is shown. There is also another way, which I'll explain a bit later.

Step 4.5 is named so because calling SavePixels is specific to such an approach to image manipulation, it directly writes the color array to image content, the "pixels" array is not the image content as is, but is a copy.

Finally, in step 5 image is saved. As image source had been provided with associated file path and the image is created with an option set from the start, there is no need to specify anything.

Drawing on an image

Here is another way to manipulate the image's contents in Aspose.Imaging API. Let's modify the previous example:
`

                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);
                    graphic.Clear(Color.Yellow);

                    // Initializes the instance of PEN class with black color and width
                    Pen BlackPen = new Pen(Color.Black, 3);
                    float startX = 10;
                    float startY = 25;
                    float controlX1 = 20;
                    float controlY1 = 5;
                    float controlX2 = 55;
                    float controlY2 = 10;
                    float endX = 90;
                    float endY = 25;

                    // Draw a Bezier shape by specifying the Pen object having black color and co-ordinate Points and save all changes.
                    graphic.DrawBezier(BlackPen, startX, startY, controlX1, controlY1, controlX2, controlY2, endX, endY);
                    image.Save();
                }

Here we see the use of Aspose.Imaging.Graphics class. Its API is essentially the same as API of System.Drawing.Graphics class and supports the same operations, so you'll feel yourself familiar with it. You can draw Bezier curves, lines, arcs, etc. this way. To save the image, just call Save on your image instance - Graphics works directly on image content so no need for additional save call.

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.

Posted on by:

Discussion

pic
Editor guide