带选择矩形的部分截图c#

时间:2015-03-10 11:32:14

标签: c# selection screenshot partial

我正在尝试对屏幕进行部分截图并将其放入图片框中。用户必须绘制一个矩形,然后我必须得到矩形内的部分。

protected override void OnMouseDown(MouseEventArgs e)
{
    mRect = new Rectangle(e.X, e.Y, 0, 0);
    this.Invalidate();
}

protected override void OnMouseMove(MouseEventArgs e)
{
    if( e.Button == MouseButtons.Left)
    {
        mRect = new Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top);
        this.Invalidate();
    }
}


protected override void OnPaint(PaintEventArgs e)
{

    using(Pen pen = new Pen(Color.Red, 1))
    {
    e.Graphics.DrawRectangle(pen, mRect);
    }
}

使用此代码我可以绘制一个重复,但我不知道如何做截图。也许与功能CopyToScreen?

如果有人有想法?

谢谢!

2 个答案:

答案 0 :(得分:1)

这是我使用的一些代码 - 它抓住了整个桌面

    public static Bitmap GetScreenAsBmp()
    {
        int min_x=0, max_x=0, min_y=0, max_y=0;
        foreach (Screen s in Screen.AllScreens)
        {
            min_x = MinOf(min_x, s.Bounds.Left);
            max_x = MaxOf(max_x, s.Bounds.Right);
            min_y = MinOf(min_y, s.Bounds.Top);
            max_y = MaxOf(max_y, s.Bounds.Bottom);
        }
        Bitmap bmp = new Bitmap(max_x - min_x, max_y - min_y);
        Graphics dst_dc =  Graphics.FromImage(bmp);
        dst_dc.CopyFromScreen(min_x, min_y, 0, 0, new Size(max_x - min_x, max_y - min_y));
        dst_dc.Dispose();
        return bmp;
    }

答案 1 :(得分:0)

这是解决问题的完整解决方案。

Button触发,然后是:

  • 创建并设置第二个Form
    • 是透明的,没有边框等,并且填满了屏幕
    • 包含绘制Rectangle
    • 所需的所有事件
  • 在绘制矩形后,即在MouseUp
  • 之后复制屏幕
  • 并将Bitmap复制到第一张表格上PictureBox的图片中。
  • 最后关闭第二张表格。在屏幕复制过程中,两个窗口都是隐藏的,因此您可以抓取屏幕本身及其所有内容。

请注意,许多视频叠加层无法以这种方式捕获!

请注意,此代码始终从第一个屏幕捕获!

为了获得最佳可用性,用户应该以某种方式被告知在按下Button并且窗口消失后他应该画出画框!

要使用它,只需向Button添加PictureBoxForm ..

public partial class Form1 : Form
{
  public Form1()
  {
      InitializeComponent();
  }

  Form form2;
  Point MD = Point.Empty;
  Rectangle rect = Rectangle.Empty;

  private void button2_Click(object sender, EventArgs e)
  {
     Hide();
     form2 = new Form();
     form2.BackColor = Color.Wheat;
     form2.TransparencyKey = form2.BackColor;
     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.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;
     }
     form2.Close();
     Show();
  }

}