2011-12-14 87 views
4

我們有一個winforms應用程序(Framework v4),它在屏幕上顯示一個圖像(通過PictureBox),並允許在該圖像上選擇一個矩形區域。在圖像選擇期間和之後,我們顯示所選區域的邊界。這目前通過DrawRectangle呼叫完成。在winforms應用程序上繪製反向(反向)顏色

問題是如何選擇這個矩形的顏色。無論選擇何種顏色,它總是可能會融入背景(圖像)中。通過在「選擇矩形」上動態反轉顏色,Microsoft繪畫可以很好地處理這一問題。這對我們的應用程序非常適合,但我不知道如何在winforms中執行它。

我還環顧了一下,看看是否有一種可以使用兩種顏色的短劃線樣式(這樣我就可以指定黑色和白色作爲這些顏色,使其無論背景顏色如何都可見),但我找不到這種類型的東西。

在此先感謝您的幫助。

+1

你會做Microsoft畫圖究竟是幹什麼的,看一下背景圖像,並確定你應該使用的顏色。請注意,有些顏色與自己的顏色相反。我會看看開源項目Paint.NET的想法。這與WinForms無關,真正的解決方案是擴展自己的能力,默認的方法不會達到你想要的。 – 2011-12-14 18:50:10

+0

@Rhhound:好的,解決方案需要在Winforms中實現,所以解決方案必須與它有關。即使我查看背景圖片,我也不確定如何在winforms繪圖函數中將這些信息與API一起使用。我也看了Paint.Net的許可證。它不允許創建派生作品,因此我不確定查看其源代碼並「獲得靈感」是否是一個好主意。 PS:感謝關於顏色的提示,它們有自己的對立面。我將在考慮解決方案時考慮這一點。 – alokoko 2011-12-14 19:07:57

回答

2

可以使用ControlPaint方法作畫的可逆矩形/幀

ControlPaint.FillReversibleRectangleMSDN

ControlPaint.DrawReversibleFrameMSDN

這裏是一個小的僞代碼方法例如

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); 
} 
0

您提到的另一種解決方案是繪製黑色和白色兩種顏色的虛線,以便在任何背景上均可見。

用一種顏色(例如黑色)繪製實線,然後用另一種顏色(例如白色)繪製一條虛線。

理念和代碼: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); 
}