截断工具滞后荧光笔

时间:2018-02-20 20:18:27

标签: c# winforms draw paint

我试图在C#Winforms中创建一个荧光笔。我使用的大多数代码都在这里Highlight effect like snipping tool 但是当我试图画得太快时,它会滞后。不知道我做错了什么。这是我尝试过的。

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Bitmap bmp = (Bitmap)pictureBox1.Image;
    using (Graphics g = Graphics.FromImage(pictureBox1.Image))
    {
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        IntPtr hBMP = bmp.GetHbitmap();
        IntPtr bDC = g.GetHdc();
        IntPtr mDC = CreateCompatibleDC(bDC);
        IntPtr oDC = SelectObject(mDC, hBMP);

        int useColor = System.Drawing.ColorTranslator.ToWin32(brush.Color);
        IntPtr pen = CreatePen(PS_SOLID, _w, (uint)useColor);
        IntPtr xDC = SelectObject(mDC, pen);
        SetROP2(mDC, R2_MASKPEN);
        Rectangle dest = new Rectangle(currentX - _x, currentY - _y, _w, _h);
        Rectangle(mDC, dest.Left, dest.Top, dest.Right, dest.Bottom);
        SetROP2(mDC, R2_COPYPEN);

        BitBlt(bDC, 0, 0, bmp.Width, bmp.Height, mDC, 0, 0, SRCCOPY);
        SelectObject(mDC, xDC);
        DeleteObject(pen);
        g.ReleaseHdc(bDC);
        SelectObject(mDC, oDC);
        DeleteDC(mDC);
        DeleteObject(hBMP);
    }
}

enter image description here

我想做的是: enter image description here

修改

bool draw = false;
int currentX = 0;
int currentY = 0;

//Rectangle width/height
int _w = 6;
int _h = 18

//These are just to center the cursor according to the width and height of rectangle.
int _x = 3; //(half of _w)
int _y = 9; //(half of _h)

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    draw = true;
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left && draw == true)
    {
        currentX = e.X;
        currentY = e.Y;
        pictureBox1.Invalidate();
    }
}

1 个答案:

答案 0 :(得分:3)

您需要在列表中存储点,然后使用平滑绘图,使用DrawCurve基于这些点绘制曲线。此外,您还需要为LineJoinStartCapEndCap设置适当设置(例如圆形)的宽笔,然后您可以使用如下高亮显示图:

enter image description here

List<List<Point>> Lines = new List<List<Point>>();
bool drawing = false;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    Lines.Add(new List<Point>());
    Lines.Last().Add(e.Location);
    drawing = true;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (drawing) { Lines.Last().Add(e.Location); pictureBox1.Invalidate(); }
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    if (drawing)
    {
        this.drawing = false;
        Lines.Last().Add(e.Location);
        pictureBox1.Invalidate();
    }
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    using (var pen = new Pen(Color.FromArgb(150, Color.Yellow), 30)
    {
        LineJoin = System.Drawing.Drawing2D.LineJoin.Round,
        StartCap = System.Drawing.Drawing2D.LineCap.Round,
        EndCap = System.Drawing.Drawing2D.LineCap.Round,
    })
        foreach (var item in Lines)
            e.Graphics.DrawCurve(pen, item.ToArray());
}