Modifying Images
Dithering for Raster Images
Dithering is a technique of creating the illusion of new colors and shades by varying the pattern of dots that actually create an image. It is the most common means of reducing the color range of images down to the 256 (or fewer) colors. Aspose.Imaging provides the dithering support for RasterImage class by introducing Dither method that accepts two parameters. First is of type DitheringMethod to be applied with two possible options FloydSteinbergDithering and ThresholdDithering. The second parameter to Dither method is the BitCount in integer. BitCount defines the sampling size for the dithering result. Default value is 1 that represents black and white, whereas allowed values are 1, 4, 8 generating palettes with 2, 4 and 256 colors respectively.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageOptions; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Create an instance of JpegImage and load an image as of JpegImage | |
using (var image = (JpegImage)Image.Load(dataDir + "template.jpg")) | |
{ | |
// Peform Floyd Steinberg dithering on the current image and Save the resultant image | |
image.Dither(DitheringMethod.ThresholdDithering, 4); | |
image.Save(dataDir + "result.bmp"); | |
} | |
File.Delete(dataDir + "result.bmp"); |
Adjusting Brightness, Contrast and Gamma
Color adjustments in digital images is one of the core features that most of the imaging libraries provide. Color adjustments can be categorized in the following.
- Brightness refers to the lightness or darkness of color. Increasing the brightness of an image lights out all colors whereas decreasing the brightness darkens all colors.
- Contrast refers to making the objects or details within an image more obvious. Increasing the contrast of an image increases the difference between light and dark areas so that the light areas becomes lighter and dark areas becomes darker. Decreasing the contrast will make lighter and darker areas stay approximately the same but the overall image becomes more homogeneous.
- Gamma optimizes the contrast and brightness of the indirect lighting that is illuminating an object in the image.
Adjusting Brightness
Aspose.Imaging for .NET API provide Adjustbrightness method for the RasterImage class that can be used to adjust the Brightness by passing an integer value as parameter. Highest parameter value denotes to a brighter image. Here is the original image and the resultant image for comparison.
Input File | Output File |
---|---|
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageOptions; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Load an image in an instance of Image | |
using (Image image__1 = Image.Load(dataDir + "template.jpg")) | |
{ | |
// Cast object of Image to RasterImage | |
RasterImage rasterImage = (RasterImage)image__1; | |
// Check if RasterImage is cached and Cache RasterImage for better performance | |
if (!rasterImage.IsCached) | |
{ | |
rasterImage.CacheData(); | |
} | |
// Adjust the brightness | |
rasterImage.AdjustBrightness(70); | |
// Create an instance of TiffOptions for the resultant image, Set various properties for the object of TiffOptions and Save the resultant image | |
TiffOptions tiffOptions = new TiffOptions(TiffExpectedFormat.Default); | |
tiffOptions.BitsPerSample = new ushort[] { 8, 8, 8 }; | |
tiffOptions.Photometric = TiffPhotometrics.Rgb; | |
rasterImage.Save(dataDir + "result.tiff", tiffOptions); | |
} | |
File.Delete(dataDir + "result.tiff"); |
Adjusting Contrast
The AdjustContrast method exposed by the RasterImage class can be used to adjust the Contrast of an image by passing a float value as parameter.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageOptions; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Load an image in an instance of Image | |
using (Image image = Image.Load(dataDir + "template.jpg")) | |
{ | |
// Cast object of Image to RasterImage | |
RasterImage rasterImage = (RasterImage)image; | |
// Check if RasterImage is cached and Cache RasterImage for better performance | |
if (!rasterImage.IsCached) | |
{ | |
rasterImage.CacheData(); | |
} | |
// Adjust the contrast | |
rasterImage.AdjustContrast(10); | |
// Create an instance of TiffOptions for the resultant image, Set various properties for the object of TiffOptions and Save the resultant image to TIFF format | |
TiffOptions tiffOptions = new TiffOptions(TiffExpectedFormat.Default); | |
tiffOptions.BitsPerSample = new ushort[] { 8, 8, 8 }; | |
tiffOptions.Photometric = TiffPhotometrics.Rgb; | |
rasterImage.Save(dataDir + "result.tiff", tiffOptions); | |
} | |
File.Delete(dataDir + "result.tiff"); |
Highest parameter value denotes to a higher contrast in the given image. Here is the original image and the resultant image for comparison.
Input File | Output File |
---|---|
Adjusting Gamma
The AdjustGamma method exposed by the RasterImage class has two versions. One of the overloads accept one float value and performs the Gamma correction for red, blue & green channel coefficients. Whereas the other overload accepts three float parameters representing each color coefficient separately. The following code example demonstrates how to AdjustGamma on an image.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageOptions; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Load an image in an instance of Image | |
using (Image image = Image.Load(dataDir + "template.jpg")) | |
{ | |
// Cast object of Image to RasterImage | |
RasterImage rasterImage = (RasterImage)image; | |
// Check if RasterImage is cached and Cache RasterImage for better performance | |
if (!rasterImage.IsCached) | |
{ | |
rasterImage.CacheData(); | |
} | |
// Adjust the contrast | |
rasterImage.AdjustGamma(10); | |
// Create an instance of TiffOptions for the resultant image, Set various properties for the object of TiffOptions and Save the resultant image to TIFF format | |
TiffOptions tiffOptions = new TiffOptions(TiffExpectedFormat.Default); | |
tiffOptions.BitsPerSample = new ushort[] { 8, 8, 8 }; | |
tiffOptions.Photometric = TiffPhotometrics.Rgb; | |
rasterImage.Save(dataDir + "result.tiff", tiffOptions); | |
} | |
File.Delete(dataDir + "result.tiff"); |
Here is the original image and the resultant image for comparison.
Input File | Output File |
---|---|
Blur an Image
This article demonstrates the usage of Aspose.Imaging for .NET to perform Blur effect on an image. Aspose.Imaging APIs have exposed efficient & easy to use methods to achieve this goal. Aspose.Imaging for .NET has exposed the GaussianBlurFilterOptions class to create blur effect on the fly. GaussianBlurFilterOptions class need radius and sigma values to create blur effect on an image. The steps to perform Resize are as simple as below:
- Load an image using the factory method Load exposed by Image class.
- Convert the image into RasterImage.
- Create an instance of GaussianBlurFilterOptions class with default constructor or provide radius and sigma values in the constructor.
- Call the RasterImage.Filter method while specifying rectangle as image bounds and GaussianBlurFilterOptions class instance.
- Save the results.
The following code example demonstrates how to create blur effect on an image.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Load the image | |
using (Image image = Image.Load(dataDir + "template.gif")) | |
{ | |
// Convert the image into RasterImage, Pass Bounds[rectangle] of image and GaussianBlurFilterOptions instance to Filter method and Save the results | |
RasterImage rasterImage = (RasterImage)image; | |
rasterImage.Filter(rasterImage.Bounds, new GaussianBlurFilterOptions(5, 5)); | |
rasterImage.Save(dataDir + "result.gif"); | |
} | |
File.Delete(dataDir + "result.gif"); |
Verify Image Transparency
This article demonstrates the usage of Aspose.Imaging for .NET to check image transparency. The steps to check image transparency are as simple as below:
- Load an image using the factory method Load exposed by Image class.
- Check image opacity if opacity is zero image is transparent.
- The following code example demonstrates how to check image is transparent or not.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (PngImage image = (PngImage)Image.Load(dataDir + "template.png")) | |
{ | |
float opacity = image.ImageOpacity; // opacity = 0,470798 | |
if (opacity == 0) | |
{ | |
// The image is fully transparent. | |
} | |
} |
Exporting Text as Shape While Converting EMF MetaFile
Using Aspose.Imaging for .NET, developers can get text as shapes while converting EMF to SVG format. This topic explains in detail how to convert text into shape while converting EMF to SVG format. Aspose.Imaging for .NET provides the TextAsShapes property to get text as shape while converting EMF metafile. Below is the code demonstration of the said functionality.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (Image image = Image.Load(dataDir + "template.emf")) | |
{ | |
EmfRasterizationOptions emfRasterizationOptions = new EmfRasterizationOptions(); | |
emfRasterizationOptions.BackgroundColor = Color.White; | |
emfRasterizationOptions.PageWidth = image.Width; | |
emfRasterizationOptions.PageHeight = image.Height; | |
image.Save(dataDir + "result.svg", new SvgOptions | |
{ | |
VectorRasterizationOptions = emfRasterizationOptions, | |
TextAsShapes = true | |
}); | |
image.Save(dataDir + "result2.svg", new SvgOptions | |
{ | |
VectorRasterizationOptions = emfRasterizationOptions, | |
TextAsShapes = false | |
}); | |
} | |
File.Delete(dataDir + "result.svg"); | |
File.Delete(dataDir + "result2.svg"); |
Implement Lossy GIF Compressor
Using Aspose.Imaging for .NET, developers can sets pixel difference. GIF’s compression is based on a “dictionary” of strings of pixels seen. Normal encoder searches the dictionary for the longest string of pixels that exactly matches pixels in the image. Lossy encoder picks longest string of pixels that’s “similar enough” to pixels in the image. Below is the code demonstration of the said functionality.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Sets the maximum allowed pixel difference. If greater than zero, lossy compression will be used. | |
// Recommended value for optimal lossy compression is 80. 30 is very light compression, 200 is heavy. | |
GifOptions gifExport = new GifOptions(); | |
gifExport.MaxDiff = 80; | |
using (Image image = Image.Load(dataDir + "template.gif")) | |
{ | |
image.Save(dataDir + "result.gif", gifExport); | |
} | |
File.Delete(dataDir + "result.gif"); |
Resizing WMF file while converting to PNG
Using Aspose.Imaging for .NET, developers can resize the WMF metafile while converting it to raster format. This topic explains the approach to load existing metafiles, resize it and convert it to raster format. Aspose.Imaging for .NET provides the Image class to load WMF files and same can be used to resize and save the image to PNG format. The following code snippet shows you how to resize the WMF file while converting it to PNG.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Load an existing WMF image | |
using (Image image = Image.Load(dataDir + "template.emf")) | |
{ | |
// Call the resize method of Image class and width,height values and Calculate new PNG image height | |
image.Resize(100, 100); | |
double k = (image.Width * 1.00) / image.Height; | |
// Create an instance of EmfRasterizationOptions class and set different properties | |
EmfRasterizationOptions emfRasterization = new EmfRasterizationOptions | |
{ | |
BackgroundColor = Color.WhiteSmoke, | |
PageWidth = 100, | |
PageHeight = (int)Math.Round(100 / k), | |
BorderX = 5, | |
BorderY = 10 | |
}; | |
// Create an instance of PngOptions class and provide rasterization option | |
ImageOptionsBase imageOptions = new PngOptions(); | |
imageOptions.VectorRasterizationOptions = emfRasterization; | |
// Call the save method, provide output path and PngOptions to convert the WMF file to PNG and save the output | |
image.Save(dataDir + "result.png", imageOptions); | |
} | |
File.Delete(dataDir + "result.png"); |
Support For BMP
Using Aspose.Imaging for .NET, developers can load the BMP OS22XBITMAPHEADER. This header contains information specific to the bitmap data.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (Image image = Image.Load(dataDir + "template.bmp")) | |
{ | |
image.Save(dataDir + "result.png", new PngOptions()); | |
} | |
File.Delete(dataDir + "result.png"); |
Support For DIB
Aspose.Imaging now supports the Device-Independent Bitmap files. DIB file is similar to a BMP file, but has different header information. In the example below, an existing DIB file is loaded by passing the file path to the Image class static Load method.
The following code snippet shows you how to load a DIB file and convert it to PNG.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (Image image = Image.Load(dataDir + "template.dib")) | |
{ | |
image.Save(dataDir + "result.png", new PngOptions()); | |
} | |
File.Delete(dataDir + "result.png"); |
Support For Reading Pixel values of 48 bpp
Using Aspose.Imaging for .NET, developers can load 16-bit color components from TIFF RGB 48Bpp. Please note that only TIFF with the following options are supported:
- Color model: RGB 48Bpp or RGBA 64Bpp
- Compression: Lzw, Deflate, Uncompressed
- Byte order: Intel (Little Endian), Motorola (Big Endian).
- Striped/Tiled: Only Striped
- Planar Config: Contiguous, Separate
Embedded ICC Profile is not applied for 16-bit color components. Below is the code demonstration of the said functionality.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// ICC profile is not applied for 16-bit color components at the moment, so disable that option explicitly. | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.UseIccProfileConversion = false; | |
Rectangle desiredArea = new Rectangle(470, 1350, 30, 30); | |
using (RasterImage image = (RasterImage)Image.Load(dataDir + "template.tiff", loadOptions)) | |
{ | |
int[] colors32Bit = image.LoadArgb32Pixels(image.Bounds); | |
} |
Support For Change Window Size
Using Aspose.Imaging for .NET, developers can now change window size in Binarize Bradley method. Below is the code demonstration of the said functionality.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (PngImage image = (PngImage)Image.Load(dataDir + "template.png")) | |
{ | |
image.BinarizeBradley(10, 20); | |
image.Save(dataDir + "result.png"); | |
} | |
File.Delete(dataDir + "result.png"); |