中易网

vb中如何打开一个bmp文件并读取里面的内容?

答案:3  悬赏:0  
解决时间 2021-01-11 11:19
  • 提问者网友:锁深秋
  • 2021-01-10 11:57
vb中如何打开一个bmp文件并读取里面的内容?
最佳答案
  • 二级知识专家网友:山河有幸埋战骨
  • 2021-01-10 12:21
给段2进制读文件的代码,然后写16进制文本文件:
Option Explicit
Dim strfFleName
Dim buffer() As Byte
Dim i As Long
Private Sub Command1_Click()
CommonDialog1.ShowOpen
strfFleName = CommonDialog1.FileName
Dim MySize
MySize = FileLen(strfFleName)
Text1 = MySize
ReDim buffer(1 To MySize)
Open strfFleName For Binary As #1
Get #1, , buffer
Close #1
Open "C:\123.txt" For Output Shared As #1
For i = 1 To MySize
' 若要以其他方式打开文件,必需先关闭此文件。
Print #1, Right("0" & Hex(buffer(i)), 2);
Next
Close #1
End Sub
全部回答
  • 1楼网友:佘樂
  • 2021-01-10 14:15
我暂时保留我的看法!
  • 2楼网友:笑迎怀羞
  • 2021-01-10 13:24
VB将bmp文件读入缓冲区
Private Type BITMAPINFOHEADER
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
Dim bbuffer() As Byte
Private Function CreateFileNumber(ByRef FileNumber As Long) As Boolean
On Error GoTo FileError_
FileNumber = FreeFile
CreateFileNumber = True
Exit Function
FileError_:
CreateFileNumber = False
Exit Function
End Function
Private Function LoadBmp(ByVal szfileName As String) As Boolean
If Dir$(szfileName, vbArchive Or vbNormal Or vbHidden Or vbSystem Or vbReadOnly) = "" Then
LoadBmp = False
Exit Function
End If
Dim hfilenum As Long
If Not CreateFileNumber(hfilenum) Then
LoadBmp = False
Exit Function
End If
Dim bfh(0 To 13) As Byte
Dim dibinfo As BITMAPINFOHEADER
Open szfileName For Binary Access Read As #hfilenum
Get #hfilenum, 1, bfh
If bfh(0) <> 66 Or bfh(1) <> 77 Then 'BM
Close #hfilenum
LoadBmp = False
Exit Function
End If
Get #hfilenum, , dibinfo
If (dibinfo.biBitCount < 24) Then
Close #hfilenum
LoadBmp = False
Exit Function
End If
On Error GoTo ReadBmp_Err
Dim dpixeladd As Long
Dim LineByteWidth As Long
dpixeladd = dibinfo.biBitCount \ 8
LineByteWidth = dibinfo.biWidth * (dpixeladd)
If (LineByteWidth Mod 4) <> 0 Then LineByteWidth = LineByteWidth + (4 - (LineByteWidth Mod 4))
ReDim bbuffer(0 To LineByteWidth * dibinfo.biHeight)
Get #hfilenum, , bbuffer
Close #hfilenum
LoadBmp = True
Exit Function
ReadBmp_Err:
Close #hfilenum
LoadBmp = False
End Function
将文件名 szfileName 指定的 BMP 文件读入了 Byte 数组 bbuffer
只能读取 24位或32位的BMP
要做处理的话 可以直接在 bbuffer 里处理
也可以 通过 API 调用 CreateDIBSection
生成 可直接读写的 HBITMAP 再做处理
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息