【问题标题】:How I use .NET ColorMatrix to change colors?如何使用 .NET ColorMatrix 更改颜色?
【发布时间】:2020-12-14 00:57:50
【问题描述】:

如果pixel(x,y).R

之后我想将所有不是白色的像素设置为黑色。

我可以使用 ColorMatrix 做到这一点吗?

【问题讨论】:

    标签: c# .net gdi+ colormatrix


    【解决方案1】:

    你不能用颜色矩阵来做到这一点。颜色矩阵适用于从一种颜色到另一种颜色的线性变换。你需要的不是线性的。

    【讨论】:

    • @Hans:你能详细说明一下吗?
    • 这是简的回答。我希望他能接球。如果这个问题对您没有帮助,请提出您自己的问题。
    【解决方案2】:

    进行这些相对简单的图像处理的一个好方法是自己直接获取位图数据。 Bob Powell 在https://web.archive.org/web/20141229164101/http://bobpowell.net/lockingbits.aspx 写了一篇关于此的文章。它解释了如何通过 Marshal 类锁定位图并访问其数据。

    最好有一个这样的结构:

    [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();
            }
        }
    }
    

    只需创建一个新的 Pixel 对象,您就可以通过 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多