Extract Metadata from Images using C#

In simple terms, if you work with C# programming, you can use code to do things like adding, changing, deleting, or taking out information about images, like their size, shape, brand, model, and more, which is saved as data about the image.

As a C# programmer, you can also use code to get and change this image data when it’s stored in the cloud. This article will teach you how to do this using a REST API in C#.

The following topics are discussed/covered in this article:

Document Metadata Manipulation REST API and .NET SDK

For manipulating the metadata of JPEG images, I will be using the .NET SDK of GroupDocs.Metadata Cloud API. It allows you to add, edit, retrieve, and remove metadata properties from documents and image file formats. You just need to define the search criteria and the metadata Cloud REST API will take care of the specified metadata operations within supported file formats. It also provides Java SDK as its document metadata manipulation family members for the Cloud API.

You can install GroupDocs.Metadata to your Visual Studio project from the NuGet Package Manager or using the following command in the Package Manager console:

Install-Package GroupDocs.Metadata-Cloud

Please get your Client ID and Client Secret from the dashboard before you start following the steps and available code examples. Once you have your client ID and Secret, add in the code as shown below:

string clientID = "112f0f38-9dae-42d5-b4fc-cc84ae644972";
string clientSecret = "16ad3fe0bdc39c910f57d2fd48a5d618";
string myStorage = "";
Configuration configuration = new Configuration(clientID, clientSecret);
configuration.ApiBaseUrl = "https://api.groupdocs.cloud";

Add Metadata to Images using a REST API in C#

You can add metadata to images by following the simple steps given below:

Upload the Image

Firstly, upload the JPEG file on the Cloud using the code sample given below:

// api initialization
FileApi fileApi = new FileApi(configuration);
string path = @"C:\Files";
var file = Directory.GetFiles(path, "input.jpg", SearchOption.AllDirectories).FirstOrDefault();
if (file.Length != 0)
{
var relativeFilePath = file.Replace(path, string.Empty).Trim(Path.DirectorySeparatorChar);
var fileStream = File.Open(file, FileMode.Open);
fileApi.UploadFile(new UploadFileRequest(relativeFilePath, fileStream, myStorage));
fileStream.Close();
}

As a result, the uploaded JPEG image file will be available in the files section of your dashboard on the cloud.

Add Metadata to Image using C#

You can add metadata to the JPEG image programmatically by following the steps given below.

  • Create an instance of MetadataApi
  • Set the JPEG image file path in the FileInfo model
  • Define AddOptions
  • Set Value and SearchCriteria for the property using the AddProperty model
  • Create AddRequest with AddOptions
  • Get results by calling the MetadataApi.Add() method

The following code sample shows how to add metadata to a JPEG image using a REST API.

// api initialization
var apiInstance = new MetadataApi(configuration);
// input image path
var fileInfo = new GroupDocs.Metadata.Cloud.Sdk.Model.FileInfo
{
FilePath = "input.jpg"
};
// define metadata add options
var options = new AddOptions
{
FileInfo = fileInfo,
Properties = new List<AddProperty>
{
new AddProperty
{
Value = "All rights reserved.",
Type = "String",
SearchCriteria = new SearchCriteria
{
TagOptions = new TagOptions
{
PossibleName = "Copyright"
}
},
}
}
};
// add request
var request = new AddRequest(options);
var response = apiInstance.Add(request);
Add Metadata from Images using a REST API in C#

Add Metadata from Images using a REST API in C#

Download the Image

The above code samples will save the updated JPEG file on the cloud and can be downloaded using the following code sample:

// api initialization
var fileApi = new FileApi(configuration);
// default path
var file = "metadata\\add_metadata\\input_jpg\\input.jpg";
// download request
var downloadRequest = new DownloadFileRequest(file, myStorage);
Stream downloadResponse = fileApi.DownloadFile(downloadRequest);
using (var fileStream = File.Create("C:\\Files\\input.jpg"))
{
downloadResponse.Seek(0, SeekOrigin.Begin);
downloadResponse.CopyTo(fileStream);
}

Update Metadata of Image using C#

You can update the metadata of the JPEG image programmatically by following the steps given below.

  • Create an instance of MetadataApi
  • Set the JPEG image file path in the FileInfo model
  • Define the SetOptions
  • Set NewValue and SearchCriteria for the property using the SetProperty model
  • Create SetRequest with SetOptions
  • Get results by calling the MetadataApi.Set() method

The following code sample shows how to set metadata of a JPEG image using a REST API. Please follow the steps mentioned earlier to upload and download a file.

// api initialization
var apiInstance = new MetadataApi(configuration);
// input image path
var fileInfo = new GroupDocs.Metadata.Cloud.Sdk.Model.FileInfo
{
FilePath = "input.jpg"
};
// define metadata set options
var options = new SetOptions
{
FileInfo = fileInfo,
Properties = new List<SetProperty>
{
new SetProperty
{
NewValue = "Copyright 2021",
SearchCriteria = new SearchCriteria
{
TagOptions = new TagOptions
{
PossibleName = "Copyright"
}
},
Type = "String"
}
}
};
// update request
var request = new SetRequest(options);
var response = apiInstance.Set(request);
Update Metadata from Images using a REST API in C#

Update Metadata from Images using a REST API in C#

Remove Metadata from Image using C#

You can remove metadata from the JPEG image programmatically by following the steps given below.

  • Create an instance of MetadataApi
  • Set the JPEG image file path in the FileInfo model
  • Define RemoveOptions
  • Set the search criteria
  • Create RemoveRequest with RemoveOptions
  • Get results by calling the MetadataApi.Remove() method

The following code sample shows how to remove metadata from a JPEG image using a REST API. Please follow the steps mentioned earlier to upload and download a file.

// api initialization
var apiInstance = new MetadataApi(configuration);
// input image path
var fileInfo = new GroupDocs.Metadata.Cloud.Sdk.Model.FileInfo
{
FilePath = "input.jpg"
};
// define metadata remove options
var options = new RemoveOptions
{
FileInfo = fileInfo,
SearchCriteria = new SearchCriteria
{
TagOptions = new TagOptions
{
PossibleName = "Copyright"
}
}
};
// remove request
var request = new RemoveRequest(options);
var response = apiInstance.Remove(request);
Remove Metadata from Images using a REST API in C#

Remove Metadata from Images using a REST API in C#

Extract Metadata from Image using C#

You can extract the metadata from the JPEG image programmatically by following the steps given below.

  • Create an instance of MetadataApi
  • Set the JPEG image file path in the FileInfo model
  • Define ExtractOptions
  • Create ExtractRequest with ExtractOptions
  • Get results by calling the MetadataApi.Extract() method

The following code sample shows how to extract metadata from a JPEG image using a REST API. Please follow the steps mentioned earlier to upload a file.

// api initialization
var apiInstance = new MetadataApi(configuration);
// source file
var fileInfo = new GroupDocs.Metadata.Cloud.Sdk.Model.FileInfo
{
FilePath = "input.jpg"
};
// extract optioins
var options = new ExtractOptions
{
FileInfo = fileInfo
};
// extract request
var request = new ExtractRequest(options);
var response = apiInstance.Extract(request);
// extracted metadata
if (response.MetadataTree.InnerPackages != null)
{
var innerPackages = response.MetadataTree.InnerPackages;
for (int i = 0; i < innerPackages.Count; i++)
{
Console.WriteLine($"\nPackage: {innerPackages[i].PackageName}");
var packageProperties = innerPackages[i].PackageProperties;
for (int j = 0; j < packageProperties.Count; j++)
{
Console.WriteLine(packageProperties[j].Name + " : " + packageProperties[j].Value);
}
}
}
Image Metadata

Image Metadata

The above code sample will produce the following output:

Package: FileFormat
FileFormat : 9
MimeType : image/jpeg
ByteOrder : 1
Width : 480
Height : 360

Package: Xmp
http://ns.microsoft.com/photo/1.0/ :

Package: Exif
Exif.GpsIfd :
Exif.ExifIfd :
Make : Canon
Model : Canon PowerShot S40
Orientation : System.Int32[]
XResolution : System.Double[]
YResolution : System.Double[]
ResolutionUnit : System.Int32[]
DateTime : 2003:12:14 12:01:44
YCbCrPositioning : System.Int32[]
ExifIfd : System.Int64[]
Exif.Thumbnail : System.Byte[] 

Try Online

Please try the following JPEG Metadata manipulation free online tool, which is developed using the above API. https://products.groupdocs.app/metadata/jpeg

Conclusion

In this article, you have acquired knowledge on various aspects of working with image metadata in the cloud. You’ve learned how to seamlessly incorporate functions such as adding, editing, removing, and extracting metadata from images. Additionally, the article has elucidated the process of programmatically uploading a JPEG image file to the cloud and subsequently downloading it.

For a deeper understanding and comprehensive utilization of the GroupDocs.Metadata Manipulation Cloud API, we recommend referring to our extensive documentation. We’ve also curated an API Reference section that enables you to explore and interact with our APIs directly within your web browser.

Should you encounter any uncertainties or require further assistance, please don’t hesitate to reach out to us via our dedicated forum. We are here to assist you.

See Also