DEV Community

Dmitry Matuzko
Dmitry Matuzko

Posted on

Introduction to Aspose.Imaging, Part 5

Part 1

Part 2

Part 3

Part 4

In this article on Aspose.Imaging, I'll, tell about use of automatic grayscaling feature, which I've forgot to include in previous articles, and most importantly, how to work with XMP metadata in Aspose.Imaging.

Grayscaling

This is as simple as possible, any descendant of RasterImage has an Grayscale method, which turns image to grayscale, obviously.

// Load an image in an instance of Image
using (Image image = Image.Load("aspose-logo.jpg"))
{
    // Cast the image to RasterCachedImage and Check if image is cached
    RasterCachedImage rasterCachedImage = (RasterCachedImage)image;               
    if (!rasterCachedImage.IsCached)
    {
        // Cache image if not already cached
        rasterCachedImage.CacheData();
    }

    // Transform image to its grayscale representation and Save the resultant image
    rasterCachedImage.Grayscale();
    rasterCachedImage.Save("Grayscaling_out.jpg");
}
Enter fullscreen mode Exit fullscreen mode

XMP metadata

XMP metadata as represented in Aspose.Imaging is organised as several "packets" that are contained within XMP packet wrapper. Packets correspond to use cases and are represented by parent XmpPackage class and descendant classes for these cases: DublinCorePackage, PdfPackage, PhotoshopPackage, XmpBasicPackage, XmpDynamicMediaPackage, XmpMediaManagementPackage and XmpRightsManagementPackage. Each of them has properties corresponding to its use case.

Packet wrapper, i.e. the top-level metadata object, is represented by XmpPacketWrapper class. Packages are appended to it via AddPackage method of XmpPacketWrapper's instance. Package's instance has to be created and its properties set up before addition to XmpPacketWrapper.
The XmpPacketWrapper itself is created using instances of three other classes: XmpHeaderPi, which contains GUID for this metadata collection, XmpMeta, which contains generic metadata and is optional, and XmpTrailerPi, which contains the flag allowing or disallowing modification of XMP data for subsequent processors.
Instance of XmpPacketWrapper is then put into XmpData property of a instance of a descendant of RasterImage class.
Conversely, to read metadata, get XmpPacketWrapper instance from XmpData property of an loaded image, and to get packages, use Packages property of the instance.
Here is an example of creating XMP metadata and embedding it into a TIFF file:


// Specify the size of image by defining a Rectangle 
Rectangle rect = new Rectangle(0, 0, 100, 200);
TiffOptions tiffOptions = new TiffOptions(TiffExpectedFormat.TiffJpegRgb);
tiffOptions.Photometric = TiffPhotometrics.MinIsBlack;
tiffOptions.BitsPerSample = new ushort[] { 8 };

// Create the brand new image just for sample purposes
using (var image = new TiffImage(new TiffFrame(tiffOptions, rect.Width, rect.Height)))
{
    // Create an instance of XMP-Header
    XmpHeaderPi xmpHeader = new XmpHeaderPi(Guid.NewGuid().ToString());

    // Create an instance of Xmp-TrailerPi, XMPmeta class to set different attributes
    XmpTrailerPi xmpTrailer = new XmpTrailerPi(true);
    XmpMeta xmpMeta = new XmpMeta();
    xmpMeta.AddAttribute("Author", "Mr Smith");
    xmpMeta.AddAttribute("Description", "The fake metadata value");

    // Create an instance of XmpPacketWrapper that contains all metadata
    XmpPacketWrapper xmpData = new XmpPacketWrapper(xmpHeader,xmpTrailer, xmpMeta);

    // Create an instacne of Photoshop package and set photoshop attributes
    PhotoshopPackage photoshopPackage =  new PhotoshopPackage();
    photoshopPackage.SetCity("London");
    photoshopPackage.SetCountry("England");
    photoshopPackage.SetColorMode(ColorMode.Rgb);
    photoshopPackage.SetCreatedDate(DateTime.UtcNow);

    // Add photoshop package into XMP metadata
    xmpData.AddPackage(photoshopPackage);

    // Create an instacne of DublinCore package and set dublinCore attributes
    DublinCorePackage dublinCorePackage = new DublinCorePackage();
    dublinCorePackage.SetAuthor("Charles Bukowski");
    dublinCorePackage.SetTitle("Confessions of a Man Insane Enough to Live With the Beasts");
    dublinCorePackage.AddValue("dc:movie", "Barfly");

    // Add dublinCore Package into XMP metadata
    xmpData.AddPackage(dublinCorePackage);

    //Add metadata to image and save
    image.XmpData = xmpData;
    image.Save("XmpSample.tiff");
}
Enter fullscreen mode Exit fullscreen mode

Now, to read the created data, see the next example:

using (var img = (TiffImage)Image.Load("XmpSample.tiff"))
{
    // Getting the XMP metadata
    XmpPacketWrapper imgXmpData = img.XmpData;


    foreach (XmpPackage package in imgXmpData.Packages)
    {
        // Use package data ...
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

Latest comments (0)