首 页 | 新 闻 | 技术中心 | 第二书店 | 《程序员》 | 《开发高手》 | 社 区 | 黄 页 | 人 才
移 动专 题SUNIBM微 软微 创精 华Donews人 邮
我的技术中心 
我的分类 我的文档
全部文章 发表文章
专栏管理 使用说明



 RSS 订阅 
最新文档列表
Windows/.NET
.NET  (rss)    
Visual C++  (rss)    
Delphi  (rss)    
Visual Basic  (rss)    
ASP  (rss)    
JavaScript  (rss)    
Java/Linux
Java  (rss)    
Perl  (rss)    
综合
其他开发语言  (rss)    
文件格式  (rss)    
企业开发
游戏开发  (rss)    
网站制作技术  (rss)    
数据库
数据库开发  (rss)    
软件工程
其他  (rss)    

积极原创作者 
iiprogram (74)
nizhigang2000 (3)
hongbo781202 (81)
Kendiv (113)
TechnoFantasy (52)
feifei1018 (19)
coofucoo (108)
qingrun (67)
btbtd (83)
longrujun (64)
CSDN - 文档中心 - .NET 阅读:6802   评论: 0    参与评论
标题   [GDI+] ColorMatrix 彩色矩阵      选择自 hbzxf 的 Blog
关键字   [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 GrayShear
{
  /// <summary>
  /// Summary description for Form1.
  /// </summary>
  public class Form1 : System.Windows.Forms.Form
  {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;
    public Form1()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </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
    /// <summary>
    /// The main entry point for the application.
    /// </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;
      }  
    }
  }


}

相关文章
对该文的评论