我们有一个winforms应用程序(框架v4),显示图像(通过PictureBox)在屏幕上,并允许在该图像上的矩形区域的选择。在图像选择期间和之后,我们显示所选区域的边界。这目前是通过DrawRectangle
调用完成的。
我也看了看是否有一种破折号样式允许使用两种颜色(这样我就可以指定黑色和白色作为这些颜色,使它无论背景颜色是什么都可见),但是我找不到这种类型的东西。
提前感谢您的帮助。
你可以使用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);
}
您提到的另一种解决方案是用黑色和白色两种颜色绘制虚线,以便在任何背景上都可见。
用一种颜色(如黑色)画实线,然后用另一种颜色(如白色)画虚线。
创意和代码来自:http://csharphelper.com/blog/2012/09/draw-two-colored-dashed-lines-that-are-visible-on-any-background-in-c/
using (Pen pen1 = new Pen(Color.Black, 2))
{
e.Graphics.DrawRectangle(pen1, rect);
}
using (Pen pen2 = new Pen(Color.White, 2))
{
pen2.DashPattern = new float[] { 5, 5 };
e.Graphics.DrawRectangle(pen2, rect);
}