Other Color Space Manipulations

I l @ ve RuBoard

Other Color Space Manipulations

Having a whole huge matrix for alpha blending alone seems like overkill, until you realize that any manipulation of the color space is possible with such a tool, if only you knew what numbers to plumb in. The technique of manipulating such a matrix for the purpose of changing the color space is called recoloring and is also easy to do with GDI+. Once again, the tool used is the ColorMatrix and the vehicle is the ImageAttributes . Unfortunately, there are no methods on the ColorMatrix to perform color space rotations , but they would be possible, given the correct settings of the linear part of the matrix. As an exercise, you could send in a modification of Listing 3.5.10 to sams @netedgesoftware.com that does color space rotations. The best 10 correct ones within the first year of publication of this book get a free NetEdge Software Polo shirt. To get you started, Listing 3.5.11 is a modification of the previous that allows you to set the red, green, and blue component levels of your chosen image.

Listing 3.5.11 ColorSpace1.cs: More Color Space Transformations
 1: using System;   2: using System.Drawing;   3: using System.Drawing.Drawing2D;   4: using System.Drawing.Imaging;   5: using System.Collections;   6: using System.ComponentModel;   7: using System.Windows.Forms;   8: using System.Data;   9:  10: namespace ColorSpace1  11: {  12:    class Form1 : Form  13:    {  14:  15:       Button b;  16:       TrackBar tr,tg,tb;  17:       Image i;  18:  19:       void OnPaint(object Sender,PaintEventArgs e)  20:       {  21:          SolidBrush b=new SolidBrush(Color.Red);  22:          Rectangle r=this.ClientRectangle;  23:          GraphicsPath pth=new GraphicsPath();  24:          if(i!=null)  25:          {  26:             ColorMatrix m=new ColorMatrix();  27:             m.Matrix00=(float)(1.0/256*tr.Value);  28:             m.Matrix11=(float)(1.0/256*tg.Value);  29:             m.Matrix22=(float)(1.0/256*tb.Value);  30:             ImageAttributes ia=new ImageAttributes();  31:             ia.SetColorMatrix(m);  32:             e.Graphics.DrawImage(i,this.ClientRectangle,0,  33:                  0,i.Width,i.Height,GraphicsUnit.Pixel,ia);  34:          }  35:       }  36:  37:       void OnClickB(object sender, EventArgs e)  38:       {  39:          OpenFileDialog dlg=new OpenFileDialog();  40:          dlg.Filter="Bitmap files(*.bmp)*.bmp";  41:          if(dlg.ShowDialog()==DialogResult.OK)  42:          {  43:             i=Image.FromFile(dlg.FileName);  44:             Invalidate();  45:          }  46:       }  47:  48:       void OnTrack(object sender, EventArgs e)  49:       {  50:          Invalidate();  51:       }  52:  53:       void OnSize(object sender, EventArgs e)  54:       {  55:          Invalidate();  56:       }  57:  58:       public Form1()  59:       {  60:          this.Paint+=new PaintEventHandler(OnPaint);  61:          this.SizeChanged+=new EventHandler(OnSize);  62:  63:          b=new Button();  64:  65:          b.Click+=new EventHandler(OnClickB);  66:  67:          b.Location=new Point(5,5);  68:          b.Size=new Size(60,22);  69:          b.Text="Image...";  70:  71:          this.Controls.Add(b);  72:  73:          tr=new TrackBar();  74:          tr.Location=new Point(100,5);  75:          tr.Size=new Size(200,22);  76:          tr.Maximum=255;  77:          tr.Minimum=0;  78:          tr.ValueChanged+=new EventHandler(OnTrack);  79:  80:  81:          this.Controls.Add(tr);  82:  83:          tg=new TrackBar();  84:          tg.Location=new Point(100,55);  85:          tg.Size=new Size(200,22);  86:          tg.Maximum=255;  87:          tg.Minimum=0;  88:          tg.ValueChanged+=new EventHandler(OnTrack);  89:  90:  91:          this.Controls.Add(tg);  92:  93:          tb=new TrackBar();  94:          tb.Location=new Point(100,105);  95:          tb.Size=new Size(200,22);  96:          tb.Maximum=255;  97:          tb.Minimum=0;  98:          tb.ValueChanged+=new EventHandler(OnTrack);  99: 100: 101:          this.Controls.Add(tb); 102:       } 103: 104:       static void Main() 105:       { 106:          Application.Run(new Form1()); 107:       } 108:    } 109: } 

Compile this file with the following command line:

 csc /t:winexe colorspace1.cs 

This file is substantially identical to that in Listing 3.5.10 with the exception that track bars for red, green, and blue are employed, instead of for alpha. The matrix is set up on lines 27 “29 to adjust the intensity of each of the R, G, and B color channels individually.

I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net