VB转如何把24位的BMP转8位BMP

我想把当前文件夹下的一个名为"1.bmp"的24位BMP转为8位的BMP并用SavePicture保存于"2.bmp"中,VB如何实现

第1个回答  2013-09-06
这个需要对bmp的数据格式要一定了解
Private Sub Command1_Click()
On Error Resume Next
Me.ScaleMode = 3
Dim Color() As Long
Dim I, II, lens As Long
Dim III As Long
Dim Pwidth, PHeight As Long
Dim Bs() As Byte
Open "C:\1.bmp" For Binary As #1
ReDim Bs(1 To LOF(1)) '将数据保存到字节
Get #1, , Bs
Close #1
lens = (UBound(Bs) - 54) / 3 '获读相素个数
Pwidth = Bs(19) + 1 '图片宽度
PHeight = lens / Pwidth '图片高度
ReDim Color(1 To lens)
For I = 0 To lens - 1
Color(I + 1) = RGB(Bs(I * 3 + 57), Bs(I * 3 + 56), Bs(I * 3 + 55))
Next
III = 1
For II = PHeight To 1 Step -1
For I = 1 To Pwidth
me.PSet (I, II), Color(III)
III = III + 1
Next
Next
End Sub
sFileName = InputBox("Path:", "BMP文件路径", "C:\pt.bmp")
lFlen = FileLen(sFileName)
ReDim bTemp(lFlen)
iFN = FreeFile

Open sFileName For Binary As iFN
Get #iFN, 1, bTemp() '将BMP文件以二进制方式打开,存入数组
Close iFN
If Not (bTemp(0) = &H42 And bTemp(1) = &H4D) Then MsgBox ("这不是BMP文件"): Exit Sub

DataOffset = DCombineBytes(bTemp(10), bTemp(11), bTemp(12), bTemp(13)) '偏移量
Width = DCombineBytes(bTemp(18), bTemp(19), bTemp(20), bTemp(21)) '位图的宽度
Height = DCombineBytes(bTemp(22), bTemp(23), bTemp(24), bTemp(25)) '位图的高度
PerPixel = WCombineBytes(bTemp(28), bTemp(29)) '每个象素的位数
Compression = bTemp(30) '是否压缩

If Compression <> 0 Then MsgBox ("此程序只能处理非压缩的BMP图像"): Exit Sub
If PerPixel <> 24 And PerPixel <> 32 Then MsgBox ("此程序只能处理24位或32位的BMP图像"): Exit Sub

If PerPixel = 24 Then t1 = 3 Else t1 = 4
For h = Height To 1 Step -1
For w = Width To 1 Step -1
B = bTemp(t)
G = bTemp(t + 1)
R = bTemp(t + 2)
qq = SetPixelV(Form1.hdc, w, h, RGB(R, G, B))
t = t + t1
Next w
Next h
End Sub

Private Function WCombineBytes(lsb As Byte, msb As Byte) As Long
WCombineBytes = CLng(lsb + (msb * 256)) '把word十六进制数换成十进制
End Function

Private Function DCombineBytes(lsb As Byte, msb As Byte, hsb As Byte, ksb As Byte) As Long
DCombineBytes = CLng(lsb + (msb * 256) + (hsb * 65536) + (ksb * 16777216)) '把dword十六进制数换成十进制
End Function
不要说看不懂
第2个回答  2013-09-06
BMP文件的信息头里面有表示这个BMP是多少位的,你把24改成8就行了 。
相似回答