C#
  Home arrow C# arrow Page 3 - Color Transformation in C# GDI+ Programmin...
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#

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

    Table of Contents:
  • Color Transformation in C# GDI+ Programming
  • Introduction to Color Transformation
  • A Sample that Uses Color Transformation Matrices
  • Conclusion

  • 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


    Color Transformation in C# GDI+ Programming - A Sample that Uses Color Transformation Matrices


    (Page 3 of 4 )

    The following example takes an image of ASTRO BOY, who is the dramatis personae in the famous Japanese film ASTRO BOY. We are to apply the color transformation matrix described in the preceding paragraphs to accomplish the following result:

    1. Double the red component

    2. Set the color saturation of the red component to 1

    When you launch the sample application, you will get the screenshot shown in Figure 6.

    Figure 6—the running time snapshot of the sample application


    The image shown on the left of Figure 6 corresponds to the original image, while the right one relates to the transformed image.

    Now, let’s take a thorough look at the related source code.

    Graphics graphics = this.CreateGraphics();

    //use white color to populate the background

    graphics.Clear(Color.White);


    //load the original picture

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

    int width = image.Width;

    int height = image.Height;

    //create an instance of class ImageAttributes

    ImageAttributes imageAttributes = new ImageAttributes();


    //define the color transformation matrix

    float[][] colorMatrixElements =

    {

    new float[]{2.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[]{1.0f, 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);


    //first render the source image

    //at the specified location (0, 0)

    graphics.DrawImage(image, 0, 0);

    //next render the target image using the

    //color transformation matrix defined above

    graphics.TranslateTransform(width + 10, 0);

    graphics.DrawImage(

    image,

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

    0, 0,

    width, height, GraphicsUnit.Pixel,

    imageAttributes);

    On the whole, the code in the preceding example utilizes the following steps to render the target image:

    1. Initialize a ColorMatrix structure.

    2. Create an ImageAttributes object and pass the address of the ColorMatrix structure to the SetColorMatrix() method of the ImageAttributes object.

    3. Pass the address of the ImageAttributes object to the Graphics::DrawImage() method of a Graphics object.

    To present a more striking contrast to the previous sample, let’s check out another image (see Figure 7) that resembles the color matrix used in Adobe Photoshop.

    Figure 7—the color matrix image comparison

    That’s all for this simple sample.

    More C# Articles
    More By Xianzhong Zhu


       · Could you please also provide more in depth introduction about color matrix and so...
     

    C# ARTICLES

    - Coding a CRC-Generating Algorithm in C
    - 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#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek