在表单上拖动选择

时间:2015-03-21 16:51:03

标签: c# winforms drag

我第一次搞乱这个。我想找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当然没有任何东西可以显示它的印刷尺寸。

1 个答案:

答案 0 :(得分:0)

我在计算中犯了一个小错误。

创造奇迹的新规范。

        #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