2012-05-30 46 views
0

我想使用inkpresenter,以便每個新筆劃都放在之前的筆劃之上,從而創建變暗效果。如何使用inkPresenter以不透明度繪製筆畫?

我已經嘗試每次創建一個新的inkpresenter並將其添加到畫布,但不透明效果只在我釋放鼠標後纔會出現。我希望它在繪圖時立即顯示。如果我將inkpresenter直接設置爲目標元素的內容(不混合),則筆畫在繪製時具有不透明度。

+0

您如何使用'InkPresenter' inyour代碼請添加一些代碼。我寫了一個簡單的測試應用程序,它似乎正常工作。 –

回答

0

您尚未發佈任何代碼,顯示您當前如何在應用程序中處理InkPresenter。我已經在WPf中做了一個快速測試程序,它似乎可以正確使用不透明度。

我放置InkPresenter控制在XAML並加入PreviewMouseDownPreviewMouseMovePreviewMouseUp和方法的處理程序的代碼的後面。我用來處理這些事件的代碼如下所示:

System.Windows.Ink.Stroke newStroke = null; 

    private void inkPresenterSample_PreviewMouseDown(object sender, MouseButtonEventArgs e) 
    { 
     inkPresenterSample.CaptureMouse(); 

     //get mouse position and add first point to a new line to be drawn 
     var mousePosition = e.GetPosition(inkPresenterSample); 
     var stylusStartPoint = new StylusPointCollection(); 
     stylusStartPoint.Add(new StylusPoint(mousePosition.X, mousePosition.Y)); 

     //set line's attributes, it real application this should be probably done outside this method 
     var drawingAttributes = new System.Windows.Ink.DrawingAttributes(); 
     //IMPORTANT: semi-transparent color is used, so the opacity effect is visible 
     drawingAttributes.Color = System.Windows.Media.Color.FromArgb(110, 0, 0, 0); 
     drawingAttributes.Width = 10; 

     //create a new stroke to be drawn 
     newStroke = new System.Windows.Ink.Stroke(stylusStartPoint, drawingAttributes); 
     newStroke.StylusPoints.Add(new StylusPoint(mousePosition.X, mousePosition.Y)); 

     //add reference to a new stroke to the InkPresenter control 
     inkPresenterSample.Strokes.Add(newStroke); 
    } 

    private void inkPresenterSample_PreviewMouseUp(object sender, MouseButtonEventArgs e) 
    { 
     inkPresenterSample.ReleaseMouseCapture(); 

     if (newStroke != null) 
     { 
      newStroke = null; 
     } 
    } 

    private void inkPresenterSample_PreviewMouseMove(object sender, MouseEventArgs e) 
    { 
     //if a stroke is currently drawn in the InkPresenter 
     if (newStroke != null) 
     { 
      //add a new point to the stroke 
      var mousePosition = e.GetPosition(inkPresenterSample); 
      newStroke.StylusPoints.Add(new StylusPoint(mousePosition.X, mousePosition.Y)); 
     } 
    } 

看起來像你描述的工作,我能看到他們幾個重疊線的暗部。

更新

的重疊效應的建議解決方案適用於層巒疊線,但如果單行overlapps本身不起作用。如果你想讓它在這種情況下工作,以及,你可以嘗試:

  • 使用Canvas並在其上添加Polyline元素(更新:這似乎工作像第一個解決方案建議,這樣一條線重疊不給不透明度效果)
  • 在使用這裏介紹DrawingContextImage元素上繪製看一看,它可以幫助:Drawing with mouse causes gaps between pixels
+0

謝謝!你的代碼真的有效。我認爲的主要區別在於,我在mouseup上添加了筆劃而不是mousedown,並且我正在將inkAttributes設置爲inkpresenter而不是stroke。 – Occham