二进制有以下几个常用的操作:
1·数组操作:无论是什么文件,对于二进制文件来说都可以快速地读入到一个Byte数组里。这个操作对于拼接或复制文件是很有用的。方法如下:Dim ReadBytes() As Byte
Dim FileSize As Long
Dim FN As Integer
FN=FreeFile '自动得到一个文件号。
Open FileName For Binary As #FN '打开文件,并以FN作为文件号。
  FileSize=LOF(FN)-1 '以LOF获得文件字节。
  ReDim ReadBytes(FileSize) '定义一个与文件一样大的数组。
  Get #FN,1,ReadBytes '读与该数组相同容量的数据到数组里。
Close #FN2·自定义类型数据操作。
  自己定义一个类型,然后以这个类型作为记录来对二进制文件操作。比如下面的程序完成一个同学录:
Type tpTCP
  UserName As String * 16 
  Tel As String 40
  OICQ As String 20
End TypeFunction GetAddressByRecNumber(pRec As Long,pTpLen as Long) As Long
  Dim tOutAdd As Long
  tOutAdd=pRec*pTpLen+1
  GetAddressByRecNumber=tOutAdd
End Function保存一个记录变量tRecData到文件的第12个记录。Dim tRecData As tpTCPtRecData.UserName="小仙妹"
tRecData.Tel="1234567"
tRecData.OICQ="7654321"Open FileName For Binary As #1
  tTpLen=LenB(tRecData) '获得记录的字节长度。
  tAddress=GetAddressByRecNumber(12,tTpLen) '计算地址。
  Put #1,tAddress,tRecData
Close #1但需要注意的是:你在VB里以文件方式读BMP并不能显示(实际上也可以显示,但是速度很慢,不实用)。如果仅仅是做BMP文件的合并那还是可以的。如果你想读取BMP供显示用,那么请寻找API函数。

解决方案 »

  1.   

    我曾经写过一个读取BMP的模块:
    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 LongPublic Type ZMapInfoType
        Headle As Integer
        MapType As String * 3
        MapCount As Integer
    End TypePrivate Type BitMapAllHeader
        bfType As String * 2
        bfSize As Long
        bfReserved1 As Integer
        bfReserved2 As Integer
        bfOffBits As Long
        '---------------------
        biSize As Long
        biWidth As Long
        biHeight As Long
        biPlanes As Integer
        biBitCount As Integer
        biCompression As Long
        biSizeImage As Long
        biXRes As Long
        biYRes As Long
        biClrUsed As Long
        biClrImportant As Long
    End TypePrivate Type BitMapPalette
        peBlue As Byte
        peGreen As Byte
        peRed As Byte
        peFlags As Byte
    End TypePublic Sub ChkMapType(MapInfo As ZMapInfoType)
        Dim LoadByt() As Byte
        
        ReDim LoadByt(0 To 1) As Byte
        Get MapInfo.Headle, 1, LoadByt
        If UCase$(StrConv(LoadByt, vbUnicode)) = "BM" Then
            MapInfo.MapType = "BMP"
        Else
            MapInfo.MapType = ""
        End If
        
    End SubPublic Function GetMapCount(MapInfo As ZMapInfoType) As Integer
        Select Case UCase$(RTrim$(MapInfo.MapType))
        Case "BMP"
            MapInfo.MapCount = 1
        Case Else
            MapInfo.MapCount = 0
        End Select
        
        GetMapCount = MapInfo.MapCount
        
    End FunctionPublic Function GetMapSize(MapInfo As ZMapInfoType, _
            Width As Long, _
            Height As Long, _
            Optional MapIndex As Integer) As Integer
        On Error GoTo LoadErr
            
        Select Case UCase$(RTrim$(MapInfo.MapType))
        Case "BMP"
            'MapInfo.MapCount = 1
            Dim BMah As BitMapAllHeader
            
            Get MapInfo.Headle, 1, BMah
            
            Width = BMah.biWidth
            Height = BMah.biHeight
            
        Case Else
            On Error GoTo 0
            GetMapSize = 0: Exit Function
        End Select
        
        On Error GoTo 0
        GetMapSize = -1
        Exit Function
        
    LoadErr:
        GetMapSize = 0
        
    End FunctionPublic Function LoadBmpMap(hDC As Long, _
            PutX As Long, PutY As Long, _
            MapInfo As ZMapInfoType, _
            FileX As Long, FileY As Long, _
            ByVal FileWidth As Long, ByVal FileHeight As Long) As Integer
        Dim Width As Long, Height As Long
        Dim I As Long, J As Long, K As Long
        Dim BMah As BitMapAllHeader
        Dim LoadPalette As BitMapPalette
        Dim LoadByt1 As Byte, LoadByt() As Byte
        Dim BMPPalette(0 To 255) As Long
        Dim PutColor As Long
        Dim LineByt As Long
        
        If FileX + FileWidth <= 0 Or FileY + FileHeight <= 0 Then GoTo LoadErr
        
        On Error GoTo LoadErr
            
        Get MapInfo.Headle, 1, BMah
        If BMah.biPlanes <> 1 Or BMah.biCompression <> 0 Then LoadBmpMap = 1: Exit Function
        
        Width = BMah.biWidth
        Height = BMah.biHeight
        If FileX + FileWidth > Width Then FileWidth = Width - FileX
        If FileY + FileHeight > Height Then FileHeight = Height - FileY
        'Debug.Print FileX, FileWidth, PutX, Width
        'Debug.Print FileY, FileHeight, PutY, Height
                
        Select Case BMah.biBitCount
        Case 24
            'Get MapInfo.Headle, BMah.bfOffBits, LoadByt1
            LineByt = Int((Width * 3 + 3) / 4) * 4
            ReDim LoadByt(0 To FileWidth * 3 - 1) As Byte
            
            For I = 0 To FileHeight - 1
                'Get MapInfo.Headle, BMah.bfOffBits + LineByt * (Height - (I + FileY) - 1) + FileX * 3, LoadByt1
                Get MapInfo.Headle, BMah.bfOffBits + LineByt * (Height - (I + FileY) - 1) + FileX * 3 + 1, LoadByt
                For J = 0 To FileWidth - 1
                    PutColor = 0
                    For K = 0 To 2
                        'Get MapInfo.Headle, , LoadByt1
                        LoadByt1 = LoadByt(J * 3 + K)
                        PutColor = PutColor Or LoadByt1 * &H100& ^ (2 - K)
                    Next K
                    SetPixelV hDC, J + PutX, I + PutY, PutColor
                Next J
            Next I
            
        Case 8
            For I = 0 To 255
                Get MapInfo.Headle, , LoadPalette
                BMPPalette(I) = RGB(LoadPalette.peRed, LoadPalette.peGreen, LoadPalette.peBlue)
            Next I
            LineByt = Int((Width + 3) / 4) * 4
            ReDim LoadByt(0 To FileWidth - 1) As Byte
            
            For I = 0 To FileHeight - 1
                Get MapInfo.Headle, BMah.bfOffBits + LineByt * (Height - (I + FileY) - 1) + FileX + 1, LoadByt
                For J = 0 To FileWidth - 1
                    SetPixelV hDC, J + PutX, I + PutY, BMPPalette(LoadByt(J))
                Next J
            Next I
            
        Case 4
            For I = 0 To 16
                Get MapInfo.Headle, , LoadPalette
                BMPPalette(I) = RGB(LoadPalette.peRed, LoadPalette.peGreen, LoadPalette.peBlue)
            Next I
            LineByt = Int((Int((Width + 1) / 2) + 3) / 4) * 4
            ReDim LoadByt(0 To Int((Width + 1) / 2) - 1) As Byte
            
            For I = 0 To FileHeight - 1
                Get MapInfo.Headle, BMah.bfOffBits + LineByt * (Height - (I + FileY) - 1) + 1, LoadByt
                For J = 0 To FileWidth - 1 Step 2
                    SetPixelV hDC, J + PutX, I + PutY, BMPPalette((LoadByt(Int(J / 2)) And &HF0) \ &H10)
                    SetPixelV hDC, J + PutX + 1, I + PutY, BMPPalette(LoadByt(Int(J / 2)) And &HF)
                Next J
            Next I
            
        Case 1
            Dim BitAnd(0 To 15) As Integer
            For I = 0 To 7
                BitAnd(I) = 2 ^ (7 - I)
            Next I
            BitAnd(8) = &H8000
            For I = 0 To 6
                BitAnd(9 + I) = 2 ^ (14 - I)
            Next I
            For I = 0 To 1
                Get MapInfo.Headle, , LoadPalette
                BMPPalette(I) = RGB(LoadPalette.peRed, LoadPalette.peGreen, LoadPalette.peBlue)
            Next I
            
            LineByt = Int((Int((Width + 7) / 8) + 3) / 4) * 4
            ReDim LoadByt(0 To Int((Width + 7) / 8) - 1) As Byte
            
            For I = 0 To FileHeight - 1
                Get MapInfo.Headle, BMah.bfOffBits + LineByt * (Height - (I + FileY) - 1) + 1, LoadByt
                For J = 0 To FileWidth - 1 Step 8
                    For K = 0 To 7
                        SetPixelV hDC, J + PutX + K, I + PutY, BMPPalette((((LoadByt(Int(J / 8)) And BitAnd(K)) <> 0) And 1))
                    Next K
                Next J
            Next I
            
        Case Else
            LoadBmpMap = 2
            Exit Function
            
        End Select
        
        On Error GoTo 0
        LoadBmpMap = 0
        Exit Function
        
    LoadErr:
        LoadBmpMap = -1
        
    End Function
      

  2.   

    使用方法:
    Private Sub LoadBMPSub()
        Dim ZMap As ZMapInfoType
        Dim Wi As Long, He As Long
        
        If ChkFileRead(Text1.Text) = False Then Exit Sub
        ZMap.Headle = FreeFile
        Open Text1.Text For Binary As ZMap.Headle
        
        Me.MousePointer = 11
        Me.Caption = MeName + " (Loading)"
        ChkMapType ZMap
        If GetMapCount(ZMap) = 0 Then Exit Sub
        If GetMapSize(ZMap, Wi, He) = 0 Then Exit Sub
        SetSize Pic2, Wi, He
        DoEvents
        LoadBmpMap Pic2.hDC, 0, 0, ZMap, 0, 0, Wi, He
        Me.MousePointer = 0
        Me.Caption = MeName
        
        Close ZMap.Headle
        
        Pic2.Picture = Pic2.Image
        JSS
        
    End Sub
      

  3.   

    写入:
    filehandle=freefile
    if filehandle=0 then filehandle=1
    op filename for binary as filehandle
    读出:
    Put FileHandle, , filename