I have a panel containing multiple picturebox.
I want to give users the option of selecting any part of any picturebox.
The user will select it by mouse.
I want to draw a semi-transparent rectangle on the picturebox while the mouse move as per the selection.
The code is working fine, but the rectangle is flickering. I want to stop the flickering.
I tried double buffer using how to stop flickering C# winforms
Also, added Invalide using How to force graphic to be redrawn with the invalidate method
But not working. Please help.
My Code:
private Brush selectionBrush = new SolidBrush(Color.FromArgb(70, 76, 255, 0));
private void Picture_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
PictureBox pb = (PictureBox)(sender);
Point tempEndPoint = e.Location;
Rect.Location = new Point(
Math.Min(RecStartpoint.X, tempEndPoint.X),
Math.Min(RecStartpoint.Y, tempEndPoint.Y));
Rect.Size = new Size(
Math.Abs(RecStartpoint.X - tempEndPoint.X),
Math.Abs(RecStartpoint.Y - tempEndPoint.Y));
pb.CreateGraphics().FillRectangle(selectionBrush, Rect);
pb.Invalidate(Rect);
}