我正在使用 c# 制作一个像 photoshop 这样的绘画项目。我使用 GDI+ 进行绘图。遗憾的是,我无法发布所需声誉点的屏幕截图。编辑:好的,我有足够的代表上传图片。
当画笔大小增加时,我使用鼠标进行的绘图会滞后。我有一个画布缓冲区,它被绘制到画布面板
protected override void OnPaint(PaintEventArgs e)
{
if (canvasBuffer != null)
{
using (Graphics g = e.Graphics)
{
g.DrawImage(canvasBuffer, e.ClipRectangle, e.ClipRectangle, GraphicsUnit.Pixel);
}
}
}
这就是完成任何绘画时会发生的情况。
- 我存储鼠标拖动事件的点列表。
- 我从a点到b点画了一系列圆,以记录点为中心画一条平滑线
- 此绘制是在存储在图层类中的笔划列表中的位图上完成的。这个绘图也是用 CompositingMode.SourceCopy 来实现 alpha 值绘图
- 我有一个 layerBuffer 来存储图层的最终图像。我通过使用 SourceCopy 兼容模式使用透明颜色的绘图清除它来将受笔触影响的更改绘制到此 layerBuffer,然后使用 SourceOver 在笔触列表中绘制位图
- 由于我正在实施的分层系统,我将所有图层缓冲区绘制到一个图片缓冲区。这张图片Buffer最终通过缩放变换绘制到canvasBuffer中。
注意:画布缓冲区的受影响区域以与图层缓冲区相同的方式完成,即清除受影响的部分并重新绘制图片缓冲区的整个受影响部分。如果我不清除之前的绘图,则使用 alpha 值绘图将无法按预期工作。
请帮助我优化此代码或提出一些新方法来提高性能并减少使用鼠标绘制时的延迟。此外,将绘图代码和使用线程的缓冲区的点计算和绘图分开会有帮助吗?
这是代码。
public void PSF_Painted(PSF_PaintEvent e)
{
Layer SelectedLayer = psf.Layers[0];//Get selected layer here
if ((BrushTool)getActiveTool() != null)
{
//getting the pen attributes from the brush tool
BrushTool brushTool = (BrushTool)getActiveTool();
Pen pen = brushTool.getPen();
Brush brush = pen.Brush;
int brushSize = (int)pen.Width;
//loading points data
List<Point> recordedPoints = null;
Point currentPoint = new Point(0, 0);
Point previousPoint = new Point(0, 0);
if (e.RecordedPoints != null)
{
recordedPoints = e.RecordedPoints;
if (recordedPoints.Count > 1)
{
currentPoint = recordedPoints[recordedPoints.Count - 1];
previousPoint = recordedPoints[recordedPoints.Count - 2];
}
else if (recordedPoints.Count == 1)
{
currentPoint = recordedPoints[0];
previousPoint = currentPoint;
}
}
if (e.PaintEventType == PSF_PaintEvent.StrokeStarted)
{
//Console.WriteLine("StrokeStarted");
SelectedLayer.Strokes.Add(new Bitmap(SelectedLayer.Width, SelectedLayer.Height));
}
else if (e.PaintEventType == PSF_PaintEvent.Painting)
{
//Draw the drawing in the bitmap of the layer's stroke data
using (Graphics g = Graphics.FromImage(SelectedLayer.Strokes[SelectedLayer.Strokes.Count - 1]))
{
List<Point> points = Global.GetPointsOnLine(previousPoint.X, previousPoint.Y, currentPoint.X, currentPoint.Y);
for (int i = 0; i < points.Count; i++)
{
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
if (pen.Width == 1)
{
g.FillRectangle(brush, new Rectangle(points[i].X, points[i].Y, brushSize, brushSize));
}
else
{
g.FillEllipse(brush, new Rectangle(points[i].X, points[i].Y, brushSize, brushSize));
}
int xt, xb, yt, yb;
xt = points[i].X;
xb = points[i].X + brushSize;
yt = points[i].Y;
yb = points[i].Y + brushSize;
if (xt < 0) xt = 0;
if (xb > psf.Width) xb = SelectedLayer.Width;
if (yt < 0) yt = 0;
if (yb > psf.Height) yb = SelectedLayer.Height;
//Refresh changes to the affected part of the canvas buffer
Rectangle affectedRect = new Rectangle(xt, yt, xb - xt, yb - yt);
float zf = psf.ZoomFactor;
Rectangle canvasAffectedRect = new Rectangle((int)(affectedRect.X * zf), (int)(affectedRect.Y * zf), (int)(affectedRect.Size.Width * zf), (int)(affectedRect.Size.Height * zf));
SelectedLayer.invalidateLayerBuffer(affectedRect);
invalidateCanvasBuffer(canvasAffectedRect);
}
}
}
else if (e.PaintEventType == PSF_PaintEvent.StrokeCompleted)
{
//Console.WriteLine("StrokeCompleted");
}
}
this.Invalidate();
}