c# - 反转矩形下的 1bbp 颜色

标签 c# colors gdi+

我正在使用 GDI+,我正在使用的图像是 1bbp 图像。我想要做的是在图像上绘制一个矩形,该矩形下的所有内容都将反转(白色像素将变为黑色,黑色像素将变为白色)。

我看到的所有示例代码都是针对 8 位 RGB 色标图像的,我不认为他们使用的技术对我有用。

这是我目前的代码。这是父控件,Epl2.IDrawableCommand 之一将是执行反转的命令。

public class DisplayBox : UserControl
{
    (...)
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        (...)
            using (Bitmap drawnLabel = new Bitmap((int)((float)Label.LabelHeight * _ImageScaleFactor), (int)((float)Label.LableLength *(int) _ImageScaleFactor), System.Drawing.Imaging.PixelFormat.Format1bppIndexed))
            {
                using (Graphics drawBuffer = Graphics.FromImage(drawnLabel))
                {
                    (...)
                    foreach (Epl2.IDrawableCommand cmd in Label.Collection)
                    {
                        cmd.Paint(drawBuffer);
                    }
                    (...)
                }
            }
        }
    }
}
public class InvertArea : IDrawableCommand
{
    (...)
    public Rectangle InvertRectangle {get; set;}
    public void Paint(Graphics g)
    {
        throw new NotImplementedExecption();
    }
}

对于此命令,我应该在 Paint(Graphic g) 中输入什么?

最佳答案

诀窍是再次绘制相同的图像并使用 inverts the image 的 ColorMatrix .例如:

    protected override void OnPaint(PaintEventArgs e) {
        e.Graphics.DrawImage(mImage, Point.Empty);
        ImageAttributes ia = new ImageAttributes();
        ColorMatrix cm = new ColorMatrix();
        cm.Matrix00 = cm.Matrix11 = cm.Matrix22 = -0.99f;
        cm.Matrix40 = cm.Matrix41 = cm.Matrix42 = 0.99f;
        ia.SetColorMatrix(cm);
        var dest = new Rectangle(50, 50, 100, 100);
        e.Graphics.DrawImage(mImage, dest, dest.Left, dest.Top, 
            dest.Width, dest.Height, GraphicsUnit.Pixel, ia);
    }

mImage 是我的示例 1bpp 图像,我在 (50, 50) 处反转了一个 100x100 的矩形。

关于c# - 反转矩形下的 1bbp 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2656212/

相关文章:

c# - System.Windows.Media.RenderCapability.Tier 返回的不是渲染模式

winapi - 使用 GDIPlus (WIn32 C++) 显示存储为带 alpha 资源的图标

.net - 寻找比 GDI 更快的渲染动态数据图的解决方案

c# - Color.GetPixel().equals(Color.Blue) 的结果为 false

javascript - HighCharts 中的单个系列中是否可以有不同颜色的图表?

c++ - GDI+图像缩放问题

c# - 具有日期文件夹的AppData中的Log4Net存储文件

c# - C# set/get 中的堆栈溢出错误

c# - 创建对象的新实例,还是修改现有实例?

colors - 管道到其他命令时保留 heroku 日志输出的颜色(例如 grep)