我有一个图像,我想设置像素为白色,如果像素(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