2016-12-27 76 views
-1

我遇到了有關GDI +的效率問題。 有幾個變量和方法 如下: 1.Points,如A(表示一個座標點,如XYZ),B,C,d,E等更新移動鼠標

命名CMD1

2列表,使用通過線程來添加點

3.Paint方法,在該方法中,該組連接到線路

4.Thread爲常數增加新的點,如F,G,H,I點的,J等

在畫圖方法中,我使用g.DrawLine()來鏈接和b,C,d,E。 在線程,當我加點,我會打電話無效刷新組件。 所以我的問題是,點越來越多,我怎麼能保持高效率,並重繪,

不要從一個點做起,把重 - DrawLine的。

Sub DrawGLines2(g As Graphics) 

    g.SmoothingMode = SmoothingMode.HighSpeed 
    Dim Pen As New Pen(Brushes.White) 
    Dim i As Int32 
    'Dim c As Int32 
    Dim preCmd1 As Cmd1 
    Try 
     For Each cmd As Cmd1 In Cmd1s    
       Dim pfs() As PointF = cmd.PointFs.ToArray 
       If preCmd1 IsNot Nothing Then 
        g.DrawLine(Pen, cmd.PointFs(0), preCmd1.PointFs(0)) 
       End If 
       preCmd1 = cmd 
      End If 

     Next 
    Catch ex As Exception 
     Debug.Print(ex.Message) 
    End Try 
End Sub 

Private Sub Sheet_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint 
    If Me.Cmd1s.Count>0 Then 
     DrawGLines2(e.Graphics) 
    End If 
End Sub 

Public Sub AddPoint(x As Double, y As Double, z As Double, Optional G As Int32 = -1) 
    Dim cmd1 As DrvSimu.Cmd1 = Nothing 
    If cmd1 Is Nothing Then 
     cmd1 = New DrvSimu.Cmd1 
     Me.Cmd1s.Add(cmd1) 
    End If 

    Dim pf3d As New PointF3D(x, y, z) 
    cmd1.PointF3Ds.Add(pf3d) 

    Me.Invalidate() 

End Sub 

線程將調用AddPoint添加A,B,C,d,E點,並使用無效的方法來刷新,當我打電話無效,則「對於Cmd1中的每一個Cmd1」,每一個都將從A點開始,所以當點越來越多時,我該如何保持高效率,並重繪,不要從A點開始重劃線

回答

1

這取決於你究竟想要做什麼。

一種可能是您當前使用的一個。在每次無效時重新繪製所有行。根據繪圖質量和CPU,可以繪製更多或更少的線條,但應該可以每毫秒繪製至少10行。

如果只添加行,你不需要刪除或修改它們,也可以提請所有線爲位圖和無效只繪製圖像到屏幕上。通過這個,你只需要在圖片添加時畫出新的線條。此方法的問題在於,如果要縮放或平移該區域或要刪除線條,則仍然需要完全重新繪製。

作爲一個起點看到Graphics.FromImage(...)方法。 使用Pixelformat Format32bppARGB可獲得最佳性能。

編輯:

public partial class LineForm : Form 
{ 
    private Bitmap lineBitmap = null; 

    private List<Tuple<PointF,PointF>> lines = new List<Tuple<PointF,PointF>>(); 

    private List<Tuple<PointF,PointF>> newLines = new List<Tuple<PointF,PointF>>(); 

    // must be set if you remove line, pan or zoom the view and if you resize the form. 
    private bool redrawAll = false; 

    public LineForm() 
    { 
     this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint); 
     this.Resize += new System.EventHandler(this.OnResize); 
    } 

    private void OnResize(object sender, EventArgs e) 
    { 
     if (this.lineBitmap!= null) 
     { 
      this.lineBitmap.Dispose(); 
     } 

     if (this.Width <= 0 || this.Height <= 0) 
     { 
      return; 
     } 

     this.lineBitmap = new Bitmap(this.Width, this.Height, PixelFormat.Format32bppPArgb); 
     this.redrawAll = true; 
    } 

    private void OnPaint(object sender, PaintEventArgs e) 
    { 
     Graphics lineGfx = Graphics.FromImage(this.lineBitmap); 
     // Settings for best drawing Performance. Must be adjusted for better quality 
     lineGfx.CompositingQuality = CompositingQuality.HighSpeed; 
     lineGfx.InterpolationMode = InterpolationMode.NearestNeighbor; 
     lineGfx.SmoothingMode = SmoothingMode.None; 
     lineGfx.PixelOffsetMode = PixelOffsetMode.None; 
     lineGfx.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; 
     lineGfx.CompositingMode = CompositingMode.SourceCopy; 

     // Only clear the Image and draw all lines if necessary 
     if(this.redrawAll) 
     { 
      lineGfx.Clear(Color.Transparent); 
      foreach(Tuple<PointF,PointF> line in this.lines) 
      { 
       lineGfx.DrawLine(Pens.Black, line.Item1, line.Item2); 
      } 
     } 

     // Draw the new Lines to the Bitmap and store them to lines list 
     foreach(Tuple<PointF,PointF> newline in this.newLines) 
     { 
      lineGfx.DrawLine(Pens.Black, newline.Item1, newline.Item2); 
      tihs.lines.Add(newLine); 
     } 

     // Clear the newLines List as the liones are added to the lines List now. 
     this.newLines.Clear(); 

     // Draw the Bitmap to the screen 
     e.Graphics.DrawImageUnscaled(this.lineBitmap,0,0); 
    } 

    private void AddLine(PointF p1, PointF p2) 
    { 
     this.newLines.Add(new Tuple<PointF,PointF>(p1,p2)); 
     // Invalidate the view => OnPaint Event is raised; 
     this.Invalidate(); 
    } 
} 

注:我還沒有添加任何鎖止機構變爲列表操作。爲了防止在使用它們時更改列表,您應該添加一些鎖。

+0

你可以展示一些細節或演示,我可以知道更多。 – xuyunhai

+0

非常感謝,我會仔細閱讀。 – xuyunhai

+0

我發現一個問題: 你認爲效率的提高是由於使用位圖,但我認爲效率的提高取決於新的點重繪,而不是所有的點。換句話說,我認爲,即使你不使用BITMAP,只要重新繪製新添加的點的唯一方法,是否可以提高效率 – xuyunhai