How to adjust an image
How to adjust an image
To adjust an image brightness use the AdjustBrightness method and set a brightness parameter between -255 and 255 to perform corrections. The same you can do with AdjustContrast method to make contrast corrections with parameter settings to a range from -100 and 100. If the image has a color hue, you can make gamma corrections by setting the Red, Green and Blue components coefficient or a general gamma coefficient with AdjustGamma method:
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Jpeg2000; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Masking; | |
using Aspose.Imaging.Masking.Options; | |
using Aspose.Imaging.Masking.Result; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
string templatesFolder = @"c:\Users\USER\Downloads"; | |
AdjustBrightness(); | |
void AdjustGammaRGB() | |
{ | |
FilterImages(image => | |
{ | |
//https://apireference.aspose.com/imaging/net/aspose.imaging.rasterimage/adjustgamma/methods/1 | |
image.AdjustGamma(5, 0.1f, 0.1f); | |
}, "adjustgammargb"); | |
} | |
void AdjustGamma() | |
{ | |
FilterImages(image => | |
{ | |
//https://apireference.aspose.com/imaging/net/aspose.imaging/rasterimage/methods/adjustgamma | |
image.AdjustGamma(3); | |
}, "adjustgamma"); | |
} | |
void AdjustContrast() | |
{ | |
FilterImages(image => | |
{ | |
//https://apireference.aspose.com/imaging/net/aspose.imaging/rasterimage/methods/adjustcontrast | |
image.AdjustContrast(50); | |
}, "adjustcontrast"); | |
} | |
void AdjustBrightness() | |
{ | |
FilterImages(image => | |
{ | |
//https://apireference.aspose.com/imaging/net/aspose.imaging/rasterimage/methods/adjustbrightness | |
image.AdjustBrightness(100); | |
}, "adjustbrightness"); | |
} | |
void FilterImages(Action<RasterImage> doFilter, string filterName) | |
{ | |
List<string> rasterFormats = new List<string>() { "jpg", "png", "bmp", "apng", "dicom", | |
"jp2", "j2k", "tga", "webp", "tif", "gif", "ico" }; | |
List<string> vectorFormats = new List<string>() { "svg", "otg", "odg", "eps", "wmf", "emf", "wmz", "emz", "cmx", "cdr" }; | |
List<string> allFormats = new List<string>(rasterFormats); | |
allFormats.AddRange(vectorFormats); | |
allFormats.ForEach( | |
formatExt => | |
{ | |
var inputFile = Path.Combine(templatesFolder, $"template.{formatExt}"); | |
bool isVectorFormat = vectorFormats.IndexOf(formatExt) > -1; | |
//Need to rasterize vector formats before background remove | |
if (isVectorFormat) | |
{ | |
inputFile = RasterizeVectorImage(formatExt, inputFile); | |
} | |
var outputFile = Path.Combine(templatesFolder, $"{filterName}_{formatExt}.png"); | |
Console.WriteLine($"Processing {formatExt}"); | |
using (var image = (RasterImage)Image.Load(inputFile)) | |
{ | |
doFilter(image); | |
//If image is multipage save each page to png to demonstrate results | |
if (image is IMultipageImage multiPage && multiPage.PageCount > 1) | |
{ | |
for (var pageIndex = 0; pageIndex < multiPage.PageCount; pageIndex++) | |
{ | |
string fileName = $"{filterName}_page{pageIndex}_{formatExt}.png"; | |
multiPage.Pages[pageIndex].Save(templatesFolder + fileName, new PngOptions()); | |
File.Delete(templatesFolder + fileName); | |
} | |
} | |
else | |
{ | |
image.Save(outputFile, new PngOptions()); | |
File.Delete(outputFile); | |
} | |
} | |
//Remove rasterized vector image | |
if (isVectorFormat) | |
{ | |
File.Delete(inputFile); | |
} | |
} | |
); | |
} | |
string RasterizeVectorImage(string formatExt, string inputFile) | |
{ | |
string outputFile = Path.Combine(templatesFolder, $"rasterized.{formatExt}.png"); | |
using (var image = Image.Load(inputFile)) | |
{ | |
image.Save(outputFile, new PngOptions()); | |
} | |
return outputFile; | |
} |