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  
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#

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


     

    C# ARTICLES

    - 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
    - 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#

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




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