当前位置:网站首页>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窗体,注意,显示一个窗体。选择第一个点是你想选择区域的左上角,第二个点是你想选择区域的右下角,看懂代码就可以了,如果把点的顺序反过来截图,则显示不出。
边栏推荐
- 0 basic C language (0)
- 郭明錤:苹果 AR / MR 头显是其有史以来设计最复杂的产品,将于 2023 年 1 月发布
- 众多碎石3d材质贴图素材一键即可获取
- Three basic backup methods of mongodb
- 第2章 构建自定义语料库
- 证券开户安全吗,有没有什么危险呢
- [most detailed] latest and complete redis interview (42 tracks)
- Yonghui released the data of Lantern Festival: the sales of Tangyuan increased significantly, and several people's livelihood products increased by more than 150%
- GEE:计算image区域内像素最大最小值
- 云计算技术的发展与芯片处理器的关系
猜你喜欢

Redis + guava local cache API combination, performance burst!

Development of NFT for digital collection platform

windows系統下怎麼安裝mysql8.0數據庫?(圖文教程)

leetcode刷题:字符串02( 反转字符串II)

Disruptor本地线程队列_使用transProcessor处理器和WorkPool两种方式进行消费对比---线程间通信工作笔记005

The source code that everyone can understand (I) the overall architecture of ahooks

How to install mysql8.0 database under Windows system? (Graphic tutorial)

QT两种方法实现定时器

Looking back at the moon

Arduino uno + DS1302 uses 31 byte static RAM to store data and print through serial port
随机推荐
分布式ID生成系统
Sword finger offer II 098 Number of paths / Sword finger offer II 099 Sum of minimum paths
Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023
MySQL - database creation and management
710. 黑名单中的随机数
SentinelResource注解详解
【 protobuf 】 quelques puits causés par la mise à niveau de protobuf
leetcode刷题:字符串04(颠倒字符串中的单词)
证券开户安全吗,有没有什么危险呢
记录一次Redis大Key的排查
基于Qt实现的“合成大西瓜”小游戏
慕课8、服务容错-Sentinel
Leetcode question brushing: String 03 (Sword finger offer 05. replace space)
[Bayesian classification 2] naive Bayesian classifier
Looking back at the moon
Many gravel 3D material mapping materials can be obtained with one click
【贝叶斯分类2】朴素贝叶斯分类器
mysql存储过程
与 MySQL 建立连接
IDEA 报错:Process terminated【已解决】