当前位置:网站首页>VB.net类库——4给屏幕截图,裁剪
VB.net类库——4给屏幕截图,裁剪
2022-06-26 20:40:00 【小虞163】
VB.net用screen类来截图,其实不需要调用api
在类库myScreen里写函数
''' <summary>
''' 获取屏幕图片
''' </summary>
''' <returns>屏幕图片</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>
''' 此函数多用来裁剪获取到的屏幕图片。注意:调用时要先在MouseDown中向GDIscr.p1中传入第一个顶点。
''' 再在MouseUp中向GDIscr.p2传入第二个顶点,顶点样式为从左上角划向右下角。
''' </summary>
''' <param name="bmp">传入截图,也可是其他的图片</param>
''' <returns>裁剪后的图片</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)) '起点和长宽
Image = bmp.Clone(rc, Imaging.PixelFormat.Format32bppArgb)
Return Image
End Function在类中写:
Public p1 As Point
Public p2 As Point公开这两个变量:在主窗体需要调用
调用:test里新建一个窗体叫Cut
加入一个picturebox:


![]()
FromBorderStyle改为none
WindowState改为Maximized,
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)
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主窗体用MouseDown获取第一个点,MouseUp获取第二个点并调用函数,传入myScreen,
ispick的作用是防止二次裁剪出现错误,代码中的msgbox段可以自行修改、或保存图片。
测试:
点screen则获取当前屏幕的截图
点击cut显示Cut窗体,注意,显示一个窗体。选择第一个点是你想选择区域的左上角,第二个点是你想选择区域的右下角,看懂代码就可以了,如果把点的顺序反过来截图,则显示不出。
边栏推荐
- 网上开户万一免五到底安不安全?
- 分布式ID生成系统
- Detailed explanation of shutter textfield
- c语言99乘法表
- Stop being a giant baby
- Sentinelresource annotation details
- [serial] shuotou O & M monitoring system 01 overview of monitoring system
- Is it safe to open a securities account? Is there any danger
- 这些地区考研太疯狂!哪个地区报考人数最多?
- Good thing recommendation: mobile terminal development security tool
猜你喜欢

阿里云个人镜像仓库日常基本使用

Many gravel 3D material mapping materials can be obtained with one click

Arduino uno + DS1302 uses 31 byte static RAM to store data and print through serial port

The relationship between the development of cloud computing technology and chip processor
Mongodb implements creating and deleting databases, creating and deleting tables (sets), and adding, deleting, modifying, and querying data

Leetcode: hash table 08 (sum of four numbers)

Two methods of QT to realize timer

回首望月

Matrix calculator design for beginners of linear algebra based on Qt development

C language 99 multiplication table
随机推荐
Muke 11. User authentication and authorization of microservices
Establish a connection with MySQL
Leetcode question brushing: String 03 (Sword finger offer 05. replace space)
第2章 构建自定义语料库
What are the specific steps for opening a stock account? Is it safe to open an account online?
QT两种方法实现定时器
Sentinelresource annotation details
Leetcode question brushing: String 01 (inverted string)
云计算技术的发展与芯片处理器的关系
Gd32 USB composite device file descriptor
JWT operation tool class sharing
MySQL stored procedure
Redis + guava local cache API combination, performance burst!
Is it safe to open an account for CICC Wealth Online?
Garbage collection mechanism of browser
C exercise. Class list plus records, display records and clear records
飞天+CIPU体为元宇宙带来更大想象空间
Is it safe to open an online account in case of five-year exemption?
MySQL - table creation and management
[serial] shuotou O & M monitoring system 01 overview of monitoring system