带范围截屏和保存图片代码

Imports System.Runtime.InteropServices

Imports System.Drawing.Imaging

Public Class Form1

Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As Int32) As Int32

Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal dwRop As Integer) As Integer

Private picture As Bitmap = Nothing '以picture作为图片格式的声

Public Sub xianshi()

Dim bmpOrgin As Bitmap = PictureBox1.Image

Dim bmpNew As New Bitmap(zb2x - zb1x, zb2y - zb1y, PictureBox1.CreateGraphics)

Dim ee As Graphics = Graphics.FromImage(bmpNew)

' 创建要在其中绘制图像的目标矩形.指定所绘制图像的位置和大小。 将图像进行缩放以适合该矩形

Dim destRect As New Rectangle(0, 0, zb2x - zb1x, zb2y - zb1y)

' 创建要从中提取图像的一部分的源矩形.

Dim srcRect As New Rectangle(zb1x, zb1y, zb2x - zb1x, zb2y - zb1y) '原来图形(50,50)-(70,70) 这一片

ee.DrawImage(bmpOrgin, destRect, srcRect, GraphicsUnit.Pixel)

PictureBox1.Image = bmpNew

End Sub

Public Sub capture_window()

Dim capture1 As IntPtr = CreateDC("DISPLAY", Nothing, Nothing, Nothing)

Dim get1 As Graphics = Graphics.FromHdc(capture1)

'创建一个新的Graphics对象

picture = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, get1)

'根据屏幕大小创建一个相同大小的Bitmap

Dim get2 As Graphics = Graphics.FromImage(picture)

Dim get3 As IntPtr = get1.GetHdc() '获取屏幕的句柄

Dim get4 As IntPtr = get2.GetHdc() '获取位图的句柄

BitBlt(get4, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, get3, _

0, 0, 13369376) '把当前屏幕复制到位图中

get1.ReleaseHdc(get3) '释放屏幕句柄

get2.ReleaseHdc(get4) '释放位图句柄

picture.Save("C://CapturePicture.jpg", ImageFormat.Jpeg)

'MessageBox.Show(" 已经把当前截取屏幕保存到CapturePicture.jpg,检查程序根目录")

'Me.Visible = True

Me.Top = 100

PictureBox1.Image = picture

End Sub

Private Sub 截屏ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 截屏ToolStripMenuItem.Click

'Me.Visible = False

Me.Top = -600

capture_window() '调用函数,开始捕获程序

End Sub

Private Sub 退出ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出ToolStripMenuItem.Click

If zb1x = 0 Then

MsgBox("请先选择要截取的范围")

Else

Dim bmpOrgin As Bitmap = PictureBox1.Image

Dim bmpNew As New Bitmap(zb2x - zb1x, zb2y - zb1y, PictureBox1.CreateGraphics)

Dim ee As Graphics = Graphics.FromImage(bmpNew)

' 创建要在其中绘制图像的目标矩形.指定所绘制图像的位置和大小。 将图像进行缩放以适合该矩形

Dim destRect As New Rectangle(0, 0, zb2x - zb1x, zb2y - zb1y)

' 创建要从中提取图像的一部分的源矩形.

Dim srcRect As New Rectangle(zb1x, zb1y, zb2x - zb1x, zb2y - zb1y) '原来图形(50,50)-(70,70) 这一片

ee.DrawImage(bmpOrgin, destRect, srcRect, GraphicsUnit.Pixel)

PictureBox1.Image = bmpNew

End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Me.Top = -700 '隐藏窗体

Form2.Show()  '打开范围选定窗体

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

TextBox1.Text = System.Windows.Forms.Cursor.Position.X.ToString & "," & System.Windows.Forms.Cursor.Position.Y.ToString

If z = 1 Then

capture_window()

xianshi()

End If

End Sub

'说明:打开程序之后马上把当前屏幕截屏保存

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim capture1 As IntPtr = CreateDC("DISPLAY", Nothing, Nothing, Nothing)

Dim get1 As Graphics = Graphics.FromHdc(capture1)

'创建一个新的Graphics对象

picture = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, get1)

'根据屏幕大小创建一个相同大小的Bitmap

Dim get2 As Graphics = Graphics.FromImage(picture)

Dim get3 As IntPtr = get1.GetHdc() '获取屏幕的句柄

Dim get4 As IntPtr = get2.GetHdc() '获取位图的句柄

BitBlt(get4, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, get3, _

0, 0, 13369376) '把当前屏幕复制到位图中

get1.ReleaseHdc(get3) '释放屏幕句柄

get2.ReleaseHdc(get4) '释放位图句柄

picture.Save("C://CapturePicture.bmp", ImageFormat.Bmp)

PictureBox1.Image = picture

End Sub

End Class

Public Class Form2

Private rectList As New List(Of Rectangle)

Private pt As Point

Private bmpOld As Bitmap

Private zb1 As Point

Private zb2 As Point

Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown

pt.X = e.X

pt.Y = e.Y

zb1 = System.Windows.Forms.Cursor.Position

End Sub

Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

If e.Button = Windows.Forms.MouseButtons.Left Then

Dim g As Graphics = Graphics.FromImage(Me.BackgroundImage)

'擦除之前绘制的内容

Dim brush As New SolidBrush(Me.BackColor)

g.FillRectangle(brush, Me.ClientRectangle)

brush.Dispose()

'绘制之前的,包括当前的内容

Dim x, y, w, h As Integer

x = Math.Min(pt.X, e.X)

y = Math.Min(pt.Y, e.Y)

w = Math.Abs(pt.X - e.X)

h = Math.Abs(pt.Y - e.Y)

For i As Integer = 0 To rectList.Count - 1

g.DrawRectangle(Pens.Red, rectList(i))

Next

g.DrawRectangle(Pens.Red, x, y, w, h)

g.Dispose()

g = Me.CreateGraphics()

g.DrawImage(Me.BackgroundImage, 0, 0)

g.Dispose()

End If

End Sub

Private Sub Form1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp

zb2 = System.Windows.Forms.Cursor.Position

Dim x, y, w, h As Integer

x = Math.Min(pt.X, e.X)

y = Math.Min(pt.Y, e.Y)

w = Math.Abs(pt.X - e.X)

h = Math.Abs(pt.Y - e.Y)

rectList.Add(New Rectangle(x, y, w, h))

Form1.TextBox2.Text = zb1.X.ToString & "," & zb1.Y.ToString

Form1.TextBox3.Text = zb2.X.ToString & "," & zb2.Y.ToString

zb1x = zb1.X

zb1y = zb1.Y

zb2x = zb2.X

zb2y = zb2.Y

z = 1

Form1.Show()

Form1.Top = 100

Me.Close()

End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'窗体距顶部和左部为0,也就是左上角开始

'透明度为70%透明

'无任何按钮

'窗体全屏幕显示

Me.Top = 0

Me.Left = 0

Me.Opacity = 0.5

Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None

Me.WindowState = FormWindowState.Maximized

Me.TransparencyKey = Color.Red

Me.BackColor = Color.Blue

TextBox1.Text = My.Computer.Screen.Bounds.Width

TextBox2.Text = My.Computer.Screen.Bounds.Height

Me.BackgroundImage = New Bitmap(Width, Height)

End Sub

Private Sub Form1_ResizeEnd(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.ResizeEnd

If bmpOld Is Nothing Then Return

Me.BackgroundImage = New Bitmap(Width, Height)

Dim g As Graphics = Graphics.FromImage(Me.BackgroundImage)

g.DrawImage(bmpOld, 0, 0)

g.Dispose()

bmpOld.Dispose()

bmpOld = Nothing

End Sub

Private Sub Form1_ResizeBegin(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.ResizeBegin

If BackgroundImage Is Nothing Then Return

bmpOld = Me.BackgroundImage

End Sub

Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize

Me.BackgroundImage = Nothing

End Sub

Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

If bmpOld Is Nothing Then Return

e.Graphics.DrawImage(bmpOld, 0, 0)

End Sub

End Class

Module Module1

'全局变量定义

Public zb1x As Integer = 0

Public zb1y As Integer = 0

Public zb2x As Integer = 0

Public zb2y As Integer = 0

Public z As Integer = 0 '用来设置程序自动刷新的变量,在form2跳回form1时生效

End Module

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容