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
怎么保存数据请帮忙修改
展开
 我来答
星球家园
2012-11-24 · TA获得超过826个赞
知道小有建树答主
回答量:481
采纳率:0%
帮助的人:390万
展开全部
红楼说的OK。
来自:求助得到的回答
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友6842af1
2012-06-11 · TA获得超过757个赞
知道小有建树答主
回答量:522
采纳率:0%
帮助的人:252万
展开全部
把位图加载到Picture控件里,你就很方便知道某点的颜色值了。
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式