截图工具滞后荧光笔 [英] Snipping tool laggy highlighter

查看:163
本文介绍了截图工具滞后荧光笔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在C#Winforms中创建一个荧光笔.我使用的大多数代码都在此处突出显示的效果(如截图工具) 但是,当我尝试画得太快时,它会滞后.不知道我在做什么错.这是我尝试过的.

I trying to create a highlighter in C# Winforms. Most of the code i used is here Highlight effect like snipping tool But when i try to draw too fast it lags. Not sure what i'm doing wrong. Here is what i have tried.

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);
    }
}

我想做的是:

修改

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();
    }
}

推荐答案

您需要将点存储在列表中,然后要进行平滑绘制,请使用DrawCurve在这些点的基础上绘制一条曲线.另外,您还需要为LineJoinStartCapEndCap设置合适的设置(例如,圆形)的宽笔,然后可以得到如下的高亮图形:

You need to store points in a list and then to have a smooth drawing, draw a curve based on those points using DrawCurve. Also you need to setup a wide pen with suitable settings (For example Round) for LineJoin, StartCap and EndCap, then you can have a highlight drawing like following:

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());
}

这篇关于截图工具滞后荧光笔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆