当前位置:网站首页>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窗体,注意,显示一个窗体。选择第一个点是你想选择区域的左上角,第二个点是你想选择区域的右下角,看懂代码就可以了,如果把点的顺序反过来截图,则显示不出。
边栏推荐
- Establish a connection with MySQL
- leetcode刷题:字符串02( 反转字符串II)
- 回首望月
- 证券开户安全吗,有没有什么危险呢
- 好物推薦:移動端開發安全工具
- What are the specific steps for opening a stock account? Is it safe to open an account online?
- Mr. Sun's version of JDBC (21:34:25, June 12, 2022)
- 0 basic C language (2)
- Three basic backup methods of mongodb
- Twenty five of offer - all paths with a certain value in the binary tree
猜你喜欢
Development of NFT for digital collection platform
分布式ID生成系统
Dynamic planning 111
Distributed ID generation system
leetcode刷题:字符串04(颠倒字符串中的单词)
慕课11、微服务的用户认证与授权
Matrix calculator design for beginners of linear algebra based on Qt development
第2章 构建自定义语料库
Leetcode: hash table 08 (sum of four numbers)
西瓜书重温(七): 贝叶斯分类器(手推+代码demo)
随机推荐
The two files are merged into a third file.
手机股票注册开户有没有什么风险?安全吗?
mongoDB的三种基础备份方法
基于Qt实现的“合成大西瓜”小游戏
郭明錤:苹果 AR / MR 头显是其有史以来设计最复杂的产品,将于 2023 年 1 月发布
[Shandong University] information sharing for the first and second examinations of postgraduate entrance examination
Yonghui released the data of Lantern Festival: the sales of Tangyuan increased significantly, and several people's livelihood products increased by more than 150%
众多碎石3d材质贴图素材一键即可获取
Serial port application program based on gd32
30. 串联所有单词的子串
【连载】说透运维监控系统01-监控系统概述
浏览器事件循环
基于SSH框架的学生信息管理系统
Dynamic planning 111
【山东大学】考研初试复试资料分享
C primer plus learning notes - 3. Character IO (input / output)
How to install mysql8.0 database under Windows system? (Graphic tutorial)
Disruptor local thread queue_ Use transprocessor processor and workpool to compare consumption - Notes on inter thread communication 005
vue中缓存组件keep-alive
Gd32 USB composite device file descriptor