首先對裝配腦袋給出上兩片文章的友好回復,還有網友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+ 提供用于存儲和操作圖像的 image 和 bitmap 類。image 和 bitmap 對象將每個像素的顏色都存儲為 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;
}
}
}
}