C#
  Home arrow C# arrow Page 2 - Performing Color Transformation Operations...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Performing Color Transformation Operations in C# GDI+
By: Xianzhong Zhu
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-07-23

    Table of Contents:
  • Performing Color Transformation Operations in C# GDI+
  • Scaling Color Operation
  • Rotating Color Operation
  • Shearing Color Operation

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Performing Color Transformation Operations in C# GDI+ - Scaling Color Operation


    (Page 2 of 4 )

    A scaling transformation corresponds to the matrix multiplication. During the scaling operation, each of the four color components of a color is multiplied by a specified number. The following Figure 4 gives the associated transformation matrix.


    Figure 4-the color matrix entries that represent scaling transformation

    It's worth noticing that the scaling transformation will not influence the color saturation.

    Now, let's also construct a sample to see how the scaling transformation will affect the changing colors. The following example constructs two Image objects from the original head.bmp file. Figure 5 gives the related running-time snapshot of the sample.


    Figure 5-the running-time snapshot of the scaling transformation sample

    Next, let's do some research into the source code, as listed below.

    Graphics graphics=this.CreateGraphics();

    graphics.Clear(Color.White);


    //load the original image

    Bitmap image = new Bitmap("head.bmp");

    ImageAttributes imageAttributes = new ImageAttributes();

    int width = image.Width;

    int height = image.Height;


    //define the first color transformation matrix

    float[][] colorMatrixElements=

    {

    new float[]{0.5f, 0.0f, 0.0f, 0.0f, 0.0f},

    new float[]{0.0f, 0.5f, 0.0f, 0.0f, 0.0f},

    new float[]{0.0f, 0.0f, 0.5f, 0.0f, 0.0f},

    new float[]{0.0f, 0.0f, 0.0f, 1.0f, 0.0f},

    new float[]{0.0f, 0.0f, 0.0f, 0.0f, 1.0f}

    };

    ColorMatrix colorMatrix=new ColorMatrix(colorMatrixElements);

    //define the second color transformation matrix

    float[][] colorMatrixElements2=

    {

    new float[]{0.5f, 0.0f, 0.0f, 0.0f, 0.0f},

    new float[]{0.0f, 0.5f, 0.0f, 0.0f, 0.0f},

    new float[]{0.0f, 0.0f, 500.50f, 0.0f, 0.0f},

    new float[]{0.0f, 0.0f, 0.0f, 1.0f, 0.0f},

    new float[]{0.0f, 0.0f, 0.0f, 0.0f, 1.0f}

    };

    ColorMatrix colorMatrix2 = new ColorMatrix(colorMatrixElements2);


    //enable the first color transformation matrix

    imageAttributes.SetColorMatrix(

    colorMatrix,

    ColorMatrixFlag.Default,

    ColorAdjustType.Bitmap);


    //render the source image

    graphics.DrawImage(image, 0, 0);


    //use the color transformation matrix to render the new target image

    graphics.TranslateTransform(width+10,0);

    graphics.DrawImage(

    image,

    new Rectangle(0, 0, width, height),

    0, 0,

    width, height,GraphicsUnit.Pixel,

    imageAttributes);


    //clear up the former color transformation

    imageAttributes.ClearColorMatrix(ColorAdjustType.Bitmap);

    //reload another color transformation matrix colorMatrix2

    imageAttributes.SetColorMatrix(

    colorMatrix2,

    ColorMatrixFlag.Default,

    ColorAdjustType.Bitmap);


    //use matrix colorMatrix2 to adjust the color and render the transformed image

    graphics.TranslateTransform(width+10,0);

    graphics.DrawImage(

    image,

    new Rectangle(0, 0, width, height),

    0, 0,

    width, height,GraphicsUnit.Pixel,

    imageAttributes);

    Apparently, here we have leveraged two color matrices to perform the scaling operation. The first three elements in the main diagonal in the first matrix are all 0.5, which indicates the red, green, and blue components of each pixel in the image are evenly scaled down 50 percent, which results in the bright colors in the image being turned into gray. Take the red component for example. The color matrix transformation with the red component is achieved through the following matrix computation in Figure 6.


    Figure 6-the red component related matrix transformation

    The matrix transformation above indicates that the red color is changed into gray (127, 0, 0, 0) after the transformation.

    Moreover, we have defined the second transformation matrix, colorMatrix2. In contrast to the first matrix, the third element in the main diagonal in this matrix becomes 500.5, which means that the blue component is multiplied by 500.5 during the transformation. What on earth will this result in?

    As you may imagine, the third picture in Figure 5 is the output result through the colorMatrix2 matrix's associated transformation. Note that in the color matrix calculation of GDI+ when the result of a color component is greater than 1, the factual saturation of the special color only utilizes the fractional part. For example, 500.5 x 1=500.5, and the fractional part of 500.5 is 0.5. Retaining only the fractional part ensures that the result is always in the interval [0, 1].

    Next, let's move to another color transformation-rotating color operation.

    More C# Articles
    More By Xianzhong Zhu


     

    C# ARTICLES

    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT