我正在使用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();
}
}
Run Code Online (Sandbox Code Playgroud)
我应该Paint(Graphic g)为此命令添加什么?
诀窍是再次绘制相同的图像并使用反转图像的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);
}
Run Code Online (Sandbox Code Playgroud)
其中mImage是我的样本1bpp图像,而我正在以50,50反转100x100矩形
| 归档时间: |
|
| 查看次数: |
881 次 |
| 最近记录: |