C# Imaging Library
How to process image colors in C# application?
How to process image colors using XImage.Raster C# image library in C# ASP.NET Core, MVC, Windows Forms, WPF applications?
XImage.Raster C# Image library has a rich set of color processing functions, all of which modify the color attributes
(hue, saturation, brightness, contrast, color space, etc.) of images.
How to process image colors using C#?
Here are the main categories:
- Basic Color Adjustment (Brightness/Contrast/Saturation)
- Color Space Conversion
- Gamma Correction & White Balance
- Histogram & Dynamic Range Adjustment
- Color Correction & Tinting
- Special Color Processing
Basic Color Adjustment (Brightness/Contrast/Saturation)
These are the most frequently used color adjustment commands, directly modifying the visual perception of color:
Brightness & Contrast
Using XImage.Raster C# imaging library, you can easily adjust image brightness and contrast (values range from -100 to +100) in your C# ASP.NET Core, MVC, Windows Forms, WPF web and desktop applications.
The following C# code, we try to increase the image brightness by 20 and contrast by 10 using method
BrightnessContrastImage.
- Load an image with
RasterImage object.
- Create an image processor with
ImageProcess object.
- Call the method
BrightnessContrastImage of ImageProcess object to complete the task flopping image.
- Save the modified image to an image file on the disk.
RasterImage img = new RasterImage("C://input//raster-arrow-down.png");
ImageProcess processor = new ImageProcess(img);
processor.BrightnessContrastImage(50, 50);
img.Save("C://output//raster-image-color-brightness-contrast.png");
Compare the source image and image with brightness & contrast applied
| Source image
|
Image with brightness & contrast applied
|
|
|
Modulate
You can use method
ModulateImage to adjust an image's brightness, saturation, and hue in
C# application.
Modulate function will adjust the three core color properties of an image. All three values are percentages, the default is 100 for all three.
- Brightness. Controls lightness/darkness.
- Saturation. Controls color intensity. 0 is grayscale (no color). 50 is muted colors. 200 is double saturation.
- Hue. Shifts the color wheel.
RasterImage img = new RasterImage("C://input//raster-image-sample-beach-tree.png");
ImageProcess processor = new ImageProcess(img);
processor.ModulateImage(150, 120, 180);
img.Save("C://output//raster-image-color-modulate.png");
Compare the source image and image with brightness & contrast applied
| Source image
|
Image with brightness & contrast applied
|
|
|
Color Space Conversion
Using XImage.Raster C# library, you can easily convert image's color space between different color models, including
RGB, HSV, HSL, CMYK, Lab, Gray (grayscale), etc.
RasterImage img = new RasterImage("C://input//raster-image-sample-beach-tree.png");
ImageProcess processor = new ImageProcess(img);
processor.TransformColorspace(ColorSpace.GRAY);
img.Save("C://output//raster-image-color-space-gray.png");
Compare the source image and image in gray.
| Source image
|
Image color space converted to Gray.
|
|
|
Gamma Correction & White Balance
Correct color deviation caused by display/lighting:
Gamma
The Gamma function
GammaImage in XImage.Raster is to adjust image brightness and contrast.
Gamma correction adjusts the non-linear relationship between pixel values and perceived brightness.
- Gamma value
> 1.0: Darkens midtones (image gets darker)
- Gamma value
= 1.0: No change (original image)
- Gamma value
< 1.0: Brightens midtones (image gets brighter)
RasterImage img = new RasterImage("C://input//raster-image-sample-beach-tree.png");
ImageProcess processor = new ImageProcess(img);
processor.GammaImage(0.5);
img.Save("C://output//raster-image-color-gamma.png");
Compare the source image and image with gamma correction applied.
| Source image
|
Image with Gamma correction
|
|
|
Auto Gamma
XImage.Raster library provides an automatic, one line gamma function, which will
it analyzes your image's pixel intensity distribution and calculates the optimal gamma value.
Call method
ImageProcess.AutoGammaImage() to process and apply the gamma correction automatically in
the C# projects.
RasterImage img = new RasterImage("C://input//raster-image-sample-beach-tree.png");
ImageProcess processor = new ImageProcess(img);
processor.AutoGammaImage();
img.Save("C://output//raster-image-color-auto-gamma.png");
Compare the source image and image with gamma automatically applied.
| Source image
|
Image with Gamma correction
|
|
|
Histogram & Dynamic Range Adjustment
Optimize color distribution to improve image layering:
Auto level
Using XImage.Raster C# image library, Auto Level function is one of the most basic but powerful auto-adjustments for fixing flat, low-contrast images.
It maximizes an image's dynamic range by stretching its pixel intensity values to span the full range of black (0) to white (255).
In simple words, the auto level image process will stretch the dynamic range of image colors (map the darkest pixel to black, brightest to white).
Use method
ImageProcess.AntoLevelImage() to apply the Auto Level in C# class.
RasterImage img = new RasterImage("C://input//raster-image-flower.png");
ImageProcess processor = new ImageProcess(img);
processor.AntoLevelImage();
img.Save("C://output//raster-image-color-auto-level.png");
Compare the source image and image with auto level applied
| Source image
|
Image with Auto Level
|
|
|
Equalize
Adjusts contrast without changing the brightness range of the image, thus more detail can be seen in areas that have very similar value.
Call method
ImageProcess.EqualizeImage() to apply the image color Equalize processing in C# class.
RasterImage img = new RasterImage("C://input//raster-image-flower.png");
ImageProcess processor = new ImageProcess(img);
processor.EqualizeImage();
img.Save("C://output//raster-image-color-equalize.png");
Compare the source image and image with Equalize applied
| Source image
|
Image with Equalize
|
|
|
Color Correction & Tinting
Modify specific color tones or correct color casts:
Colorize
Image colorize function is to tint an entire image with a specific color.
Use method
ColorizeImage to apply the colorize function with two parameters
- Opacity From 0 to 100. 0 means no tint (original image), 100 means full color (image becomes solid color, no original details).
- Color Tinting color in RGB.
RasterImage img = new RasterImage("C://input//raster-image-flower.png");
ImageProcess processor = new ImageProcess(img);
processor.ColorizeImage(25, Color.Blue);
img.Save("C://output//raster-image-color-colorize.png");
Compare the source image and image with Colorize (tint with blue color) applied.
| Source image
|
Image with Colorize blue color tinted
|
|
|
Level
In XImage.Raster C# imaging library, funtion
Level is a manual tool to adjust black point, white point, and midtones (gamma) with percentage values.
Adjust the levels of the image by scaling the colors falling between specified white and black points to the full available quantum range
Use method
LevelImage to apply the Level function with three parameters:
- Black Point Valid value from 0 to 1. The intensity value that becomes pure black (0)
- White Point Valid value from 0 to 1. The intensity value that becomes pure white (255).
- Gamma Gamma correction.
< 1.0 darken the image; >= 1.0 lighten the image
RasterImage img = new RasterImage("C://input//raster-image-flower.png");
ImageProcess processor = new ImageProcess(img);
processor.LevelImage(0.5, 0.5, 5);
img.Save("C://output//raster-image-color-level.png");
Compare the source image and image with Level function applied.
| Source image
|
Image with Level applied
|
|
|
Special Color Processing
Sepia tone
Make images like achieved in a photo darkroom. The image process function will do the following jobs on the applied images
- Convert the image to grayscale
- Apply a warm brown tint across the whole image
- Preserve brightness and contrast
- The percentage controls how strong the sepia is:
0 = no sepia (original image)
0.8 = classic strong sepia
1 = full sepia effect
RasterImage img = new RasterImage("C://input//raster-image-sample-beach-tree.png");
ImageProcess processor = new ImageProcess(img);
processor.SepiaToneImage(0.2);
img.Save("C://output//raster-image-color-sepia-tone.png");
Posterize
Simulate a poster effect. Posterize image processing will reduce the number of brightness/color levels in the image.
Use method
PosterizeImage to apply image Posterize in C# application.
- Levels Number of discrete color/intensity steps
- Dither
RasterImage img = new RasterImage("C://input//raster-image-sample-beach-tree.png");
ImageProcess processor = new ImageProcess(img);
processor.PosterizeImage(3, false);
img.Save("C://output//raster-image-color-posterize.png");
Compare the source image and image with Posterize function applied.
| Source image
|
Image with Posterize applied
|
|
|