请教:如何获取24位BMP图片的RGB分量,我现在有一24位的BMP图片,现在想要将此图片的各个像素的RGB值保存为(R,G,B)形式的txt文本文档,如何实现?

zhmh_1982 2008-08-26 11:33:43
请教:如何获取24位BMP图片的RGB分量,我现在有一24位的BMP图片,现在想要将此图片的各个像素的RGB值保存为(R,G,B)形式的txt文本文档,如何实现?

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

Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long

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 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 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
Dim a As String
a = Text2.Text

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 / 3, 2)
If GetDIBits(picture1.hdc, picture1.Picture.Handle, 0, BITMAP.bmiHeader.biHeight - 1, ImgData(0, 0), BITMAP, DIB_RGB_COLORS) <> 0 Then

'ImgData(X,0)中保存的为蓝色分量值
'ImgData(X,1)中保存的为绿色分量值
'ImgData(X,2)中保存的为红色分量值
Point_x.Text = BITMAP.bmiHeader.biWidth
Point_y.Text = BITMAP.bmiHeader.biHeight


List1.Clear
For lIndex = 0 To BITMAP.bmiHeader.biSizeImage / 3

List1.AddItem ImgData(lIndex, 2) & ";" & ImgData(lIndex, 1) & ";" & ImgData(lIndex, 0)


Next

End If
End If
End Sub

这样list1中显示是否是我要的东西呢?但是好像又是不对的,请指教
...全文
520 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
laviewpbt 2008-08-26
  • 打赏
  • 举报
回复
ImgData 数组大小不正确
lsftest 2008-08-26
  • 打赏
  • 举报
回复
用GetBitmapBits读出来的就是啊。。。注意它的顺序是BGR,而不是传统的RGB。建议先看看BMP的数据格式,推荐zyl910兄写的《用VB写高效的图像处理程序》:
http://blog.csdn.net/zyl910/archive/2003/08/18/14789.aspx
熊孩子开学喽 2008-08-26
  • 打赏
  • 举报
回复
给你个简单的例子看看:
http://download.csdn.net/source/455619

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧