0

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();
        }
Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
yeet
  • 33
  • 4

2 Answers2

1

You should keep the main image too (the image that you haven't applied 50% white transparency, and draw the original image inside your red rectangle:

for example if the main image is original and overlayed one is overlayed:

 Pen pen = Pens.Red;
 e.Graphics.DrawRectangle(pen, rect);
 if(rect.Width - pen.Width * 2 > 0 && rect.Height - pen.Width * 2 > 0)
 {
      Rectangle rect2 = new Rectangle(rect.X + pen.Width, Rect.Y + Pen.Width, rect.Width - pen.Width * 2, rect.Height - pen.Width * 2);
      e.Graphics.DrawImage(original, rext2, rect2, GraphicsUnit.Pixels);
 }
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
1

Draw four rectangles around the center rectangle instead:

+---------------------------------+
|                                 |
|                                 |
+------+-------------------+------+
|      |                   |      |
|      |                   |      |
|      |                   |      |
|      |                   |      |
|      |                   |      |
+------+-------------------+------+
|                                 |
|                                 |
+---------------------------------+
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188