Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 跨多个显示器捕获屏幕截图_C#_Winforms - Fatal编程技术网

C# 跨多个显示器捕获屏幕截图

C# 跨多个显示器捕获屏幕截图,c#,winforms,C#,Winforms,作为应用程序的一部分,我希望能够在桌面上绘制所选区域的屏幕截图。不久前,我遇到了如何实现这一点的示例代码(如果我记得我将在何处引用的话),该示例运行得很好,但是它在多个显示器上存在问题 它的工作原理是创建一个透明的全屏窗体,然后在其中绘制一个橡皮筋来指定该区域。问题是它在主显示器上生成,并且只允许您抓取该显示器或其右侧的内容。如果你有3个显示器,你的主人是中心,没有办法抓住左边 我的问题是如何允许在所有3个显示器上进行捕获 示例代码: RubberBand.cs public partial c

作为应用程序的一部分,我希望能够在桌面上绘制所选区域的屏幕截图。不久前,我遇到了如何实现这一点的示例代码(如果我记得我将在何处引用的话),该示例运行得很好,但是它在多个显示器上存在问题

它的工作原理是创建一个透明的全屏窗体,然后在其中绘制一个橡皮筋来指定该区域。问题是它在主显示器上生成,并且只允许您抓取该显示器或其右侧的内容。如果你有3个显示器,你的主人是中心,没有办法抓住左边

我的问题是如何允许在所有3个显示器上进行捕获

示例代码:

RubberBand.cs

public partial class RubberBand : Form
{
    public Point lastLoc;
    public Size lastSize;

    bool mouseDown = false;
    Point mouseDownPoint = Point.Empty;
    Point mousePoint = Point.Empty;
    Main mainform;
    Pen pen;
    Rectangle bounds = new Rectangle();

    public RubberBand(Main mainform)
    {
        this.mainform = mainform;
        InitializeComponent();
        this.TopMost = true;
        this.Opacity = .30;
        this.TransparencyKey = System.Drawing.Color.White;
        this.Location = new Point(0, 0);
        DoubleBuffered = true;
        pen = new Pen(System.Drawing.Color.DarkRed, 3);
        pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;

        int maxX = 0;
        int maxY = 0;

        foreach (Screen screen in System.Windows.Forms.Screen.AllScreens)
        {
            int x = screen.Bounds.X + screen.Bounds.Width;
            if (x > maxX)
                maxX = x;
            int y = screen.Bounds.Y + screen.Bounds.Height;
            if (y > maxY)
                maxY = y;

        }
        bounds.X = 0;
        bounds.Y = 0;
        bounds.Width = maxX;
        bounds.Height = maxY;

        this.Size = new Size(bounds.Width, bounds.Height);

    }



    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e);
        mouseDown = true;
        mousePoint = mouseDownPoint = e.Location;
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        base.OnMouseUp(e);
        mouseDown = false;

        // corey
        this.lastLoc = new Point(Math.Min(mouseDownPoint.X, mousePoint.X), Math.Min(mouseDownPoint.Y, mousePoint.Y));
        this.lastSize = new Size(Math.Abs(mouseDownPoint.X - mousePoint.X), Math.Abs(mouseDownPoint.Y - mousePoint.Y));
        this.Close();
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);
        mousePoint = e.Location;
        Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        //base.OnPaint(e);

        Region region = new Region(bounds);

        if (mouseDown)
        {

            Rectangle selectionWindow = new Rectangle(
                Math.Min(mouseDownPoint.X, mousePoint.X),
                Math.Min(mouseDownPoint.Y, mousePoint.Y),
                Math.Abs(mouseDownPoint.X - mousePoint.X),
                Math.Abs(mouseDownPoint.Y - mousePoint.Y));

            // make a hole, where we can see thru this form
            region.Xor(selectionWindow);

            e.Graphics.FillRegion(Brushes.Black, region);

        }
        else
        {
            e.Graphics.FillRegion(Brushes.LightGray, region);
            e.Graphics.DrawLine(pen,
                mousePoint.X, 0,
                mousePoint.X, this.Size.Height);
            e.Graphics.DrawLine(pen,
                0, mousePoint.Y,
                this.Size.Width, mousePoint.Y);

        }
    }
}
partial class RubberBand
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.SuspendLayout();
        //
        // RubberBand
        //
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.White;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Cursor = System.Windows.Forms.Cursors.Cross;
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "RubberBand";
        this.ShowInTaskbar = false;
        this.Text = "RubberBand";
        this.TransparencyKey = System.Drawing.Color.White;
        this.ResumeLayout(false);

    }

    #endregion

}
RubberBand.Designer.cs

public partial class RubberBand : Form
{
    public Point lastLoc;
    public Size lastSize;

    bool mouseDown = false;
    Point mouseDownPoint = Point.Empty;
    Point mousePoint = Point.Empty;
    Main mainform;
    Pen pen;
    Rectangle bounds = new Rectangle();

    public RubberBand(Main mainform)
    {
        this.mainform = mainform;
        InitializeComponent();
        this.TopMost = true;
        this.Opacity = .30;
        this.TransparencyKey = System.Drawing.Color.White;
        this.Location = new Point(0, 0);
        DoubleBuffered = true;
        pen = new Pen(System.Drawing.Color.DarkRed, 3);
        pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;

        int maxX = 0;
        int maxY = 0;

        foreach (Screen screen in System.Windows.Forms.Screen.AllScreens)
        {
            int x = screen.Bounds.X + screen.Bounds.Width;
            if (x > maxX)
                maxX = x;
            int y = screen.Bounds.Y + screen.Bounds.Height;
            if (y > maxY)
                maxY = y;

        }
        bounds.X = 0;
        bounds.Y = 0;
        bounds.Width = maxX;
        bounds.Height = maxY;

        this.Size = new Size(bounds.Width, bounds.Height);

    }



    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e);
        mouseDown = true;
        mousePoint = mouseDownPoint = e.Location;
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        base.OnMouseUp(e);
        mouseDown = false;

        // corey
        this.lastLoc = new Point(Math.Min(mouseDownPoint.X, mousePoint.X), Math.Min(mouseDownPoint.Y, mousePoint.Y));
        this.lastSize = new Size(Math.Abs(mouseDownPoint.X - mousePoint.X), Math.Abs(mouseDownPoint.Y - mousePoint.Y));
        this.Close();
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);
        mousePoint = e.Location;
        Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        //base.OnPaint(e);

        Region region = new Region(bounds);

        if (mouseDown)
        {

            Rectangle selectionWindow = new Rectangle(
                Math.Min(mouseDownPoint.X, mousePoint.X),
                Math.Min(mouseDownPoint.Y, mousePoint.Y),
                Math.Abs(mouseDownPoint.X - mousePoint.X),
                Math.Abs(mouseDownPoint.Y - mousePoint.Y));

            // make a hole, where we can see thru this form
            region.Xor(selectionWindow);

            e.Graphics.FillRegion(Brushes.Black, region);

        }
        else
        {
            e.Graphics.FillRegion(Brushes.LightGray, region);
            e.Graphics.DrawLine(pen,
                mousePoint.X, 0,
                mousePoint.X, this.Size.Height);
            e.Graphics.DrawLine(pen,
                0, mousePoint.Y,
                this.Size.Width, mousePoint.Y);

        }
    }
}
partial class RubberBand
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.SuspendLayout();
        //
        // RubberBand
        //
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.White;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Cursor = System.Windows.Forms.Cursors.Cross;
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "RubberBand";
        this.ShowInTaskbar = false;
        this.Text = "RubberBand";
        this.TransparencyKey = System.Drawing.Color.White;
        this.ResumeLayout(false);

    }

    #endregion

}

this.Location=新点(0,0)与主屏幕左上角相关。主(主)屏幕左侧或上方的屏幕具有负位置坐标。在定位窗口和设置其大小时,必须将其考虑在内

在RubberBand构造函数中:

    int maxX = 0;
    int maxY = 0;
    int minX = 0;
    int minY = 0;

    foreach (Screen screen in System.Windows.Forms.Screen.AllScreens)
    {
        int x = screen.Bounds.X + screen.Bounds.Width;
        if (x > maxX)
            maxX = x;
        int y = screen.Bounds.Y + screen.Bounds.Height;
        if (y > maxY)
            maxY = y;
        if(screen.Bound.X < minX) minX = screen.Bound.X;
        if(screen.Bound.Y < minY) minY = screen.Bound.Y;
    }

    bounds.X = minX;
    bounds.Y = minY;
    bounds.Width = maxX - minX;
    bounds.Height = maxY - minY;

    this.Location = new Point(bounds.X, bounds.Y);
    this.Size = new Size(bounds.Width, bounds.Height);
intmaxx=0;
int-maxY=0;
int minX=0;
int minY=0;
foreach(System.Windows.Forms.Screen.AllScreens中的屏幕)
{
int x=screen.Bounds.x+screen.Bounds.Width;
如果(x>maxX)
maxX=x;
int y=screen.Bounds.y+screen.Bounds.Height;
如果(y>maxY)
maxY=y;
如果(screen.Bound.X