Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINTAPI) As Long '全屏坐標(biāo)聲明
站在用戶的角度思考問題,與客戶深入溝通,找到安慶網(wǎng)站設(shè)計與安慶網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站設(shè)計制作、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、主機域名、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋安慶地區(qū)。
Private Declare Function ScreenToClient Lib "user32.dll" (ByVal hwnd As Int32, ByRef lpPoint As POINTAPI) As Int32 '窗口坐標(biāo)聲明
Private Structure POINTAPI '聲明坐標(biāo)變量
Public x As Int32 '聲明坐標(biāo)變量為32位
Public y As Int32 '聲明坐標(biāo)變量為32位
End Structure
'以上是聲明部分
'以下是窗口部分
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick '用時鐘控件來取坐標(biāo),窗口上放個Timer組件,Enabled為允許,周期為10到100毫秒均可
Dim P As POINTAPI '聲明坐標(biāo)變量
Dim xx, yy '聲明轉(zhuǎn)換要用到的變量
GetCursorPos(P) '獲取鼠標(biāo)在屏幕中的位置
ScreenToClient(Me.Handle.ToInt32, P) '轉(zhuǎn)換為本窗體的坐標(biāo)
xx = P.x.ToString '把X轉(zhuǎn)換成能顯示到文本框的字符串格式
yy = P.y.ToString '把Y轉(zhuǎn)換成能顯示到文本框的字符串格式
TextBox1.Text = xx + "和" + yy '文本框的內(nèi)容為X坐標(biāo)和Y坐標(biāo)
End Sub
可以直接顯示的。你看下面的示例,使用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
public Byte[] getphoto(string photopath) //參數(shù)圖片地址,主要用到的類有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;
}//這是定義函數(shù)..
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
'截取指定位置指定大小的區(qū)域,保存到c:\1.bmp文件
Private Sub Form_Load()
Dim hDCtmp As Long
Dim w, h, X, Y
w = 300 ' 要截取屏幕部分的寬度
h = 300 ' 要截取屏幕部分的高度
X = 300 ' 橫坐標(biāo) / 左邊界 Left
Y = 300 ' 縱坐標(biāo) / 上邊界 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
既然能夠取得整個屏幕,可以取到整個屏幕以后創(chuàng)建一個全屏窗體顯示靜態(tài)的屏幕圖像,然后在窗體上實現(xiàn)部分截取。
估計別的截圖也是這個原理,因為一點開始截圖屏幕就變?yōu)殪o態(tài)不動了,肯定是在截圖初期復(fù)制了當(dāng)時的屏幕場景
這種處理就是純粹位圖處理了
既然已經(jīng)獲取了窗口的設(shè)備句柄 hdc
那就 由 CreateDIBSection 生成一個位圖對象
用 BitBlt 將窗口圖象復(fù)制到位圖對象中
CreateDIBSection 生成的位圖對象的數(shù)據(jù)就可以放到 Byte數(shù)組中了
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 就生成了一個指定寬度,高度和顏色為數(shù)的位圖對象
位圖句柄 m_Myhbitmap
設(shè)備描述句柄 m_Myhdc
數(shù)據(jù)區(qū)首地址 m_MyBuffer
生成位圖對象后,就可以通過BitBlt做需要的處理
要取數(shù)時
ReDim bbuffer(0 To m_MyLineByteWidth * m_MyHeight) As Byte
CopyMemory bbuffer(0), ByVal m_MyBuffer, m_MyLineByteWidth * m_MyHeight
就可以把位圖數(shù)據(jù)放到Byte數(shù)組中了
如果希望效率更高的話就借助指針直接讀寫數(shù)據(jù)必再復(fù)制到 Byte數(shù)組中
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)直接讀寫位圖數(shù)據(jù)
p3Dest(0) = p3Src(0)
p3ByteDest(0) = p3ByteDest(0) + 1
p3ByteSrc(0) = p3ByteSrc(0) + 1
End Sub