我有一個圖像,我想設置像素爲白色,如果像素(x,y).R < 165.如何使用.NET ColorMatrix更改顏色?
之後,我想設置黑色不是白色的所有像素。
我可以使用ColorMatrix來做到這一點嗎?
我有一個圖像,我想設置像素爲白色,如果像素(x,y).R < 165.如何使用.NET ColorMatrix更改顏色?
之後,我想設置黑色不是白色的所有像素。
我可以使用ColorMatrix來做到這一點嗎?
你不能用colormatrix做到這一點。顏色矩陣適用於從一種顏色到另一種顏色的線性轉換。你需要的不是線性的。
做這些相對簡單的圖像操作的好方法是直接在位圖數據上自己。鮑勃鮑威爾在http://www.bobpowell.net/lockingbits.htm上寫了一篇文章。它解釋瞭如何鎖定位圖並通過Marshal類訪問其數據。
我也寫過一篇文章,對此進行了擴展。最大的區別是我將圖像數據複製到一個int數組中,這可以使事情變得更簡單。 http://ilab.ahemm.org/tutBitmap.html
這是件好事,沿着這些線結構:
[StructLayout(LayoutKind.Explicit)]
public struct Pixel
{
// These fields provide access to the individual
// components (A, R, G, and B), or the data as
// a whole in the form of a 32-bit integer
// (signed or unsigned). Raw fields are used
// instead of properties for performance considerations.
[FieldOffset(0)]
public int Int32;
[FieldOffset(0)]
public uint UInt32;
[FieldOffset(0)]
public byte Blue;
[FieldOffset(1)]
public byte Green;
[FieldOffset(2)]
public byte Red;
[FieldOffset(3)]
public byte Alpha;
// Converts this object to/from a System.Drawing.Color object.
public Color Color {
get {
return Color.FromArgb(Int32);
}
set {
Int32 = Color.ToArgb();
}
}
}
只需創建一個全新的像素對象,你可以通過的Int32字段設置它的數據和回讀/修改各個顏色分量。
Pixel p = new Pixel();
p.Int32 = pixelData[pixelIndex]; // index = x + y * stride
if(p.Red < 165) {
p.Int32 = 0; // Reset pixel
p.Alpha = 255; // Make opaque
pixelData[pixelIndex] = p.Int32;
}
他可以,使用兩個。 – 2010-11-28 13:57:28