围绕矩形绘图而不是填充矩形?

时间:2019-03-31 15:49:57

标签: c#

我正在制作一个截图程序供个人使用,其中包含一些很酷的功能。

有一个按钮可以选择屏幕。我制作了一个红色矩形,该矩形绘制在跟随鼠标的第二种形式(不透明度为0.5)上。一切正常。

我现在正在努力寻找一种方法来将不透明度保持在0.5(这样我会得到白色覆盖),并且同时使矩形透明,以便可以看到类似的屏幕(没有第二种形式)不透明)。

基本上,我想像下面这样在红色矩形周围绘制非常浅的白色:https://i.imgur.com/YFztY84.png, 而我的程序执行此操作时:https://i.imgur.com/9FbYvfU.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();
        }

2 个答案:

答案 0 :(得分:1)

您也应该保留主图像(尚未应用50%白色透明度的图像,并在红色矩形内绘制原始图像:

例如,如果主图像是original,而重叠的图像是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);
 }

答案 1 :(得分:1)

改为围绕中心矩形绘制四个矩形:

+---------------------------------+
|                                 |
|                                 |
+------+-------------------+------+
|      |                   |      |
|      |                   |      |
|      |                   |      |
|      |                   |      |
|      |                   |      |
+------+-------------------+------+
|                                 |
|                                 |
+---------------------------------+