C#
  Home arrow C# arrow Page 3 - Color Transformation Applications in C# GD...
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 Applications in C# GDI+ Programming
By: Xianzhong Zhu
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2008-07-30

    Table of Contents:
  • Color Transformation Applications in C# GDI+ Programming
  • Pulling Out the Foreground Image Demo
  • About the Color Channel
  • Summary

  • 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 Applications in C# GDI+ Programming - About the Color Channel


    (Page 3 of 4 )

    Quite a few readers may be familiar with Adobe’s PhotoShop, the famous two dimensional picture and image processing software. One thing PhotoShop supports is color channel isolating. By decomposing the original compound image into three isolated r/g/b channels, we can edit the current image more easily. With all this work done, we can compose the three channels again.

    In fact, color mapping is a kind of color filtering. If we use the color transformation matrices to set the related color saturation to zero, then this color will be invisible when outputed.

    Using the tools in GDI+, we can use the SetOutputChannel method of the ImageAttributes class to output the individual color channels. But, regrettably, the SetOutputChannel method only supports outputting the C/M/Y/K channels. In fact, with the help of different color transformation matrices, we can also succeed in outputting the individual R/G/B channels. Next, we will see an example that accomplishes this using the color transformation matrix technique.

    Isolating R/G/B Channel Demo

    As usual, let’s visualize the finished line. Figure 2 demonstrates the related running-time snapshot.

    Figure 2—using the color transformation matrix to isolate the R/G/B channel


    In Figure 2, the original compound image is rendered at the upper left corner, with the other three being the images maintaining the R/G/B channels.

    Now, let’s dig into the "how-to's" of the above sample. The following lists the souce code with detailed explanations.

    Graphics graphics = this.CreateGraphics();

    graphics.Clear(Color.White);


    //load the image

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

    //render the source image

    graphics.DrawImage(image, 50, 50);

    //get the necessary data required by the later rendering operation

    int width = image.Width;

    int height = image.Height;

    ImageAttributes imageAttributes = new ImageAttributes();


    //set up the red channel

    float[][] colorMatrixElements =

    {

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

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

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


    //enable the color transformation matrix

    imageAttributes.SetColorMatrix(

    colorMatrix,

    ColorMatrixFlag.Default,

    ColorAdjustType.Bitmap);


    //use the above color transformation matrix to render the new picture

    //corresponding to the one in the red channel

    graphics.TranslateTransform(width + 60, 50);

    graphics.DrawImage(image, new Rectangle(0, 0, width, height),

    0, 0, width, height, GraphicsUnit.Pixel, imageAttributes);


    //clear out the already-used color transformation

    imageAttributes.ClearColorMatrix(ColorAdjustType.Bitmap);


    //set up the second channel—the green channel

    float[][] colorMatrixElements2 =

    {

    new float[]{0.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, 0.0f, 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 color transformation matrix

    imageAttributes.SetColorMatrix(

    colorMatrix2,

    ColorMatrixFlag.Default,

    ColorAdjustType.Bitmap);

    //use the color transformation matrix to render the new picture

    //corresponding to the one in the green channel

    //first reset transformation matrix to identity

    graphics.ResetTransform();

    graphics.TranslateTransform(50, height + 60);

    graphics.DrawImage(image, new Rectangle(0, 0, width, height),

    0, 0, width, height, GraphicsUnit.Pixel, imageAttributes);


    //clear out the used color transformation

    imageAttributes.ClearColorMatrix(ColorAdjustType.Bitmap);


    //set up the blue channel

    float[][] colorMatrixElements3 =

    {

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

    new float[]{0.0f, 0.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.0f, 0.0f, 0.0f, 0.0f, 1.0f}

    };

    ColorMatrix colorMatrix3 = new ColorMatrix(colorMatrixElements3);


    //enable the color transformation matrix

    imageAttributes.SetColorMatrix(

    colorMatrix3,

    ColorMatrixFlag.Default,

    ColorAdjustType.Bitmap);


    //use the color transformation matrix to output picture

    graphics.TranslateTransform(width+10 , 0);

    graphics.DrawImage(image, new Rectangle(0, 0, width, height),

    0, 0, width, height, GraphicsUnit.Pixel, imageAttributes);

    By using the color transformation matrices we’ve easily taken out the independent R/G/B channels. This resembles the function that PhotoShop has provided. However, the above sample is the simplest application of color transformation. You can easily find out that if you modify the R/G/B/A component at the main diagonal in the color transformation matrix, you can obtain some special and even miraculous effects.

    To further grasp the above idea, I recommend that you test the SetOutputChannel method of the ImageAttributes class so you can output the C/M/Y/K channels in order to make a good comparison with the idea supplied in this article.

    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 5 hosted by Hostway
    Stay green...Green IT