让.NET截图工具可兼容多台监视器监视器、多台、截图、工具

2023-09-04 03:11:01 作者:竹马青梅

这是另一种截图工具的解决方案是在此公告规定: .NET截图工具

An alternative snipping tool solution was provided in this posting: .NET Equivalent of Snipping Tool

现在有必要使其成为选择的屏幕工作(多显示器系统)。

Now it's necessary to make it work for selected screens (on multi-monitor systems).

在code已被相应修改:

The code has been modified accordingly:

Public Class SnippingTool


    Private Shared _Screen As Screen

    Private Shared BitmapSize As Size

    Private Shared Graph As Graphics


    Public Shared Function Snip(ByVal screen As Screen) As Image

        _Screen = screen

        Dim bmp As New Bitmap(screen.Bounds.Width, screen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)

        Dim gr As Graphics = Graphics.FromImage(bmp)

        Graph = gr


        gr.SmoothingMode = Drawing2D.SmoothingMode.None '###

        BitmapSize = bmp.Size


        Using snipper = New SnippingTool(bmp)

            snipper.Location = New Point(screen.Bounds.Left, screen.Bounds.Top)

            If snipper.ShowDialog() = DialogResult.OK Then
                Return snipper.Image
            End If

        End Using

        Return Nothing


    End Function



    Public Sub New(ByVal screenShot As Image)
        InitializeComponent()
        Me.BackgroundImage = screenShot
        Me.ShowInTaskbar = False
        Me.FormBorderStyle = FormBorderStyle.None


        'Me.WindowState = FormWindowState.Maximized

        Me.DoubleBuffered = True
    End Sub
    Public Property Image() As Image
        Get
            Return m_Image
        End Get
        Set(ByVal value As Image)
            m_Image = Value
        End Set
    End Property
    Private m_Image As Image


    Private rcSelect As New Rectangle()
    Private pntStart As Point

    Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
        ' Start the snip on mouse down
        If e.Button <> MouseButtons.Left Then
            Return
        End If
        pntStart = e.Location
        rcSelect = New Rectangle(e.Location, New Size(0, 0))
        Me.Invalidate()
    End Sub
    Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
        ' Modify the selection on mouse move
        If e.Button <> MouseButtons.Left Then
            Return
        End If
        Dim x1 As Integer = Math.Min(e.X, pntStart.X)
        Dim y1 As Integer = Math.Min(e.Y, pntStart.Y)
        Dim x2 As Integer = Math.Max(e.X, pntStart.X)
        Dim y2 As Integer = Math.Max(e.Y, pntStart.Y)
        rcSelect = New Rectangle(x1, y1, x2 - x1, y2 - y1)
        Me.Invalidate()
    End Sub


    Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
        ' Complete the snip on mouse-up
        If rcSelect.Width <= 0 OrElse rcSelect.Height <= 0 Then
            Return
        End If
        Image = New Bitmap(rcSelect.Width, rcSelect.Height)
        Using gr As Graphics = Graphics.FromImage(Image)
            gr.DrawImage(Me.BackgroundImage, New Rectangle(0, 0, Image.Width, Image.Height), rcSelect, GraphicsUnit.Pixel)
        End Using
        DialogResult = DialogResult.OK
    End Sub
    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        ' Draw the current selection
        Using br As Brush = New SolidBrush(Color.FromArgb(120, Color.White))
            Dim x1 As Integer = rcSelect.X
            Dim x2 As Integer = rcSelect.X + rcSelect.Width
            Dim y1 As Integer = rcSelect.Y
            Dim y2 As Integer = rcSelect.Y + rcSelect.Height
            e.Graphics.FillRectangle(br, New Rectangle(0, 0, x1, Me.Height))
            e.Graphics.FillRectangle(br, New Rectangle(x2, 0, Me.Width - x2, Me.Height))
            e.Graphics.FillRectangle(br, New Rectangle(x1, 0, x2 - x1, y1))
            e.Graphics.FillRectangle(br, New Rectangle(x1, y2, x2 - x1, Me.Height - y2))
        End Using
        Using pen As New Pen(Color.Red, 3)
            e.Graphics.DrawRectangle(pen, rcSelect)
        End Using
    End Sub
    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        ' Allow canceling the snip with the Escape key
        If keyData = Keys.Escape Then
            Me.DialogResult = DialogResult.Cancel
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)
        Me.Size = New Size(_Screen.Bounds.Width, _Screen.Bounds.Height)
        Dim area = _Screen.WorkingArea
        Graph.CopyFromScreen(area.X, area.Y, area.Y, area.Y, BitmapSize)
    End Sub

End Class

但它拒绝正常工作。该剪断不会出现在选定的屏幕上,而不是它的出现无论是在剪断功能画面参数放在了第一位。 如何使其正常工作?

But it refuses to work as expected. The snipper doesn't appear on the selected screen, instead it appears on the first one, regardless of the "screen" parameter in "Snip" function. How to make it work correctly?

更新:最新版本剪断正确显示屏幕上,但空白

UPDATE: The latest snipper version appears on correct screen, but blank.

更新X2 :在code以上已被更新,以反映最新的版本,现在工作正常。

UPDATE X2 : The code above has been updated to reflect the latest version, which now works properly.

推荐答案

该解决方案是使用 Screen.WorkingArea 属性,而不是 Screen.Bounds 。 第二个选项产生不正确的结果与 Graphics.CopyFromScreen 。 code段以上已更新,功能齐全的版本。

The solution is to use Screen.WorkingArea property rather than Screen.Bounds. The second option yields incorrect results with Graphics.CopyFromScreen. Code snippet above has been updated with fully functional version.