Manipulating JPEG Images

Using ExifData Class to Read and Modify Jpeg EXIF Tags

Almost all digital cameras (including smartphones), scanners and other systems handling image save images with EXIF (Exchangeable Image File) information. Camera settings and scene information are recorded by the camera into the image file. EXIF data also include shutter speed, date and time a photo was taken, focal length, exposure compensation, metering pattern and if a flash was used. Aspose.Imaging APIs has made possible to extract the EXIF information from a given image in a very easy and simple manner. Developers may also write EXIF data to the images or modify the existing information as per their requirement. Aspose.Imaging has provided ExifData class for reading, writing and modifying the EXIF data, where as Aspose.Imaging.Exif.Enums namespace contains the relevant enumerations used in the process.

Reading EXIF Data

Aspose.Imaging APIs provide means to read EXIF data from a given image. Below provided steps illustrate the usage of ExifData class to read the EXIF information from an image.

  1. Load an image into an instance of Image using the factory method Load.
  2. Create and initialize an instance of ExifData class.
  3. Fetch the required information and write it to console.
using Aspose.Imaging;
using Aspose.Imaging.Exif;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Djvu;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Eps;
using Aspose.Imaging.FileFormats.Eps.Consts;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Jpeg2000;
using Aspose.Imaging.FileFormats.Pdf;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Psd;
using Aspose.Imaging.FileFormats.Svg;
using Aspose.Imaging.FileFormats.Tga;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageFilters.FilterOptions;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.Dicom;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
using (JpegImage image = (JpegImage)Image.Load(dataDir + "template.jpg"))
{
JpegExifData exifData = image.ExifData;
if (exifData != null)
{
Type type = exifData.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine(property.Name + ":" + property.GetValue(exifData, null));
}
}
}

Alternatively, developers may also get the specific information using the following code snippet.

using Aspose.Imaging;
using Aspose.Imaging.Exif;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Djvu;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Eps;
using Aspose.Imaging.FileFormats.Eps.Consts;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Jpeg2000;
using Aspose.Imaging.FileFormats.Pdf;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Psd;
using Aspose.Imaging.FileFormats.Svg;
using Aspose.Imaging.FileFormats.Tga;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageFilters.FilterOptions;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.Dicom;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Load an image using the factory method Load exposed by Image class
using (Image image = Image.Load(dataDir + "template.jpg"))
{
// Initialize an object of ExifData and fill it will image's EXIF information
ExifData exif = ((JpegImage)image).ExifData;
// Check if image has any EXIF entries defined and Display a few EXIF entries
if (exif != null)
{
Console.WriteLine("Exif WhiteBalance: " + exif.WhiteBalance);
Console.WriteLine("Exif PixelXDimension: " + exif.PixelXDimension);
Console.WriteLine("Exif PixelYDimension: " + exif.PixelYDimension);
Console.WriteLine("Exif ISOSpeed: " + exif.ISOSpeed);
Console.WriteLine("Exif FocalLength: " + exif.FocalLength);
}
}

Writing & Modifying EXIF Data

Using Aspose.Imaging APIs, developers can write new EXIF information and modify existing EXIF data of an image. Both processes (Writing & Modifying) requires loading of an image and getting the EXIF data into an instance of ExifData class. Then one can access properties exposed by ExifData class to set them accordingly. Sample code to demonstrate the usage is as follow:

using Aspose.Imaging;
using Aspose.Imaging.Exif;
using Aspose.Imaging.Exif.Enums;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Djvu;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Eps;
using Aspose.Imaging.FileFormats.Eps.Consts;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Jpeg2000;
using Aspose.Imaging.FileFormats.Pdf;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Psd;
using Aspose.Imaging.FileFormats.Svg;
using Aspose.Imaging.FileFormats.Tga;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageFilters.FilterOptions;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.Dicom;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Load an image using the factory method Load exposed by Image class
using (Image image = Image.Load(dataDir + "template.jpg"))
{
// Initialize an object of ExifData and fill it will image's EXIF information
JpegExifData exif = ((JpegImage)image).ExifData;
if (exif != null)
{
// Set LensMake, WhiteBalance, Flash information Save the image
exif.LensMake = "Sony";
exif.WhiteBalance = ExifWhiteBalance.Auto;
exif.Flash = ExifFlash.Fired;
image.Save(dataDir + "result.jpg");
}
}
File.Delete(dataDir + "result.jpg");

Creating Thumbnails from JPEG Images

Thumbnails are reduced-size versions of pictures, used to display a significant part of the picture instead of the full frame. Some image files (especially the ones shot with a digital camera) have a thumbnail image embedded in the file. In such cases, Aspose.Imaging API retrieves the embedded thumbnail image and allows you to store it separately on disk. With the release of Aspose.Imaging for .NET 2.3.1, the JpegImage class contains the ExifData.Thumbnail property that can retrieve the thumbnail information from a JPEG image file. The code snippet provided below demonstrates how to use it.

using Aspose.Imaging;
using Aspose.Imaging.Exif;
using Aspose.Imaging.Exif.Enums;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Djvu;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Eps;
using Aspose.Imaging.FileFormats.Eps.Consts;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Jpeg2000;
using Aspose.Imaging.FileFormats.Pdf;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Psd;
using Aspose.Imaging.FileFormats.Svg;
using Aspose.Imaging.FileFormats.Tga;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageFilters.FilterOptions;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.Dicom;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Load an image using the factory method Load exposed by Image class
using (Image image = Image.Load(dataDir + "template.jpg"))
{
image.Resize(100, 100, ResizeType.LanczosResample);
image.Save(dataDir + "result.jpg");
}
File.Delete(dataDir + "result.jpg");

If you wish to generate thumbnails from other image formats such as BMP & PNG, please refer to the Resizing Images.

Adding Thumbnails to JFIF and EXIF Segments of JPEG Images

The release of Aspose.Imaging 2.3.1 enabled developers to create thumbnails from JPEG images using the ExifData.Thumbnail property. Starting from Aspose.Imaging 2.4.0, it is possible to add thumbnails to the JFIF and EXIF segments of JPEG images. There are additional thumbnail properties in the ExifData and Jfif classes, which are of the JpegImage type, that can can be used to store additional thumbnail images inside the original JPEG image.

Add Thumbnail to JFIF Segment

The code snippet below demonstrates how to use the Jfif.Thumbnail property to add a thumbnail image to the JFIF segment of a new JPEG image.

using Aspose.Imaging;
using Aspose.Imaging.Exif;
using Aspose.Imaging.Exif.Enums;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Djvu;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Eps;
using Aspose.Imaging.FileFormats.Eps.Consts;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Jpeg2000;
using Aspose.Imaging.FileFormats.Pdf;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Psd;
using Aspose.Imaging.FileFormats.Svg;
using Aspose.Imaging.FileFormats.Tga;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageFilters.FilterOptions;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.Dicom;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
using (MemoryStream stream = new MemoryStream())
{
JpegImage thumbnailImage = new JpegImage(100, 100);
JpegImage image = new JpegImage(1000, 1000);
image.Jfif = new JFIFData();
image.Jfif.Thumbnail = thumbnailImage;
image.Save(dataDir + "result.jpg");
}
File.Delete(dataDir + "result.jpg");

Thumbnail images with other segment data cannot occupy more than 65,545 bytes because of the JPEG format specifications. In cases where large images are to be set as a thumbnail, exception may arise.

Add Thumbnail to EXIF Segment

The code snippet below demonstrate how to use the ExifData.Thumbnail property to add a thumbnail image to the EXIF segment of a new JPEG image.

using Aspose.Imaging;
using Aspose.Imaging.Exif;
using Aspose.Imaging.Exif.Enums;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Djvu;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Eps;
using Aspose.Imaging.FileFormats.Eps.Consts;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Jpeg2000;
using Aspose.Imaging.FileFormats.Pdf;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Psd;
using Aspose.Imaging.FileFormats.Svg;
using Aspose.Imaging.FileFormats.Tga;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageFilters.FilterOptions;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.Dicom;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
using (MemoryStream stream = new MemoryStream())
{
JpegImage thumbnailImage = new JpegImage(100, 100);
JpegImage image = new JpegImage(1000, 1000);
image.Jfif = new JFIFData();
image.Jfif.Thumbnail = thumbnailImage;
image.Save(dataDir + "result.jpg");
}
File.Delete(dataDir + "result.jpg");

In this case, the Aspose.Imaging API cannot estimate the thumbnail image size, but it can check the size of the entire EXIF data segment. This cannot be bigger than 65,535 bytes.

Using JpegExifData Class to Read and Modify Jpeg EXIF Tags

Aspose.Imaging APIs provide JpegExifData class that is exclusive to Jpeg image formats to retrieve & update EXIF information. This article demonstrates the usage of JpegExifData class to achieve the same. Aspose.Imaging.Exif.JpegExifData class serves as EXIF data container for Jpeg images, and provide means to retrieve standard Jpeg EXIF tags as demonstrated below:

using Aspose.Imaging;
using Aspose.Imaging.Exif;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Djvu;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Eps;
using Aspose.Imaging.FileFormats.Eps.Consts;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Jpeg2000;
using Aspose.Imaging.FileFormats.Pdf;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Psd;
using Aspose.Imaging.FileFormats.Svg;
using Aspose.Imaging.FileFormats.Tga;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageFilters.FilterOptions;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.Dicom;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Load an image using the factory method Load exposed by Image class
using (Image image = Image.Load(dataDir + "template.jpg"))
{
// Initialize an object of ExifData and fill it will image's EXIF information
ExifData exif = ((JpegImage)image).ExifData;
// Check if image has any EXIF entries defined and Display a few EXIF entries
if (exif != null)
{
Console.WriteLine("Exif WhiteBalance: " + exif.WhiteBalance);
Console.WriteLine("Exif PixelXDimension: " + exif.PixelXDimension);
Console.WriteLine("Exif PixelYDimension: " + exif.PixelYDimension);
Console.WriteLine("Exif ISOSpeed: " + exif.ISOSpeed);
Console.WriteLine("Exif FocalLength: " + exif.FocalLength);
}
}

Complete List of EXIF Tags

The above code snippet reads a few EXIF Tags using the properties offered by Aspose.Imaging.Exif.JpegExifData class. Complete list of these properties is available here. Following code will read all the EXIF tags using the System.Reflection.PropertyInfo class.

using Aspose.Imaging;
using Aspose.Imaging.Exif;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Djvu;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Eps;
using Aspose.Imaging.FileFormats.Eps.Consts;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Jpeg2000;
using Aspose.Imaging.FileFormats.Pdf;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Psd;
using Aspose.Imaging.FileFormats.Svg;
using Aspose.Imaging.FileFormats.Tga;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageFilters.FilterOptions;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.Dicom;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
using (JpegImage image = (JpegImage)Image.Load(dataDir + "template.jpg"))
{
JpegExifData exifData = image.ExifData;
if (exifData != null)
{
Type type = exifData.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine(property.Name + ":" + property.GetValue(exifData, null));
}
}
}

Support for JPEG-LS

 Aspose.Imaging for .NET API now provide support for JPEG-LS. The code snippet below demonstrates how to use that support for JPEG-LS image and decode that and save into PNG.

using Aspose.Imaging;
using Aspose.Imaging.Exif;
using Aspose.Imaging.Exif.Enums;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Djvu;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Eps;
using Aspose.Imaging.FileFormats.Eps.Consts;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Jpeg2000;
using Aspose.Imaging.FileFormats.Pdf;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Psd;
using Aspose.Imaging.FileFormats.Svg;
using Aspose.Imaging.FileFormats.Tga;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageFilters.FilterOptions;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.Dicom;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Decoding
using (JpegImage jpegImage = (JpegImage)Image.Load(dataDir + "template.jpg"))
{
JpegOptions jpegOptions = jpegImage.JpegOptions;
// You can read new options:
System.Console.WriteLine("Compression type: {0}", jpegOptions.CompressionType);
System.Console.WriteLine("Allowed lossy error (NEAR): {0}", jpegOptions.JpegLsAllowedLossyError);
System.Console.WriteLine("Interleaved mode (ILV): {0}", jpegOptions.JpegLsInterleaveMode);
// Save the original JPEG-LS image to PNG.
jpegImage.Save(dataDir + "result.png", new PngOptions());
// Save the bottom-right quarter of the original JPEG-LS to PNG
Rectangle quarter = new Rectangle(jpegImage.Width / 2, jpegImage.Height / 2, jpegImage.Width / 2, jpegImage.Height / 2);
jpegImage.Save(dataDir + "result2.png", new PngOptions(), quarter);
}
File.Delete(dataDir + "result.png");
File.Delete(dataDir + "result2.png");

Support for JPEG-LS with CMYK and YCCK

 Aspose.Imaging for .NET API now provide support for CMYK and YCCK color models with JPEG-LS. The code snippet below demonstrates how to use that support for JPEG-LS.

using Aspose.Imaging;
using Aspose.Imaging.Exif;
using Aspose.Imaging.Exif.Enums;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Djvu;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Eps;
using Aspose.Imaging.FileFormats.Eps.Consts;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Jpeg2000;
using Aspose.Imaging.FileFormats.Pdf;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Psd;
using Aspose.Imaging.FileFormats.Svg;
using Aspose.Imaging.FileFormats.Tga;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageFilters.FilterOptions;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.Dicom;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
MemoryStream jpegLsStream = new MemoryStream();
try
{
// Save to CMYK JPEG-LS
using (JpegImage image = (JpegImage)Image.Load(dataDir + "template.jpg"))
{
JpegOptions options = new JpegOptions();
//Just replace one line given below in examples to use YCCK instead of CMYK
//options.ColorType = JpegCompressionColorMode.Cmyk;
options.ColorType = JpegCompressionColorMode.Cmyk;
options.CompressionType = JpegCompressionMode.JpegLs;
// The default profiles will be used.
options.RgbColorProfile = null;
options.CmykColorProfile = null;
image.Save(jpegLsStream, options);
}
// Load from CMYK JPEG-LS
jpegLsStream.Position = 0;
using (JpegImage image = (JpegImage)Image.Load(jpegLsStream))
{
image.Save(dataDir + "result.png", new PngOptions());
}
}
finally
{
jpegLsStream.Dispose();
}
File.Delete(dataDir + "result.png");
using Aspose.Imaging;
using Aspose.Imaging.Exif;
using Aspose.Imaging.Exif.Enums;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Djvu;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Eps;
using Aspose.Imaging.FileFormats.Eps.Consts;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Jpeg2000;
using Aspose.Imaging.FileFormats.Pdf;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Psd;
using Aspose.Imaging.FileFormats.Svg;
using Aspose.Imaging.FileFormats.Tga;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageFilters.FilterOptions;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.Dicom;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
MemoryStream jpegStream = new MemoryStream();
FileStream rgbProfileStream = new FileStream(dataDir + "iccprofiles\\eciRGB_v2.icc", FileMode.Open);
FileStream cmykProfileStream = new FileStream(dataDir + "iccprofiles\\ISOcoated_v2_FullGamut4.icc", FileMode.Open);
Aspose.Imaging.Sources.StreamSource rgbColorProfile = new Aspose.Imaging.Sources.StreamSource(rgbProfileStream);
Aspose.Imaging.Sources.StreamSource cmykColorProfile = new Aspose.Imaging.Sources.StreamSource(cmykProfileStream);
try
{
// Save to JPEG Lossless CMYK
using (JpegImage image = (JpegImage)Image.Load(dataDir + "template.jpg"))
{
JpegOptions options = new JpegOptions();
options.ColorType = JpegCompressionColorMode.Cmyk;
options.CompressionType = JpegCompressionMode.Lossless;
// The custom profiles will be used.
options.RgbColorProfile = rgbColorProfile;
options.CmykColorProfile = cmykColorProfile;
image.Save(jpegStream, options);
}
// Load from JPEG Lossless CMYK
jpegStream.Position = 0;
rgbProfileStream.Position = 0;
cmykProfileStream.Position = 0;
using (JpegImage image = (JpegImage)Image.Load(jpegStream))
{
image.RgbColorProfile = rgbColorProfile;
image.CmykColorProfile = cmykColorProfile;
image.Save(dataDir + "result.png", new PngOptions());
}
}
finally
{
jpegStream.Dispose();
rgbProfileStream.Dispose();
cmykProfileStream.Dispose();
}
File.Delete(dataDir + "result.png");

Support for 2-7 bits per sample in JPEG-LS images

 Aspose.Imaging for .NET API now provide support for 2-7 bits per sample JPEG-LS images. The code snippet below demonstrates how to use that support for JPEG-LS.

using Aspose.Imaging;
using Aspose.Imaging.Exif;
using Aspose.Imaging.Exif.Enums;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Djvu;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Eps;
using Aspose.Imaging.FileFormats.Eps.Consts;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Jpeg2000;
using Aspose.Imaging.FileFormats.Pdf;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Psd;
using Aspose.Imaging.FileFormats.Svg;
using Aspose.Imaging.FileFormats.Tga;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageFilters.FilterOptions;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.Dicom;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
int bpp = 2; // Set 2 bits per sample to see the difference in size and quality
// The origin PNG with 8 bits per sample
string originPngFileName = dataDir + "template.png";
// The output JPEG-LS with 2 bits per sample.
string outputJpegFileName = dataDir + "result.jls";
using (PngImage pngImage = (PngImage)Image.Load(originPngFileName))
{
JpegOptions jpegOptions = new JpegOptions();
jpegOptions.BitsPerChannel = (byte)bpp;
jpegOptions.CompressionType = JpegCompressionMode.JpegLs;
pngImage.Save(outputJpegFileName, jpegOptions);
}
// The output PNG is produced from JPEG-LS to check image visually.
string outputPngFileName = dataDir + "result.png";
using (JpegImage jpegImage = (JpegImage)Image.Load(outputJpegFileName))
{
jpegImage.Save(outputPngFileName, new PngOptions());
}
File.Delete(outputPngFileName);
File.Delete(dataDir + "result.jls");
File.Delete(dataDir + "result.png");

Setting ColorType and CompressionType for JPEG images

 Aspose.Imaging for .NET API now provide support for Color Type and compression Type and set them as gray scale and progressive for JPEG images. The code snippet below demonstrates how to use that support.

using Aspose.Imaging;
using Aspose.Imaging.Exif;
using Aspose.Imaging.Exif.Enums;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Djvu;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Eps;
using Aspose.Imaging.FileFormats.Eps.Consts;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Jpeg2000;
using Aspose.Imaging.FileFormats.Pdf;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Psd;
using Aspose.Imaging.FileFormats.Svg;
using Aspose.Imaging.FileFormats.Tga;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageFilters.FilterOptions;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.Dicom;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
using (var original = Image.Load(dataDir + "template.gif"))
{
var jpegOptions = new JpegOptions()
{
ColorType = JpegCompressionColorMode.Grayscale,
CompressionType = JpegCompressionMode.Progressive,
};
original.Save(dataDir + "result.jpg", jpegOptions);
}
File.Delete(dataDir + "result.jpg");

Convert TIFF to JPEG image

Using Aspose.Imaging for .NET, developers can convert TIFF to JPEG format. This topic explains the approach to load existing TIFF image and convert it to JPEG using JpegOptions class.

The steps to convert TIFF image to JPEG are as simple as below:

  • Create an instance of the TiffImage and load image using Load method of Image class
  • Iterate over the collection of frames of type TiffFrame
  • Create an instance of JpegOptions class and set ResolutionSettings
  • Set the resolution unit explicitly
  • Call TiffFrame.Save method with destination path and an instance JpegOptions

Below provided sample code demonstrate how to convert TIFF to JPEG.

using Aspose.Imaging;
using Aspose.Imaging.Exif;
using Aspose.Imaging.Exif.Enums;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Djvu;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Eps;
using Aspose.Imaging.FileFormats.Eps.Consts;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Jpeg2000;
using Aspose.Imaging.FileFormats.Pdf;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Psd;
using Aspose.Imaging.FileFormats.Svg;
using Aspose.Imaging.FileFormats.Tga;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageFilters.FilterOptions;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.Dicom;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
using (Aspose.Imaging.FileFormats.Tiff.TiffImage tiffImage = (Aspose.Imaging.FileFormats.Tiff.TiffImage)Image.Load(dataDir + "template.tiff"))
{
int i = 0;
foreach (Aspose.Imaging.FileFormats.Tiff.TiffFrame tiffFrame in tiffImage.Frames)
{
Aspose.Imaging.ImageOptions.JpegOptions saveOptions = new Aspose.Imaging.ImageOptions.JpegOptions();
saveOptions.ResolutionSettings = new ResolutionSetting(tiffFrame.HorizontalResolution, tiffFrame.VerticalResolution);
if (tiffFrame.FrameOptions != null)
{
// Set the resolution unit explicitly.
switch (tiffFrame.FrameOptions.ResolutionUnit)
{
case Aspose.Imaging.FileFormats.Tiff.Enums.TiffResolutionUnits.None:
saveOptions.ResolutionUnit = ResolutionUnit.None;
break;
case Aspose.Imaging.FileFormats.Tiff.Enums.TiffResolutionUnits.Inch:
saveOptions.ResolutionUnit = ResolutionUnit.Inch;
break;
case Aspose.Imaging.FileFormats.Tiff.Enums.TiffResolutionUnits.Centimeter:
saveOptions.ResolutionUnit = ResolutionUnit.Cm;
break;
default:
throw new System.NotSupportedException();
}
}
tiffFrame.Save(dataDir + "result.jpg", saveOptions);
}
}
File.Delete(dataDir + "result.jpg");

Memory Strategy optimization

Loading and creating of JPEG images can be proceeded using memory strategy optimization - ie limiting memory buffer size for operation.

using Aspose.Imaging;
using Aspose.Imaging.Exif;
using Aspose.Imaging.Exif.Enums;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Dicom;
using Aspose.Imaging.FileFormats.Djvu;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Eps;
using Aspose.Imaging.FileFormats.Eps.Consts;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Jpeg2000;
using Aspose.Imaging.FileFormats.Pdf;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Psd;
using Aspose.Imaging.FileFormats.Svg;
using Aspose.Imaging.FileFormats.Tga;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageFilters.FilterOptions;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.Dicom;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Setting a memory limit of 50 megabytes for target loaded image
using (Image image = Image.Load(dataDir + "template.jpg", new LoadOptions() { BufferSizeHint = 50 }))
{
image.Save(dataDir + "result.jpg", new JpegOptions { CompressionType = JpegCompressionMode.Baseline, Quality = 100 });
image.Save(dataDir + "result2.jpg", new JpegOptions { CompressionType = JpegCompressionMode.Progressive });
image.Save(dataDir + "result3.jpg", new JpegOptions
{
ColorType = JpegCompressionColorMode.YCbCr,
CompressionType = JpegCompressionMode.Lossless,
BitsPerChannel = 4
});
image.Save(dataDir + "result4.jpg", new JpegOptions
{
ColorType = JpegCompressionColorMode.YCbCr,
CompressionType = JpegCompressionMode.JpegLs,
JpegLsInterleaveMode = JpegLsInterleaveMode.None,
JpegLsAllowedLossyError = 3,
JpegLsPreset = null
});
}
// Setting a memory limit of 50 megabytes for target created image
ImageOptionsBase createOptions = new JpegOptions { CompressionType = JpegCompressionMode.Progressive };
createOptions.BufferSizeHint = 50;
createOptions.Source = new FileCreateSource(dataDir + "result5.jpg", false);
using (var image = Image.Create(createOptions, 1000, 1000))
{
image.Save(); // save to same location
}
File.Delete(dataDir + "result.jpg");
File.Delete(dataDir + "result2.jpg");
File.Delete(dataDir + "result3.jpg");
File.Delete(dataDir + "result4.jpg");
File.Delete(dataDir + "result5.jpg");

Jpeg saved quality estimation

Using Aspose.Imaging you can easily estimate JPEG quality and work with it.

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Jpeg;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
using (JpegImage image = (JpegImage)Image.Load(dataDir + "template.jpg"))
{
int quality = image.JpegOptions.Quality;
// Process quality related tasks
}