Home > Code > C# > How to Read and Write XMP Data to Images inside .NET Apps

How to Read and Write XMP Data to Images inside .NET Apps

by sher azam   on Feb 24, 2016   Category: C#   |  Views: 526    |  Points: 25   |  Gold 


This technical tip explains how .NET developers can read and write XMP data to images inside their .NET applications. XMP (Extensible Metadata Platform) is an ISO standard. XMP standardizes a data model, a serialization format and core properties for the definition and processing of extensible metadata. It also provides guidelines for embedding XMP information into popular image such as JPEG, without breaking their readability by applications that do not support XMP. Using Aspose.Imaging for .NET API developers can read or write XMP metadata to images. This article demonstrates how XMP metadata can be read from image and write XMP metadata to images. Aspose.Imaging for .NET 3.3.0 contains the Xmp namespace. With the help of Xmp namespace developer can create XMP metadata object and write it to an image. The code snippet provided below demonstrates how to use the XmpHeaderPi, XmpTrailerPi, XmpMeta, XmpPacketWrapper, PhotoshopPackage and DublinCorePackage packages contained in Xmp namespace.

//Create XMP Metadata, Write It And Read From File

//[C# Code Sample]

// Specify the size of image by defining a Rectangle
Aspose.Imaging.Rectangle rect = new Aspose.Imaging.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
Aspose.Imaging.Xmp.XmpHeaderPi xmpHeader = new Aspose.Imaging.Xmp.XmpHeaderPi(System.Guid.NewGuid().ToString());

// create an instance of Xmp-TrailerPi
Aspose.Imaging.Xmp.XmpTrailerPi xmpTrailer = new Aspose.Imaging.Xmp.XmpTrailerPi(true);

// create an instance of XMPmeta class to set different attributes
Aspose.Imaging.Xmp.XmpMeta xmpMeta = new Aspose.Imaging.Xmp.XmpMeta();
xmpMeta.AddAttribute("Author", "Mr Smith");
xmpMeta.AddAttribute("Description", "The fake metadata value");

// create an instance of XmpPacketWrapper that contains all metadata
Aspose.Imaging.Xmp.XmpPacketWrapper xmpData = new Aspose.Imaging.Xmp.XmpPacketWrapper(xmpHeader, xmpTrailer, xmpMeta);

// create an instacne of Photoshop package and set photoshop attributes
Aspose.Imaging.Xmp.Schemas.Photoshop.PhotoshopPackage photoshopPackage = new Aspose.Imaging.Xmp.Schemas.Photoshop.PhotoshopPackage();
photoshopPackage.SetCity("London");
photoshopPackage.SetCountry("England");
photoshopPackage.SetColorMode(Aspose.Imaging.Xmp.Schemas.Photoshop.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
Aspose.Imaging.Xmp.Schemas.DublinCore.DublinCorePackage dublinCorePackage = new Aspose.Imaging.Xmp.Schemas.DublinCore.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);

using (var ms = new MemoryStream())
{
// update XMP metadata into image
image.XmpData = xmpData;

// Save image on the disk or in memory stream
image.Save(ms);

ms.Seek(0, System.IO.SeekOrigin.Begin);

// Load the image from moemory stream or from disk to read/get the metadata
using (var img = (TiffImage)Aspose.Imaging.Image.Load(ms))
{
// getting the XMP metadata
Aspose.Imaging.Xmp.XmpPacketWrapper imgXmpData = img.XmpData;
foreach (Aspose.Imaging.Xmp.XmpPackage package in imgXmpData.Packages)
{
// use package data ...
}
}
}
}

//[VB.NET Code Sample]

' Specify the size of image by defining a Rectangle
Dim rect As New Aspose.Imaging.Rectangle(0, 0, 100, 200)

Dim tiffOptions As New TiffOptions(TiffExpectedFormat.TiffJpegRGB)
tiffOptions.Photometric = TiffPhotometrics.MinIsBlack
tiffOptions.BitsPerSample = New UShort() {8}

' create the brand new image just for sample purposes
Using image = New TiffImage(New TiffFrame(tiffOptions, rect.Width, rect.Height))
' create an instance of XMP-Header
Dim xmpHeader As New Aspose.Imaging.Xmp.XmpHeaderPi(System.Guid.NewGuid().ToString())

' create an instance of Xmp-TrailerPi
Dim xmpTrailer As New Aspose.Imaging.Xmp.XmpTrailerPi(True)

' create an instance of XMPmeta class to set different attributes
Dim xmpMeta As New Aspose.Imaging.Xmp.XmpMeta()
xmpMeta.AddAttribute("Author", "Mr Smith")
xmpMeta.AddAttribute("Description", "The fake metadata value")

' create an instance of XmpPacketWrapper that contains all metadata
Dim xmpData As New Aspose.Imaging.Xmp.XmpPacketWrapper(xmpHeader, xmpTrailer, xmpMeta)

' create an instacne of Photoshop package and set photoshop attributes
Dim photoshopPackage As New Aspose.Imaging.Xmp.Schemas.Photoshop.PhotoshopPackage()
photoshopPackage.SetCity("London")
photoshopPackage.SetCountry("England")
photoshopPackage.SetColorMode(Aspose.Imaging.Xmp.Schemas.Photoshop.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
Dim dublinCorePackage As New Aspose.Imaging.Xmp.Schemas.DublinCore.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)

Using ms = New MemoryStream()
' update XMP metadata into image
image.XmpData = xmpData

' Save image on the disk or in memory stream
image.Save(ms)

ms.Seek(0, System.IO.SeekOrigin.Begin)

' Load the image from moemory stream or from disk to read/get the metadata
Using img = DirectCast(Aspose.Imaging.Image.Load(ms), TiffImage)
' getting the XMP metadata
Dim imgXmpData As Aspose.Imaging.Xmp.XmpPacketWrapper = img.XmpData
' use package data ...
For Each package As Aspose.Imaging.Xmp.XmpPackage In imgXmpData.Packages
Next
End Using
End Using
End Using



Overview: Aspose.Imaging for .NET

Aspose.Imaging for .NET is an image processing & manipulation component that allows developers to create, edit, draw or convert images in their .NET application. It allows developers to convert image files to PSD, BMP, JPEG, PNG, TIFF and GIF formats. Moreover a set of pens, brushes and fonts can be used to draw images or add new elements & text to existing images. Aspose.Imaging for .NET works well with both web & windows applications. Moreover, it adds the support for Silverlight platform.

- Homepage of Aspose.Imaging for .NET: http://www.aspose.com/.net/imaging-component.aspx

- Download Aspose.Imaging for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.imaging-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts