I'm making a screenshot-program for personal use with some cool features.
There is one button where I can make a selection of the screen. I made a red rectangle that draws on a 2nd form (with 0.5 opacity) that follows the mouse. All that works fine.
I'm struggling now to find a way of keeping the opacity at 0.5 (so I get that white overlay) and, at the same time, making the rectangle transparent so I can see the screen like it (without the 2nd form opacity).
Basically, I want to draw very light white around the red rectangle like here: https://i.stack.imgur.com/hap0J.png, while my program does this: https://i.stack.imgur.com/aBIKY.png
private void Selection()
{
Hide();
form2 = new Form();
form2.Opacity = .5;
form2.Cursor = Cursors.Cross;
form2.ControlBox = false;
form2.MaximizeBox = false;
form2.MinimizeBox = false;
form2.FormBorderStyle = FormBorderStyle.None;
form2.WindowState = FormWindowState.Maximized;
form2.MouseDown += form2_MouseDown;
form2.MouseMove += form2_MouseMove;
form2.Paint += form2_Paint;
form2.MouseUp += form2_MouseUp;
form2.Show();
}
void form2_MouseDown(object sender, MouseEventArgs e)
{
MD = e.Location;
}
void form2_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
Point MM = e.Location;
rect = new Rectangle(Math.Min(MD.X, MM.X), Math.Min(MD.Y, MM.Y), Math.Abs(MD.X - MM.X), Math.Abs(MD.Y - MM.Y));
form2.Invalidate();
}
void form2_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.HighSpeed;
if (rect != null && rect.Width > 0 && rect.Height > 0)
{
e.Graphics.DrawRectangle(Pens.Red, rect);
}
}
void form2_MouseUp(object sender, MouseEventArgs e)
{
form2.Hide();
Screen scr = Screen.AllScreens[0];
Bitmap bmp = new Bitmap(rect.Width, rect.Height);
using (Graphics G = Graphics.FromImage(bmp))
{
G.CopyFromScreen(rect.Location, Point.Empty, rect.Size, CopyPixelOperation.SourceCopy);
pictureBox1.Image = bmp;
bmp.Save(System.IO.Path.GetTempPath() + "tempOnra2.png");
Clipboard.SetImage(pictureBox1.Image);
}
form2.Close();
Show();
}