当前位置:网站首页>VB. Net class library - 4 screen shots, clipping

VB. Net class library - 4 screen shots, clipping

2022-06-26 21:56:00 Xiaoyu 163

VB.net use screen Class to screenshot , In fact, there is no need to call api

In class library myScreen Write the function in

 ''' <summary>
    '''  Get screen pictures 
    ''' </summary>
    ''' <returns> screen picture </returns>
    Public Function GetScreen()
        Dim scr As Screen = Screen.PrimaryScreen
        Dim recSc As Rectangle = scr.Bounds
        Image = New Bitmap(recSc.Width, recSc.Height)
        g = Graphics.FromImage(Image)
        g.CopyFromScreen(New Point(0, 0), New Point(0, 0), New Size(recSc.Width, recSc.Height))
        Return Image
    End Function
    ''' <summary>
    '''  This function is mainly used to crop the obtained screen image . Be careful : When calling, the MouseDown To the GDIscr.p1 Pass in the first vertex in .
    '''  And then MouseUp To the GDIscr.p2 Pass in the second vertex , The vertex style is drawn from the upper left corner to the lower right corner .
    ''' </summary>
    ''' <param name="bmp"> Incoming screenshot , But also other pictures </param>
    ''' <returns> Cut out pictures </returns>
    Public Function CutScren(bmp As Bitmap)
        g = Graphics.FromImage(bmp)
        Dim rc As Rectangle = New Rectangle(p1, New Size(p2.X - p1.X, p2.Y - p1.Y))   ' Starting point, length and width 
        Image = bmp.Clone(rc, Imaging.PixelFormat.Format32bppArgb)
        Return Image
    End Function

  Write... In the class :

 Public p1 As Point
    Public p2 As Point

Expose these two variables : In the main form, you need to call

call :test Create a new form called Cut

Add a picturebox:

 FromBorderStyle Change it to none

 WindowState Change it to Maximized,

Cut Write code :

'Imports GDI_Make_NET4_5.myScreen
Public Class Cut
    Public GDIscr As New GDI_Make_NET4_5.myScreen
    Private ispick As Boolean
    Public Sub ShowMe(bmp As Bitmap)
        Pic.Location = New Point(0, 0)
        Pic.Width = bmp.Width
        Pic.Height = bmp.Height
        Pic.Image = bmp
        ispick = True
        Me.Show()
    End Sub

    Private Sub Pic_MouseDown(sender As Object, e As MouseEventArgs) Handles Pic.MouseDown
        If ispick = True Then
            GDIscr.p1 = New Point(e.X, e.Y)
        End If
    End Sub

    Private Sub Pic_MouseUp(sender As Object, e As MouseEventArgs) Handles Pic.MouseUp
        If ispick = True Then
            GDIscr.p2 = New Point(e.X, e.Y)
            Pic.Image = GDIscr.CutScren(Pic.Image)
            Clipboard.SetImage(Pic.Image)
            MsgBox(" Copied to clipboard , Double click the picture to exit ")
            ispick = False
        End If
    End Sub

    Private Sub Pic_DoubleClick(sender As Object, e As EventArgs) Handles Pic.DoubleClick
        Close()
    End Sub
End Class

The main form uses MouseDown Get the first point ,MouseUp Get the second point and call the function , Pass in myScreen,

ispick The function of the is to prevent errors in secondary cropping , In code msgbox The segment can be modified by itself 、 Or save the picture .

  test :

spot screen Get a screenshot of the current screen

Click on cut Show Cut forms , Be careful , Show a form . The first point is the upper left corner of the area you want to select , The second point is the lower right corner of the area you want to select , Just understand the code , If the order of points is reversed, the screenshot , It doesn't show .

 

 

原网站

版权声明
本文为[Xiaoyu 163]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206262040131398.html