Click here to Skip to main content
11,931,104 members (63,244 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked

A VB.NET class useful for color manipulation

, 7 Jul 2003 LGPL3
Rate this:
Please Sign up or sign in to vote.
A VB.NET class for advanced colorimetric image processing and manipulation


Although windows seems to have some color management build into, none of this is found in any of the .NET classes, at least to my knowledge. Similarly, GDI and GDI+ contain almost no provisions for color manipulation or colorimetric image processing (there's is a color matrix, but no convolution or any more advanced image processing. It is also hard to operate on linear-light RGB values instead of gamma-corrected R*G*B* values). It seems Microsoft has no concern for the underlying principles and foundations of image processing and colorimetry, instead going for a simple pragmatic 'look I can adjust brightness or hue' type of approach ... Nothing wrong with that, but we expected more from a such a comprehensive framework as .NET (maybe we DO have to have a look at java and Java Advanced Imaging, or JAI, which seems built on a much more researched foundation). The net result (hehe) is that for my colorimetric imaging research I needed to write a support library, at first in VB6. This library has now been converted to VB.NET.


An introduction to color is useful, check out Charles Poynton page to name but one.

Using the code

The library of mostly shared functions and subroutines and is not written for speed, instead I hope it is clear and easy to read. It makes heavy use of overloading and needs another class called Algebra to run, which is included in the download. Its main use is moving from one color space to another and applying gamma-correction using arrays of color triplets, but it also contains some routines for supporting image processing using unmanaged pointers, and GDI+. The supported color spaces are (a * in the notation means a non-linear light color space, e.g. after gamma-correction):

  • Generic RGB and R*G*B*. User must, obviously, provide a transform to go to other color spaces here as there are literally 100's of RGB color spaces!
  • ITU Rec 709 sRGB and sR*G*B*. A standard RGB color space used on the web, monitors and in printers.
  • CIE XYZ. A human vision based tristimulus color space encompassing all visible colors.
  • CIE L*a*b*. A perceputally uniform color space. In this space the Euclidean metric is proportional to color differences as perceived by a human observer, albeit under a rather restricted set of viewing conditions.

The library also defines several whitepoints

  • CIE D65: Noon daylight simulation with color temperature of 6500 K
  • CIE D55: Daylight simulation with color temperature of 5500 K
  • CIE A: Tungsten illumination with color temperature of 3800 K
  • CIE C: Fluorescent illumination with correlated color temperature about 5200 K
  • CIE D93: Color temperature of 9300 K, similar to some computer monitor
  • HDTVD65: High-definition television whitepoint at 6500 K

Several types of polynomial transforms between color spaces are supported, from a 3x3 linear transform to a highly non-linear, 20-term transform. The class also provides a way to compute this transform, given a set of colors in an input color space, and their desired mappings in an output color space (based on the singular value decomposition solution provided by the algebra class. Note that this routine was converted from 'Numerical Recipes in C', a well known set of numerical routines). The class also allows the use of a lookup-table or LUT to be applied prior to the polynomial transformation.

The simplest is to check out the code in the download

Sample code using the class

Here is a piece of C# code that runs through an image and transforms every pixel using a given polynomial transform and lookup table. This code also demonstrates the use of unmanaged pointers in C#, which greatly improves the speed over using managed GDI+ classes with GetPixel and SetPixel. This is not possible in VB.NET! It is assumed that the Color.vb class has been properly imported.

public static unsafe void PolynomialTransform(Bitmap objBitmap, 
    float[,]Transform, float[,] LUT )
//Do LUT and polynomial transform on whole image ... 
//Use unmanaged pointers to go FAST. In Memory RGB values 
//are stored in the order B-G-R!!!!!! 

int x, y, iPtrPixel0; 

Rectangle rect = new Rectangle(0,0,objBitmap.Width,objBitmap.Height);
System.Drawing.Imaging.BitmapData bitmapData = objBitmap.LockBits(rect, 

iPtrPixel0 = bitmapData.Scan0.ToInt32();

//Byte pointer and corresponding managed array type 
byte * byPixel;
byte[] bySafePixel = new byte[3];

for (y = 0; y < bitmapData.Height; y ++)
  byPixel = (byte *)(iPtrPixel0 + y * bitmapData.Stride);
  for (x = 0; x < bitmapData.Width; x ++)
    bySafePixel[2] = *byPixel;
    bySafePixel[1] = *(byPixel + 1);
    bySafePixel[0] = *(byPixel + 2);

    Color.GammaRGBToGammasRGB(bySafePixel, Transform, LUT);
    *byPixel = bySafePixel[2];
    byPixel ++;
    *byPixel = bySafePixel[1];
    byPixel ++;
    *byPixel = bySafePixel[0];
    byPixel ++;

Points of Interest

No special points of intrest,except that even using unmanaged pointers, image processing in .NET is still pretty sloooow!


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Software Developer (Senior)
Belgium Belgium
Physicist, Biomedical Engineer, Phd in engineering. Specific expertise is in medical photography and it's related image processing, and in colourimetry.

You may also be interested in...

Comments and Discussions

QuestionC Code Pin
stixoffire8-Apr-08 14:19
memberstixoffire8-Apr-08 14:19 
GeneralRe: C Code Pin
yvdh8-Apr-08 21:55
memberyvdh8-Apr-08 21:55 
QuestionHow to use. Pin
Alexandros5413-Sep-07 8:19
memberAlexandros5413-Sep-07 8:19 
AnswerRe: How to use. Pin
yvdh13-Sep-07 13:06
memberyvdh13-Sep-07 13:06 
GeneralRotation of Controls Pin
JK Rajesh6-Jun-07 1:07
memberJK Rajesh6-Jun-07 1:07 
GeneralRe: Rotation of Controls Pin
Vincent DUVERNET (Nolmë Informatique)24-Jun-07 9:06
memberVincent DUVERNET (Nolmë Informatique)24-Jun-07 9:06 
GeneralConvert RGB to CMYK Colors Pin
Metexim8-Apr-06 1:44
memberMetexim8-Apr-06 1:44 
GeneralRe: Convert RGB to CMYK Colors Pin
Vincent DUVERNET (Nolmë Informatique)3-Oct-06 9:34
memberVincent DUVERNET (Nolmë Informatique)3-Oct-06 9:34 
GeneralRe: Convert RGB to CMYK Colors Pin
Vincent DUVERNET (Nolmë Informatique)3-Oct-06 12:10
memberVincent DUVERNET (Nolmë Informatique)3-Oct-06 12:10 
QuestionRe: Convert RGB to CMYK Colors Pin
JK Rajesh5-Jun-07 21:34
memberJK Rajesh5-Jun-07 21:34 
Hello Vincent,
Thank You for your valuable was really of great help.I got a nother problem,i.e,my application needs to implement the Picture Rotation and Text Rotation Functionality as it is in Microsoft Office2003 with Rotation Handles. I need the similar kind of there any user control or an OCX which might help me achieve tht kind of a functionality. the picture should rotate withing the picturebox as we drag the mouse using the Rotation Handle. Hope the question is understood.Looking for a soulution at the earliest.

Than You
Yusuf Shabbir

Generaljust what I am looking for! Pin
ivan 4525-Oct-05 7:12
memberivan 4525-Oct-05 7:12 
GeneralRe: just what I am looking for! Pin
yvdh25-Oct-05 8:17
memberyvdh25-Oct-05 8:17 
GeneralRe: just what I am looking for! Pin
ivan 4526-Oct-05 0:36
memberivan 4526-Oct-05 0:36 
GeneralRe: just what I am looking for! Pin
ivan 4526-Oct-05 0:52
memberivan 4526-Oct-05 0:52 
GeneralRe: just what I am looking for! Pin
yvdh26-Oct-05 0:59
memberyvdh26-Oct-05 0:59 
GeneralRe: just what I am looking for! Pin
ivan 452-Nov-05 1:09
memberivan 452-Nov-05 1:09 
GeneralRe: just what I am looking for! Pin
yvdh8-Dec-08 3:24
memberyvdh8-Dec-08 3:24 
GeneralConstruct YCbCr images Pin
Yawen4-Mar-04 22:08
sussYawen4-Mar-04 22:08 
GeneralRe: Construct YCbCr images Pin
yvdh4-Mar-04 23:37
memberyvdh4-Mar-04 23:37 
GeneralRe: Construct YCbCr images Pin
yyyychan7-Mar-04 22:15
memberyyyychan7-Mar-04 22:15 
Questionsource code link broken ? Pin
BillWoodruff14-Jul-03 23:27
memberBillWoodruff14-Jul-03 23:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 8 Jul 2003
Article Copyright 2003 by yvdh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid