Getting Started with BatchImagePro

BatchImagePro is a simple to use Batch Image Processing software library for C# .net developers who develop applications and utilities for Windows and Linux. The following pre-requisites are necessary before the downloadable coding examples can be run:

After downloading and unzipping the source, Visual Studio users will need to navigate to and open the .sln file.

Visual Studio Code users should open a command terminal window and navigate to the project folder (where the .csproj file resides) and enter code at the command prompt. To build and run the source, enter the following at the command prompt whilst in the project folder:

> dotnet build
> dotnet bin/Debug/netcoreapp2.2/<name of dll>

Before building and running the source code, set the folder location of your images and the destination folder in program.cs file. When executing the code using the free edition of BatchImagePro, please ensure your images are within the size limits permitted for this edition.

Online developer help is available for the BatchImagePro library that covers the different data types and methods and properties.

Free, Personal and Commercial Editions

When BatchImagePro is used without a licence file, it operates in free edition mode. A valid licence file is required to take advantage of the benefits offered by the personal and commercial editions. The code snippet below demonstrates how to reference a licence file:

using CohesiveComputing.Imaging;

namespace Batch_Image_Pro_Sample_Code
{
    class Program
    {
        static void Main(string[] args)
        {
            // The licence file can be read using a relative path, full path or a stream.
            // To use BatchImagePro in free edition mode, comment the line below.
            BatchImagePro.SetLicence("licence.xml");
        }
    }
}

Loading and Processing Images

BatchImagePro is able to process either individual images or a batch of images. To load a single image, the BitmapImage.Load() method is used:

public void Run_AutoContrast_Example(string sourceFilepath, string destFilepath)
{
	var image = BitmapImage.Load(sourceFilepath);
	var newImage = image.AutoContrast();
	newImage.Save(destFilepath);
}

To process a batch of images, either the ForEachImage() or StartWorkFlow() instance methods of  class BatchProcessor are used:

public void Run_AutoContrast_Batch_Example(string sourceDir, string destDir)
{
	BatchProcessor batchProcessor = BatchImagePro.CreateBatchProcessor(0);

	//batchProcessor.Progress += bp_Progress;
	//batchProcessor.Error += bp_Error;

	batchProcessor.ForEachImage(sourceDir, destDir, "-contrast", p => p.Image.AutoContrast());
	batchProcessor.Dispose();
}

The optional events Progress and Error are used to track which images have been processed, and which caused errors.

Image EXIF Meta Data

To read and write image meta data using BatchImagePro, use the MetaData property of an image. The saving of image meta data is currently supported on Windows only:

public void Run_Exif_Extract_Example (string sourceFilepath)
{
	var image = BitmapImage.Load(sourceFilepath);

	// Refer to https://www.exiv2.org/tags.html for tag identifiers.

	// Should check existence of each tag id using ContainsKey,
	// e.g if (image.MetaData.ContainsKey(nnn)) ...
	var imageDate = image.MetaData[306].GetString();
	var deviceModel = image.MetaData[272].GetString();
	var fstop = image.MetaData[33437].GetUInt32Pair();
	var iso = image.MetaData[34855].GetUInt16();

	// Gets all image meta data
	var delimitedString = image.MetaData.ToDelimitedString();

	// Modify meta data example
	var newFstop = new ImgPropertyItem(33437, new Tuple<UInt32, UInt32>(56, 10) );
	image.MetaData.Set(newFstop);
}

To dump all image meta data to a file, use the ToDelimitedString() method of the MetaData property. This can be useful when seeking to determine the data type of meta data values.

Thumbnail Generation

Thumbnail images can be generated by scaling down images with the Resize() method using box sample interpolation. The same technique can be applied for generating images for displaying on web pages or for sending by email.

public void Run_ThumbNails_Batch_Example(string sourceDir, string destDir)
{
	BatchProcessor batchProcessor = BatchImagePro.CreateBatchProcessor(0);
	batchProcessor.ForEachImage(sourceDir, destDir, "-thumbnail", p => ResizedToThumbnail(p.Image));
	batchProcessor.Dispose();
}

private IBitmapImage ResizedToThumbnail (IBitmapImage image)
{
	int maxXDimension = 120, maxYDimension = 80;

	bool isLandscape = image.Width > image.Height;
	bool resized = false;

	int newWidth = image.Width, newHeight = image.Height;

	if (isLandscape)
	{
		if (image.Width > maxXDimension)
		{
			newWidth = maxXDimension;
			newHeight = (image.Height * maxXDimension) / image.Width;
			resized = true;
		}
	}
	else
	{
		if (image.Height > maxYDimension)
		{
			newHeight = maxYDimension;
			newWidth = (image.Width * maxYDimension) / image.Height;
			resized = true;
		}
	}
	var newImg = image;

	if (resized)
	{
		newImg = image.Resize(newWidth, newHeight, ResizeInterpolation.BoxSample);
	}
	return newImg;
}

Image Watermarking

With BatchImagePro, watermarking is achieved by blending one image over another using the Paste() method. The example below watermarks a batch of images using a dynamically generated text image:

public void Run_Watermark_Batch_Example(string sourceDir, string destDir)
{
	BatchProcessor batchProcessor = BatchImagePro.CreateBatchProcessor(0);

	var textImage = BitmapImage.DrawText("<i>This</i> <u>is</u> a <b>water mark</b>.", "Arial", 72, 0xffffff, 0x000000, 1)
		.Rotate(30, 0x000000, Interpolation.Bilinear);

	batchProcessor.ForEachImage(sourceDir, destDir, "-watermarked", p => WaterMarked(p.Image, textImage));
	batchProcessor.Dispose();
}


private IBitmapImage WaterMarked(IBitmapImage image, IBitmapImage textImage)
{
	var newImg = image.Copy(); // it's not necessary to clone the original image.

	// may need to resize text image if it is larger than image to be water marked.
	newImg.Paste(textImage, (image.Width - textImage.Width) / 2, (image.Height - textImage.Height) / 2, .5f, true);
	return newImg;
}

Downloads

BatchImagePro library downloads:

Please check this page frequently for updates with additional examples.

Leave a Reply

Your email address will not be published. Required fields are marked *