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);
}
}
What I'm trying to do is:
Edit
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();
}
}