C# 在矩形下反转1bbp颜色
我正在使用GDI+,我正在使用的图像是1bbp图像。我想做的是在图像上画一个矩形,矩形下的所有东西都将被反转(白色像素将变为黑色,黑色像素将变为白色) 我看到的所有示例代码都是针对8位RGB色阶图像的,我认为它们使用的技术对我来说不适用 这是我到目前为止的代码。这是父控件,C# 在矩形下反转1bbp颜色,c#,colors,gdi+,C#,Colors,Gdi+,我正在使用GDI+,我正在使用的图像是1bbp图像。我想做的是在图像上画一个矩形,矩形下的所有东西都将被反转(白色像素将变为黑色,黑色像素将变为白色) 我看到的所有示例代码都是针对8位RGB色阶图像的,我认为它们使用的技术对我来说不适用 这是我到目前为止的代码。这是父控件,Epl2.IDrawableCommand中的一个将是执行反转的命令 public class DisplayBox : UserControl { (...) protected override void
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();
}
}
对于这个命令,我应该在
画图(图g)
中添加什么?诀窍是再次绘制相同的图像,并使用颜色矩阵。例如:
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的矩形。技巧是再次绘制相同的图像,并使用一个颜色矩阵。例如:
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的矩形。使用GDI,您可以使用带有R2_NOT或R2_XORPEN的SetROP2函数反转像素值。我认为GDI+没有类似的概念。该代码不会运行:“无法从具有索引像素格式的图像创建图形对象。”(在FromImage行上)使用GDI,可以使用带有R2_NOT或R2_XORPEN的SetROP2函数来反转像素值。我认为GDI+没有类似的概念。该代码不会运行:“不能从具有索引像素格式的图像创建图形对象。”(在FromImage行上)