参考 :前面我写的文章VB.net类库——4给屏幕截图
有一定的局限性,因为当时只想到了rectangle类的参数是:左上角point as point , width as integer,height as integer,就写从左上角滑到右下角。
基本思路:注意,那边的斜杠不是除以,是或者的意思
在Windows截图中:可以使用win+shift+s来截图,可以全屏,矩形,选择窗口和自定义截图,功能甚是强大。
我们在传参数时,仍然只要在MouseDown时传p1,在MouseUp时传p2,MouseMove啥要不要
看修改后的Cut窗体的代码:
'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)
GDIscr.PointList() '改动在这里
Pic.Image = GDIscr.CutScren(Pic.Image)
Clipboard.SetImage(Pic.Image)
MsgBox("已复制到剪切板,双击图片退出")
ispick = False
End If
End Sub
Private Sub Pic_DoubleClick(sender As Object, e As EventArgs) Handles Pic.DoubleClick
Close()
End Sub
End Class
topMost=true
WindowState=Maximized
在类库里写
Public p3 As Point
Public p4 As Point
Public width As Integer
Public height As Integer
Public LeftTop As Point
Public Function CutScren(bmp As Bitmap)
g = Graphics.FromImage(bmp)
Dim rc As Rectangle = New Rectangle(LeftTop, New Size(width, height)) '起点和长宽,这里有改动
Image = bmp.Clone(rc, Imaging.PixelFormat.Format32bppArgb)
Return Image
End Function
Public Function PointList()
If p1.X < p2.X AndAlso p1.Y < p2.Y Then
p3 = New Point(p2.X, p1.X)
p4 = New Point(p1.X, p2.Y)
width = p3.X - p1.X
height = p4.Y - p1.Y
LeftTop = p1
ElseIf p1.X > p2.X AndAlso p1.Y < p2.Y Then
p3 = New Point(p1.X, p2.Y)
p4 = New Point(p2.X, p1.Y)
width = p1.X - p4.X
height = p2.Y - p4.Y
LeftTop = p4
ElseIf p1.X > p2.X AndAlso p1.Y > p2.Y Then
p3 = New Point(p1.X, p2.X)
p4 = New Point(p2.X, p1.Y)
width = p3.X - p2.X
height = p4.Y - p2.Y
LeftTop = p2
ElseIf p1.X < p2.X AndAlso p1.Y > p2.Y Then
p3 = New Point(p2.X, p1.Y)
p4 = New Point(p1.X, p2.Y)
width = p2.X - p4.X
height = p1.Y - p4.Y
LeftTop = p4
End If
End Function
之前的注释可以把 “两个顶点样式为从左上角划向右下角。”改掉了