VB .bmp图片数据问题! 50
VB中我用DimS()AsByte'定义图片数据数组Open"C:\1.bmp"ForBinaryAccessReadAs#1ReDimS(0To4213)Get#1,,...
VB中我用
Dim S() As Byte'定义图片数据数组
Open "C:\1.bmp" For Binary Access Read As #1
ReDim S(0 To 4213)
Get #1, , S()
Close #1
获取了1.bmp图片的数据数组 ,请问如何用该数据数组将图片画在窗体上?不是直接由1.bmp图片的路径加载到窗体上!最好源码说一下! 展开
Dim S() As Byte'定义图片数据数组
Open "C:\1.bmp" For Binary Access Read As #1
ReDim S(0 To 4213)
Get #1, , S()
Close #1
获取了1.bmp图片的数据数组 ,请问如何用该数据数组将图片画在窗体上?不是直接由1.bmp图片的路径加载到窗体上!最好源码说一下! 展开
2个回答
展开全部
Option Explicit
Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Sub Form_Click()
Dim iFN As Integer
Dim bTemp() As Byte
Dim lFlen As Long
Dim DataOffset As Long
Dim Width As Long
Dim Height As Long
Dim PerPixel As Long
Dim Compression As Long
Dim sFileName As String
Dim h As Long
Dim w As Long
Dim R As Integer
Dim G As Integer
Dim B As Integer
Dim t As Long
Dim qq As Long
Dim t1 As Long
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
Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Sub Form_Click()
Dim iFN As Integer
Dim bTemp() As Byte
Dim lFlen As Long
Dim DataOffset As Long
Dim Width As Long
Dim Height As Long
Dim PerPixel As Long
Dim Compression As Long
Dim sFileName As String
Dim h As Long
Dim w As Long
Dim R As Integer
Dim G As Integer
Dim B As Integer
Dim t As Long
Dim qq As Long
Dim t1 As Long
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个需要对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
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
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询