vb.net取屏幕位图 VB读取屏幕指定位置的内容

在VB.net中怎么获取 鼠标在屏幕中的坐标

Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINTAPI) As Long '全屏坐标声明

站在用户的角度思考问题,与客户深入沟通,找到安庆网站设计与安庆网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计制作、网站建设、企业官网、英文网站、手机端网站、网站推广、主机域名、网络空间、企业邮箱。业务覆盖安庆地区。

Private Declare Function ScreenToClient Lib "user32.dll" (ByVal hwnd As Int32, ByRef lpPoint As POINTAPI) As Int32 '窗口坐标声明

Private Structure POINTAPI '声明坐标变量

Public x As Int32 '声明坐标变量为32位

Public y As Int32 '声明坐标变量为32位

End Structure

'以上是声明部分

'以下是窗口部分

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick '用时钟控件来取坐标,窗口上放个Timer组件,Enabled为允许,周期为10到100毫秒均可

Dim P As POINTAPI '声明坐标变量

Dim xx, yy '声明转换要用到的变量

GetCursorPos(P) '获取鼠标在屏幕中的位置

ScreenToClient(Me.Handle.ToInt32, P) '转换为本窗体的坐标

xx = P.x.ToString '把X转换成能显示到文本框的字符串格式

yy = P.y.ToString '把Y转换成能显示到文本框的字符串格式

TextBox1.Text = xx + "和" + yy '文本框的内容为X坐标和Y坐标

End Sub

vb.net桌面中bitmap类如何直接绘制到屏幕?

可以直接显示的。你看下面的示例,使用vb.net画的齿轮:

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

b = New Bitmap(PictureBox1.Width, PictureBox1.Height)

g = Graphics.FromImage(b)

'g.RotateTransform(90)

g.Clear(Color.White)

g.TranslateTransform(PictureBox1.Width / 2, PictureBox1.Height / 2)

g.ScaleTransform(1, -1)

'g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality

DrawCL(g, New PointF(Val(TextBox1.Text), Val(TextBox2.Text)), Val(TextBox3.Text), Val(TextBox4.Text), Val(TextBox5.Text), Val(TextBox6.Text), Val(TextBox7.Text), Val(TextBox8.Text), Val(TextBox9.Text))

DrawCL(g, New PointF(Val(TextBox18.Text), Val(TextBox17.Text)), Val(TextBox16.Text), Val(TextBox15.Text), Val(TextBox14.Text), Val(TextBox13.Text), Val(TextBox12.Text), Val(TextBox11.Text), Val(TextBox10.Text))

PictureBox1.Image = b

End Sub

VB.net 如何将数据转换为位图(Bitmap)所需要的byte()数组?急!!!!

public Byte[] getphoto(string photopath) //参数图片地址,主要用到的类有FileStream

{

string str = photopath;

FileStream file = new FileStream(str, FileMode.Open, FileAccess.Read);

Byte[] bytBLOBData = new Byte[file.Length];

file.Read(bytBLOBData, 0, bytBLOBData.Length);

file.Close();

return bytBLOBData;

}//这是定义函数..

vb求截取区域屏幕图并保存为文件```的代码

Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function ReleaseDC _

Lib "user32" (ByVal hwnd As Long, _

ByVal hdc As Long) As Long

Private Declare Function BitBlt _

Lib "gdi32" (ByVal hDestDC As Long, _

ByVal X As Long, _

ByVal Y As Long, _

ByVal nWidth As Long, _

ByVal nHeight As Long, _

ByVal hSrcDC As Long, _

ByVal xSrc As Long, _

ByVal ySrc As Long, _

ByVal dwRop As Long) As Long

Private Declare Function StretchBlt _

Lib "gdi32" (ByVal hdc As Long, _

ByVal X As Long, _

ByVal Y As Long, _

ByVal nWidth As Long, _

ByVal nHeight As Long, _

ByVal hSrcDC As Long, _

ByVal xSrc As Long, _

ByVal ySrc As Long, _

ByVal nSrcWidth As Long, _

ByVal nSrcHeight As Long, _

ByVal dwRop As Long) As Long

'首先,在窗体上添加个图片框PictureBox控件Name为Picture1

'截取指定位置指定大小的区域,保存到c:\1.bmp文件

Private Sub Form_Load()

Dim hDCtmp As Long

Dim w, h, X, Y

w = 300 ' 要截取屏幕部分的宽度

h = 300 ' 要截取屏幕部分的高度

X = 300 ' 横坐标 / 左边界 Left

Y = 300 ' 纵坐标 / 上边界 Top

Me.Hide

hDCtmp = GetDC(0)

Picture1.BorderStyle = 0

Picture1.AutoRedraw = True

Picture1.Width = w * Screen.TwipsPerPixelX

Picture1.Height = h * Screen.TwipsPerPixelY

Call BitBlt(Picture1.hdc, 0, 0, w, h, hDCtmp, X, Y, vbSrcCopy)

ReleaseDC 0, hDCtmp

SavePicture Picture1.Image, "c:\1.bmp"

Me.Show

End Sub

VB.NET如何实现撷取区域屏幕

既然能够取得整个屏幕,可以取到整个屏幕以后创建一个全屏窗体显示静态的屏幕图像,然后在窗体上实现部分截取。

估计别的截图也是这个原理,因为一点开始截图屏幕就变为静态不动了,肯定是在截图初期复制了当时的屏幕场景

VB 如何获取屏幕图片数据保存在一个二进制数组里面?

这种处理就是纯粹位图处理了

既然已经获取了窗口的设备句柄 hdc

那就 由 CreateDIBSection 生成一个位图对象

用 BitBlt 将窗口图象复制到位图对象中

CreateDIBSection 生成的位图对象的数据就可以放到 Byte数组中了

Private Type BITMAPFILEHEADER

bfType As Integer

bfSize As Long

bfReserved1 As Integer

bfReserved2 As Integer

bfOffBits As Long

End Type

Private Type BITMAPINFOHEADER

biSize As Long

biWidth As Long

biHeight As Long

biPlanes As Integer

biBitCount As Integer

biCompression As Long

biSizeImage As Long

biXPelsPerMeter As Long

biYPelsPerMeter As Long

biClrUsed As Long

biClrImportant As Long

End Type

Private Type BitMap

bmType As Long

bmWidth As Long

bmHeight As Long

bmWidthBytes As Long

bmPlanes As Integer

bmBitsPixel As Integer

bmBits As Long

End Type

Private Const BI_RGB As Long = H0

Private Const DIB_RGB_COLORS = 0

Private Const DIB_PAL_COLORS = 1

Private Const ERRORAPI = 0

Private Declare Function CreateDIBSection Lib "gdi32" (ByVal hdc As Long, pBitmapInfo As BITMAPINFOHEADER, ByVal un As Long, lplpVoid As Long, ByVal Handle As Long, ByVal dw As Long) As Long

Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal XSrc As Long, ByVal YSrc As Long, ByVal dwRop As Long) As Long

Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long

Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long

Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

Private Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long

Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long

Private Declare Function CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Dest As Any, Src As Any, ByVal cb As Long) As Long

Private Declare Sub ZeroMemory Lib "kernel32" Alias "RtlZeroMemory" (Destination As Any, ByVal Length As Long)

Private Declare Function ObjPtr Lib "msvbvm60.dll" Alias "VarPtr" (Var As Object) As Long

Private Declare Function VarPtr Lib "msvbvm60.dll" (Var As Any) As Long

Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (Var() As Any) As Long

Private m_MyCreated As Boolean

Private m_MyDibInfo As BITMAPINFOHEADER

Private m_Myhdc As Long

Private m_Myhbitmap As Long

Private m_MyhbitmapOld As Long

Private m_MyBuffer As Long

Private m_MyptrByte As Long

Private m_MyWidth As Long

Private m_MyHeight As Long

Private m_MyLineAdd As Long

Private m_MyPixelAdd As Long

Private m_MyLineByteWidth As Long

Private m_pvDataPtrAdd As Long

Private m_InitPtrFlag As Boolean

Private pByte0(0 To 0) As Byte

Private pByte0Ptr(0 To 0) As Long

Private OldpByte0 As Long

Private OldpByte0Ptr As Long

Private pByte1(0 To 0) As Byte

Private pByte1Ptr(0 To 0) As Long

Private OldpByte1 As Long

Private OldpByte1Ptr As Long

Private p3Dest(0 To 2) As Byte

Private p3ByteDest(0 To 0) As Long

Private Oldp3ByteDest As Long

Private Oldp3ByteDestPtr As Long

Private p3Src(0 To 2) As Byte

Private p3ByteSrc(0 To 0) As Long

Private Oldp3ByteSrc As Long

Private Oldp3ByteSrcPtr As Long

Private pLongAll(0 To 0) As Long

Private pLongAllPtr(0 To 0) As Long

Private OldpLongAll As Long

Private OldpLongAllPtr As Long

Private Function CreateDIB(ByVal Width As Long, ByVal Height As Long, Optional ByVal iBitCount As Integer = 24) As Boolean

If m_MyCreated And m_MyWidth = Width And m_MyHeight = Height And m_MyDibInfo.biBitCount = iBitCount Then

CreateDIB = True

Exit Function

End If

DestoryDIB

If iBitCount 24 And iBitCount 32 Then

CreateDIB = False

Exit Function

End If

m_MyWidth = Width

m_MyHeight = Height

If m_MyWidth 1 Then m_MyWidth = 1

If m_MyHeight 1 Then m_MyHeight = 1

m_Myhdc = CreateCompatibleDC(0)

If m_Myhdc = 0 Then

m_MyWidth = 0

m_MyHeight = 0

CreateDIB = False

Exit Function

End If

With m_MyDibInfo

.biSize = Len(m_MyDibInfo)

.biWidth = m_MyWidth

.biHeight = m_MyHeight

.biPlanes = 1

.biBitCount = iBitCount

.biCompression = BI_RGB

.biClrImportant = 0

.biXPelsPerMeter = 0

.biYPelsPerMeter = 0

End With

m_Myhbitmap = CreateDIBSection(m_Myhdc, m_MyDibInfo, 0, m_MyBuffer, 0, 0)

If m_Myhbitmap = 0 Then

DeleteDC m_Myhdc

m_Myhdc = 0

m_MyWidth = 0

m_MyHeight = 0

CreateDIB = False

Exit Function

End If

m_MyhbitmapOld = SelectObject(m_Myhdc, m_Myhbitmap)

m_MyptrByte = m_MyBuffer

Dim dpixeladd As Long

dpixeladd = iBitCount \ 8

m_MyPixelAdd = dpixeladd - 3

m_MyLineByteWidth = m_MyWidth * (dpixeladd)

If (m_MyLineByteWidth Mod 4) 0 Then m_MyLineByteWidth = m_MyLineByteWidth + (4 - (m_MyLineByteWidth Mod 4))

m_MyCreated = True

CreateDIB = True

End Function

Private Sub DestoryDIB()

If m_MyCreated Then

DeleteObject (SelectObject(m_Myhdc, m_MyhbitmapOld))

DeleteDC m_Myhdc

m_Myhdc = 0

m_Myhbitmap = 0

m_MyhbitmapOld = 0

m_MyBuffer = 0

m_MyptrByte = 0

m_MyCreated = False

End If

End Sub

CreateDIB 就生成了一个指定宽度,高度和颜色为数的位图对象

位图句柄 m_Myhbitmap

设备描述句柄 m_Myhdc

数据区首地址 m_MyBuffer

生成位图对象后,就可以通过BitBlt做需要的处理

要取数时

ReDim bbuffer(0 To m_MyLineByteWidth * m_MyHeight) As Byte

CopyMemory bbuffer(0), ByVal m_MyBuffer, m_MyLineByteWidth * m_MyHeight

就可以把位图数据放到Byte数组中了

如果希望效率更高的话就借助指针直接读写数据必再复制到 Byte数组中

Private Sub PointInit()

If m_InitPtrFlag Then Exit Sub

MakePoint VarPtrArray(pLongAll), VarPtrArray(pLongAllPtr), OldpLongAll, OldpLongAllPtr

m_InitPtrFlag = True

MakePoint VarPtrArray(p3Dest), VarPtrArray(p3ByteDest), Oldp3ByteDest, Oldp3ByteDestPtr

MakePoint VarPtrArray(p3Src), VarPtrArray(p3ByteSrc), Oldp3ByteSrc, Oldp3ByteSrcPtr

End Sub

Private Sub PointFree()

If m_InitPtrFlag = False Then Exit Sub

FreePoint VarPtrArray(p3Dest), VarPtrArray(p3ByteDest), Oldp3ByteDest, Oldp3ByteDestPtr

FreePoint VarPtrArray(p3Src), VarPtrArray(p3ByteSrc), Oldp3ByteSrc, Oldp3ByteSrcPtr

m_InitPtrFlag = False

FreePoint VarPtrArray(pLongAll), VarPtrArray(pLongAllPtr), OldpLongAll, OldpLongAllPtr

End Sub

Private Sub MakePoint(ByVal DataArrPtr As Long, ByVal pDataArrPtr As Long, _

ByRef OldArrPtr As Long, ByRef OldpArrPtr As Long)

Dim TempLng As Long

Dim TempPtr As Long

If m_InitPtrFlag Then

pLongAllPtr(0) = DataArrPtr

TempLng = pLongAll(0) + m_pvDataPtrAdd

pLongAllPtr(0) = pDataArrPtr

TempPtr = pLongAll(0) + m_pvDataPtrAdd

pLongAllPtr(0) = TempPtr

OldpArrPtr = pLongAll(0)

pLongAll(0) = TempLng

pLongAllPtr(0) = TempLng

OldArrPtr = pLongAll(0)

Else

CopyMemory TempLng, ByVal DataArrPtr, 4

TempLng = TempLng + m_pvDataPtrAdd

CopyMemory TempPtr, ByVal pDataArrPtr, 4

TempPtr = TempPtr + m_pvDataPtrAdd

CopyMemory OldpArrPtr, ByVal TempPtr, 4

CopyMemory ByVal TempPtr, TempLng, 4

CopyMemory OldArrPtr, ByVal TempLng, 4

End If

End Sub

Private Sub FreePoint(ByVal DataArrPtr As Long, ByVal pDataArrPtr As Long, ByVal OldArrPtr As Long, ByVal OldpArrPtr As Long)

Dim TempPtr As Long

If m_InitPtrFlag Then

pLongAllPtr(0) = DataArrPtr

pLongAllPtr(0) = pLongAll(0) + m_pvDataPtrAdd

pLongAll(0) = OldArrPtr

pLongAllPtr(0) = pDataArrPtr

pLongAllPtr(0) = pLongAll(0) + m_pvDataPtrAdd

pLongAll(0) = OldpArrPtr

Else

CopyMemory TempPtr, ByVal DataArrPtr, 4

CopyMemory ByVal (TempPtr + m_pvDataPtrAdd), OldArrPtr, 4

CopyMemory TempPtr, ByVal pDataArrPtr, 4

CopyMemory ByVal (TempPtr + m_pvDataPtrAdd), OldpArrPtr, 4

End If

End Sub

Private Sub Form_Load()

m_pvDataPtrAdd = 12

m_InitPtrFlag = False

PointInit

End Sub

Private Sub Form_Unload(Cancel As Integer)

PointFree

End Sub

Private Sub Command1_Click()

p3ByteDest(0) = m_MyBuffer

p3ByteSrc(0) = m_MyBuffer

'可以通过 p3Dest(0),p3Dest(1),p3Dest(2),p3Src(0).p3Src(1),p3Src(2)直接读写位图数据

p3Dest(0) = p3Src(0)

p3ByteDest(0) = p3ByteDest(0) + 1

p3ByteSrc(0) = p3ByteSrc(0) + 1

End Sub


文章标题:vb.net取屏幕位图 VB读取屏幕指定位置的内容
地址分享:http://azwzsj.com/article/hjeess.html