在我的應用程式中,有 2 個視窗,并且都包含一個 PictureBox。第一個 (pb1) 允許互動,并且可以通過單擊和滑鼠移動事件更改影像。這些事件呼叫 pb1.Invalidate(); 效果很好。
我希望第二個 PictureBox (pb2) 也重繪,所以我從 pb1 的繪制事件中呼叫 pb2.Invalidate()。[只是為了背景關系,第二個 PictureBox 顯示幾乎相同的影像,但比例更大,并且繪圖的某些部分將來會被遺漏,所以我在兩個繪圖事件中使用相同的方法來決定繪制什么和不繪制什么]
它有效,但它“滯后”,我希望它像第一個 PictureBox 上的油漆一樣光滑。出于測驗目的,我將繪畫事件簡化為網格。
- 兩個視窗都是雙緩沖的。
- 我嘗試用 SkiaSharp 的 SKGLControls 替換圖片框(應該有更好的性能)。示例代碼仍然使用 SkiaEvents,因此如果兩個控制元件都出現問題,請不要混淆。
- 我嘗試使用 .Update() 或 .Refresh() 而不是 .Invalidate() 但我想它要處理的太多了,應用程式只是崩潰了..
這是兩個 OnPaint 事件呼叫的方法
public void Update(SKPaintGLSurfaceEventArgs e, bool bigscreen)
{
SKCanvas canvas = e.Surface.Canvas;
canvas.Clear(SKColors.Beige);
//Zoom to specified area
SKMatrix matrix = SKMatrix.Identity;
if (!bigscreen)
{
matrix = matrix.PostConcat(SKMatrix.CreateScale(canvasSize / (float)zoomArea.Width, canvasSize / (float)zoomArea.Height));
}
else
{
matrix = matrix.PostConcat(SKMatrix.CreateScale(bigCanvasSize / (float)zoomArea.Width, bigCanvasSize / (float)zoomArea.Height));
}
matrix = matrix.PreConcat(SKMatrix.CreateTranslation(-zoomArea.X, -zoomArea.Y));
canvas.SetMatrix(matrix);
DrawGrid(canvas);
}
和網格繪制方法
private void DrawGrid(SKCanvas canvas)
{
using (SKPaint paint = new SKPaint() { IsAntialias = true,Color=SKColors.LightGray,StrokeWidth = 1})
{
canvas.DrawLine(0, 0, 0, gridCanvas.Height, paint); //Size gridCanvas is always the same at the moment and defines the space where the grid is drawn
canvas.DrawLine(0, 0, gridCanvas.Width, 0, paint);
for (int i = 0; i <= (gridCanvas.Width - gridoffsetX) / pxPerSquare; i )
{
canvas.DrawLine(i * pxPerSquare gridoffsetX, 0, i * pxPerSquare gridoffsetX, gridCanvas.Height, paint);
}
for (int i = 0; i <= (gridCanvas.Height - gridoffsetY) / pxPerSquare; i )
{
canvas.DrawLine(0, i * pxPerSquare gridoffsetY, gridCanvas.Width, i * pxPerSquare gridoffsetY, paint);
}
}
}
最后是原始的 Paint Event
private void Pb1_PaintSurface(object sender, SKPaintGLSurfaceEventArgs e)
{
win2.UpdateDrawing(); //Just calls .Invalidate() on pb2
painter.Update(e, false);
}
示例圖片
所以我的問題是:有沒有辦法讓兩個控制元件幾乎同時繪制而沒有延遲,雖然我不明白為什么第一個 PictureBox 實時繪制而第二個不...謝謝!
uj5u.com熱心網友回復:
在搜索了一天之后,我在發布后立即找到了這個頁面,這對我有幫助: Onpaint events (invalidated) changed execution order after a period normal operation (runtime)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/477906.html
上一篇:C#執行時無法從.exe讀取輸出
