VB获取位图图像的像素数据,并输出RGB的值
PrivateDeclareFunctionGetDIBitsLib"gdi32"(ByValaHDCAsLong,ByValhBitmapAsLong,ByValnSt...
Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
Private Declare Function SetDIBits Lib "gdi32" (ByVal hdc As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
Private Type BITMAPINFOHEADER '40 bytes
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 RGBQUAD
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type
Private Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors As RGBQUAD
End Type
Private Const DIB_RGB_COLORS = 0 ' color table in RGBs
Private Sub Command1_Click()
Dim BITMAP As BITMAPINFO
Dim ImgData() As Byte
Dim lIndex As Long
With BITMAP
.bmiHeader.biSize = 40
End With
If GetDIBits(Picture1.hdc, Picture1.Picture.Handle, 0, 0, 0, BITMAP, DIB_RGB_COLORS) <> 0 Then '获取图像位图数据大小
ReDim ImgData(BITMAP.bmiHeader.biSizeImage) '重定义数组准备接收位图数据
If GetDIBits(Picture1.hdc, Picture1.Picture.Handle, 0, BITMAP.bmiHeader.biHeight - 1, ImgData(0), BITMAP, DIB_RGB_COLORS) <> 0 Then '正式获取位图数据并将数据存入定义的数组中
'注意数组中颜色分量的存储顺序为:B、G、R(并不是R、G、B),如果你需要对图像进行处理只需改变数组中的数据即可完成
'**************** 下面这段程序是将图像亮度变暗(只作为演示,其它效果你可以自己动手做) ***********************
'注意:第一条扫描线的位置是从左下角开始
For lIndex = 0 To BITMAP.bmiHeader.biSizeImage
If ImgData(lIndex) - 100 < 0 Then
ImgData(lIndex) = 0
Else
ImgData(lIndex) = ImgData(lIndex) - 100
End If
Next
'*********************************************************************
SetDIBits Picture2.hdc, Picture2.Image.Handle, 0, BITMAP.bmiHeader.biHeight - 1, ImgData(0), BITMAP, DIB_RGB_COLORS '将位图数据写入PICTURE2中
End If
End If
End Sub
怎么保存数据请帮忙修改 展开
Private Declare Function SetDIBits Lib "gdi32" (ByVal hdc As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
Private Type BITMAPINFOHEADER '40 bytes
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 RGBQUAD
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type
Private Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors As RGBQUAD
End Type
Private Const DIB_RGB_COLORS = 0 ' color table in RGBs
Private Sub Command1_Click()
Dim BITMAP As BITMAPINFO
Dim ImgData() As Byte
Dim lIndex As Long
With BITMAP
.bmiHeader.biSize = 40
End With
If GetDIBits(Picture1.hdc, Picture1.Picture.Handle, 0, 0, 0, BITMAP, DIB_RGB_COLORS) <> 0 Then '获取图像位图数据大小
ReDim ImgData(BITMAP.bmiHeader.biSizeImage) '重定义数组准备接收位图数据
If GetDIBits(Picture1.hdc, Picture1.Picture.Handle, 0, BITMAP.bmiHeader.biHeight - 1, ImgData(0), BITMAP, DIB_RGB_COLORS) <> 0 Then '正式获取位图数据并将数据存入定义的数组中
'注意数组中颜色分量的存储顺序为:B、G、R(并不是R、G、B),如果你需要对图像进行处理只需改变数组中的数据即可完成
'**************** 下面这段程序是将图像亮度变暗(只作为演示,其它效果你可以自己动手做) ***********************
'注意:第一条扫描线的位置是从左下角开始
For lIndex = 0 To BITMAP.bmiHeader.biSizeImage
If ImgData(lIndex) - 100 < 0 Then
ImgData(lIndex) = 0
Else
ImgData(lIndex) = ImgData(lIndex) - 100
End If
Next
'*********************************************************************
SetDIBits Picture2.hdc, Picture2.Image.Handle, 0, BITMAP.bmiHeader.biHeight - 1, ImgData(0), BITMAP, DIB_RGB_COLORS '将位图数据写入PICTURE2中
End If
End If
End Sub
怎么保存数据请帮忙修改 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询