How to Read and Write XMP Data to Images inside .NET Apps
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.
//your code here...//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
Url: http://www.aspose.com/.net/imaging-component.aspx
Language: C# | User: Sheraz Khan | Created: Feb 24, 2016 | Tags: Read XMP Data from Images Write XMP Data to Images Extensible Metadata Platform embedding XMP information write XMP metadata to images Specify size of image .NET image processing