C#
  Home arrow C# arrow 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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+


    (Page 1 of 4 )

    With the basic idea of color transformation matrices in mind, in this second installment of a three-part series, we will delve into the four types of operations of color transformation matrices in GDI+, as well as give the related samples.

    Translating Color Operation

    In reality, a translation is the addition calculation necessary to add a value to one or more of the four color components (i.e. red, green, blue, and alpha). The color transformation matrix entries that represent translations are given in the following Figure 1.


    Figure 1-the transformation matrix corresponding to translating color operation

    The components colored gray in Figure 1 represent the elements concerned with translating color operation. Now, let's take a look at an example that performs the translating color operation.

    The following example constructs an Image object from the given file named ColorInput.bmp. Then the code adds 0.5 to the red component of each pixel in the image. The transformed image is drawn alongside the original image.

    Graphics graphics=this.CreateGraphics();

    graphics.Clear(Color.White);

    //load the original image

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

    ImageAttributes imageAttributes = new ImageAttributes();

    int width = image.Width;

    int height = image.Height;

    //define a color transformation matrix

    float[][] colorMatrixElements=

    {

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

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

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

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

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

    };

    ColorMatrix colorMatrix=new ColorMatrix(colorMatrixElements);

    //enable the 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);

    The following Figure 2 shows the related running-time snapshot, with the original image on the left and the transformed one on the right.

    Figure 2-the running-time snapshot for the translating color operation

    From Figure 2, we can easily see that after the translating color transformation, the previous black color is changed into a catsup red color, and the other colors in Figure 2 also change accordingly. Why?

    From the above code we can see that the saturation of the red component is set to 0.5 in the color transformation matrix. Take the black color as an example; its related color transformation matrix can be figured out using the following matrix transformation (A=0.5 x 255) in Figure 3.


    Figure 3-the color transformation for the black color

    In the matrix transformation in Figure 3, the RGB value of black (0, 0, 0) is changed into (128, 0, 0) (i.e. the RGB value of a catsup red color) after the transformation. In the same way, you can figure out the RGBs of all the rest of the colors after the color transformation.

    Next, let's shift our attention to another important color calculation-scaling transformation.

    More C# Articles
    More By Xianzhong Zhu


     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT