我们有一个 winforms 应用程序 (Framework v4),它在屏幕上显示图像(通过 PictureBox)并允许在该图像上选择一个矩形区域。在图像选择期间和之后,我们显示所选区域的边界。目前这是通过 DrawRectangle
调用完成的。
问题是如何选择这个矩形的颜色。无论选择何种颜色,它总是有可能融入背景(图像)中。 Microsoft paint 通过在“选择矩形”上动态反转颜色来很好地处理这个问题。这非常适合我们的应用程序,但我不知道如何在 winforms 中执行此操作。
我还环顾四周,看看是否有允许使用两种颜色的破折号样式(这样我就可以将黑色和白色指定为这些颜色,无论背景颜色是什么,它都可见),但我找不到此类内容。
在此先感谢您的所有帮助。
最佳答案
您可以使用ControlPaint
方法绘制可翻转的矩形/框架
ControlPaint.FillReversibleRectangle
MSDN
和
ControlPaint.DrawReversibleFrame
MSDN
这里是一个伪代码方法的小例子
private void DrawReversibleRectangle(int x, int y) {
// Hide the previous rectangle by calling the methods with the same parameters.
var rect = GetSelectionRectangle(this.PointToScreen(this.reversibleRectStartPoint), this.PointToScreen(this.reversibleRectEndPoint));
ControlPaint.FillReversibleRectangle(rect, Color.Black);
ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Dashed);
this.reversibleRectEndPoint = new Point(x, y);
// Draw the new rectangle by calling
rect = GetSelectionRectangle(this.PointToScreen(this.reversibleRectStartPoint), this.PointToScreen(this.reversibleRectEndPoint));
ControlPaint.FillReversibleRectangle(rect, Color.Black);
ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Dashed);
}
关于c# - 在 winforms 应用程序上绘制相反(反向)颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8509909/