[GDI+] ColorMatrix 彩色矩阵

首先对装配脑袋给出上两片文章的友好回复,还有网友Fisherman一起探讨ColorMatrix话题表示感谢!

ColorMatrix (彩色矩阵) 类位于System.Drawing.Imaging命名空间 先看看下面的代码

ColorMatrix cm = new ColorMatrix( new float [][] { new float [] { 0.5f , 0.5f , 0.5f , 0 , 0 } ,
new float [] { 0.5f , 0.5f , 0.5f , 0 , 0 } ,
new float [] { 0.5f , 0.5f , 0.5f , 0 , 0 } ,
new float [] { 0 , 0 , 0 , 1 , 0 , 0 } ,
new float [] { 0 , 0 , 0 , 0 , 1 , 0 } ,
new float [] { 0 , 0 , 0 , 0 , 0 , 1 } } );

矩阵系数组成一个 5x5 的线性转换,用于转换 ARGB 的单色值。例如,ARGB 向量表示为 Alpha、Red(红色)、Green(绿色)、Blue(蓝色)和 W,此处 W 始终为 1。

那么w是什么?为什么要定义为5x5的矩阵呢?

经过查找MSDN发现有这篇文章 《 使用颜色矩阵对单色进行变换 》里面这样讲到:

GDI+ 提供用于存储和操作图像的 ImageBitmap 类。 ImageBitmap 对象将每个像素的颜色都存储为 32 位的数:红色、绿色、蓝色和 alpha 各占 8 位。这四个分量的值都是 0 到 255,其中 0 表示没有亮度,255 表示最大亮度。alpha 分量指定颜色的透明度:0 表示完全透明,255 表示完全不透明。

颜色矢量采用 4 元组形式(红色、绿色、蓝色、alpha)。例如,颜色矢量 (0, 255, 0, 255) 表示一种没有红色和蓝色但绿色达到最大亮度的不透明颜色。

表示颜色的另一种惯例是用数字 1 表示亮度达到最大。使用这种惯例,上一段中描述的颜色将用 (0, 1, 0, 1) 表示。GDI+ 在进行颜色变换时使用以 1 表示最大亮度的惯例。

可通过用 4×4 矩阵乘以这些颜色矢量将线性变换(旋转和缩放等)应用到颜色矢量中。但是,您不能使用 4×4 矩阵进行平移(非线性)。如果在每个颜色矢量中再添加一个虚拟的第 5 坐标(例如,数字 1),则可使用 5×5 矩阵应用任何组合形式的线性变换和平移。由线性变换组成的后跟平移的变换称为仿射变换。

我认为可以理解为虚拟向量


**在Visual C#下实现图像的透明处理 这篇文章写得不错建议看看
地址: ** http://tech.ccidnet.com/pub/article/c294_a36258_p1.html

**彩色矩阵应用到图像上可以使用ImageAttributes.SetColorMatrix 方法

**

[C#]

**using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;**


**namespace Gr a** **yShear
{
  ///
1<summary>
2      /// Summary description for Form1.
3      /// </summary>

public class Form1 : System.Windows.Forms.Form { ///

1<summary>
2        /// Required designer variable.
3        /// </summary>

private System.ComponentModel.Container components = null;**

**public Form1()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();**


**//
      // TODO: Add any constructor code after InitializeComponent call
      //
    }**


**///
1<summary>
2        /// Clean up any resources being used.
3        /// </summary>

protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }**

**#region Windows Form Designer generated code
    ///
1<summary>
2        /// Required method for Designer support - do not modify
3        /// the contents of this method with the code editor.
4        /// </summary>

private void InitializeComponent() { // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(296, 269); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load);**

**}
    #endregion**


**///
1<summary>
2        /// The main entry point for the application.
3        /// </summary>

[STAThread] static void Main() { Application.Run(new Form1()); }**

**private void Form1_Load(object sender, System.EventArgs e)
    {
      OpenFileDialog dlg = new OpenFileDialog();
      dlg.Filter="Image files (*.BMP, *.JPG, *.GIF)|*.bmp;*.jpg;*.gif";
      if(dlg.ShowDialog()==DialogResult.OK)
      {
        Image img = Image.FromFile(dlg.FileName);
        Bitmap bm = new Bitmap(img.Width,img.Height);
        Graphics g = Graphics.FromImage(bm);**


****


**ColorMatrix cm = new ColorMatrix(new float[][]{   new float[]{0.5f,0.5f,0.5f,0,0},
                                  new float[]{0.5f,0.5f,0.5f,0,0},
                                  new float[]{0.5f,0.5f,0.5f,0,0},
                                  new float[]{0,0,0,1,0,0},
                                  new float[]{0,0,0,0,1,0},
                                  new float[]{0,0,0,0,0,1}});**


****


**/***


**//Gilles Khouzams colour corrected grayscale shear
        ColorMatrix cm = new ColorMatrix(new float[][]{   new float[]{0.3f,0.3f,0.3f,0,0},
                                  new float[]{0.59f,0.59f,0.59f,0,0},
                                  new float[]{0.11f,0.11f,0.11f,0,0},
                                  new float[]{0,0,0,1,0,0},
                                  new float[]{0,0,0,0,1,0},
                                  new float[]{0,0,0,0,0,1}});**


***/**


**ImageAttributes ia = new ImageAttributes();
        ia.SetColorMatrix(cm);
        g.DrawImage(img,new Rectangle(0,0,img.Width,img.Height),0,0,img.Width,img.Height,GraphicsUnit.Pixel,ia);
        g.Dispose();
        this.BackgroundImage=bm;
      }  
    }
  }  
  
 }**
Published At
Categories with Web编程
Tagged with
comments powered by Disqus