在窗体上拖动选择

Dragging Selection on form

我是第一次弄乱这个。我想要像 Gyazo 那样的东西。我有下面的代码,但它实际上不会捕获图像。有点晚了,我有点累,所以可能会错过一些非常容易的事情。它现在所做的只是打印一张随机大小的空白图片。

    private Point start = Point.Empty;
    private Point end = Point.Empty;
    private void Form2_MouseDown(object sender, MouseEventArgs e)
    {
        start.X = e.X;
        start.Y = e.Y;
    }

    private void Form2_MouseMove(object sender, MouseEventArgs e)
    {
        Point p1;
        Point p2;
        if (((e.Button & MouseButtons.Left) != 0) && (start != Point.Empty))
        {
            using (Graphics g = this.CreateGraphics())
            {
                p1 = PointToScreen(start);
                end.X = e.X;
                end.Y = e.Y;
                p2 = PointToScreen(end);
                Console.WriteLine(end);
            }
        }
    }

    private void Form2_MouseUp(object sender, MouseEventArgs e)
    {
        Point p1;
        Point p2;
        Console.WriteLine("Mouse Up");
        if ((end != Point.Empty) && (start != Point.Empty))
        {
            using (Graphics g = this.CreateGraphics())
            {
                p1 = PointToScreen(start);
                p2 = PointToScreen(end);
                int x1 = p1.X;
                int y1 = p1.Y;
                int x2 = p2.X;
                int y2 = p2.Y;
                using (Bitmap bmpScreenCapture = new Bitmap(x1,y1))
                {
                    using (Graphics gra = Graphics.FromImage(bmpScreenCapture))
                    {
                        gra.CopyFromScreen(x1, y1, x2, y2, bmpScreenCapture.Size, CopyPixelOperation.SourceCopy);
                        bmpScreenCapture.Save("test.png", ImageFormat.Png);
                        Console.WriteLine("Image Saved");
                    }
                }
            }

        }
        start = Point.Empty;
        end = Point.Empty;
        Console.WriteLine("Image Saved1");
    }

图片:http://imgur.com/zcDMbgk,wH1k0uP,PIsiQjw#1 当然那里什么也没有,但显示它打印了一些尺寸的东西。

我计算出了一点小错误

创造奇迹的新代码。

        #region ============================Dragging============================

    private Point start = Point.Empty;
    private Point end = Point.Empty;
    private void Form2_MouseDown(object sender, MouseEventArgs e)
    {
        start.X = e.X;
        start.Y = e.Y;
    }

    private void Form2_MouseMove(object sender, MouseEventArgs e)
    {
        Point p1;
        Point p2;
        if (((e.Button & MouseButtons.Left) != 0) && (start != Point.Empty))
        {
            using (Graphics g = this.CreateGraphics())
            {
                p1 = PointToScreen(start);
                end.X = e.X;
                end.Y = e.Y;
                p2 = PointToScreen(end);
                Console.WriteLine(end);
            }
        }
    }

    private void Form2_MouseUp(object sender, MouseEventArgs e)
    {
        Point p1;
        Point p2;
        Console.WriteLine("Mouse Up");
        if ((end != Point.Empty) && (start != Point.Empty))
        {
            using (Graphics g = this.CreateGraphics())
            {
                p1 = PointToScreen(start);
                p2 = PointToScreen(end);
                int x1 = p1.X;
                int y1 = p1.Y;
                int x2 = p2.X;
                int y2 = p2.Y;
                int x = x1 - x2;
                int y = y1 - y2;
                Console.WriteLine(x);
                Console.WriteLine(y);
                string[] xsp = x.ToString().Split('-');
                string[] ysp = y.ToString().Split('-');
                int rx = Convert.ToInt32(xsp[1]);
                int ry = Convert.ToInt32(ysp[1]);
                using (Bitmap bmpScreenCapture = new Bitmap(rx, ry, g))
                {
                    using (Graphics gra = Graphics.FromImage(bmpScreenCapture))
                    {
                        gra.CopyFromScreen(x1, y1, 0, 0, bmpScreenCapture.Size, CopyPixelOperation.SourceCopy);
                        string filename = GenerateRandomString(20) + ".png";
                        bmpScreenCapture.Save(Path.GetTempPath() + "" + filename, ImageFormat.Png);
                        Upload(Path.GetTempPath() + "" + filename, filename);
                    }
                }
            }

        }
        start = Point.Empty;
        end = Point.Empty;
    }
    #endregion