查了很多资料,都是先将数据库中的信息先写成一张图片文件,然后显示这个图片文件,有没有办法不写文件而直接显示?

解决方案 »

  1.   

    存储的时候就以bitmap数据形式存储,读取出来以后再调用API把bitmap数据写入指定的设备场景中就可以了,或者直接用DIB数据
      

  2.   

    Private Type BITMAP  bytes
            bmType As Long
            bmWidth As Long
            bmHeight As Long
            bmWidthBytes As Long
            bmPlanes As Integer
            bmBitsPixel As Integer
            bmBits As Long
    End Type
      

  3.   

    将StdPicture写入Byte数组及读出 
    '-------------------------------------------------------- 
    ' Procedure : SaveImage 
    ' Purpose   : Saves a StdPicture object in a byte array. 
    '-------------------------------------------------------- 

    Public Function SaveImage( _ 
       ByVal image As StdPicture) As Byte() 
    Dim abData() As Byte 
    Dim oPersist As IPersistStream 
    Dim oStream As IStream 
    Dim lSize As Long 
    Dim tStat As STATSTG      ' Get the image IPersistStream interface 
       Set oPersist = image 
        
        ' Create a stream on global memory 
       Set oStream = CreateStreamOnHGlobal(0, True) 
        
        ' Save the picture in the stream 
       oPersist.Save oStream, True 
           
        ' Get the stream info 
       oStream.Stat tStat, STATFLAG_NONAME 
           
        ' Get the stream size 
       lSize = tStat.cbSize * 10000 
        
        ' Initialize the array 
       ReDim abData(0 To lSize - 1) 
        
        ' Move the stream position to 
        ' the start of the stream 
       oStream.Seek 0, STREAM_SEEK_SET 
        
        ' Read all the stream in the array 
       oStream.Read abData(0), lSize 
        
        ' Return the array 
       SaveImage = abData 
        
        ' Release the stream object 
       Set oStream = Nothing  End Function  '-------------------------------------------------------- 
    ' Procedure : LoadImage 
    ' Purpose   : Creates a StdPicture object from a byte array. 
    '-------------------------------------------------------- 

    Public Function LoadImage( _ 
       ImageBytes() As Byte) As StdPicture 
    Dim oPersist As IPersistStream 
    Dim oStream As IStream 
    Dim lSize As Long 
       
        ' Calculate the array size 
       lSize = UBound(ImageBytes) - LBound(ImageBytes) + 1 
        
        ' Create a stream object 
        ' in global memory 
       Set oStream = CreateStreamOnHGlobal(0, True) 
        
        ' Write the header to the stream 
       oStream.Write &H746C&, 4& 
        
        ' Write the array size 
       oStream.Write lSize, 4& 
        
        ' Write the image data 
       oStream.Write ImageBytes(LBound(ImageBytes)), lSize 
        
        ' Move the stream position to 
        ' the start of the stream 
       oStream.Seek 0, STREAM_SEEK_SET 
           
        ' Create a new empty picture object 
       Set LoadImage = New StdPicture 
        
        ' Get the IPersistStream interface 
        ' of the picture object 
       Set oPersist = LoadImage 
        
        ' Load the picture from the stream 
       oPersist.Load oStream 
           
        ' Release the streamobject 
       Set oStream = Nothing 
        
    End Function 
      

  4.   

    谢谢楼上几位,不过如通过ADODB.Stream将图片数据写入到Byte数组中呢?具体如何在界面上显示我还是不明白,比如我是希望将它显示在图片框或图像框中应如何实现?
      

  5.   

    回楼上的话,代码写了,不过今天我在家,代码没在家.以下是代码大致流程,如果需要具体代码,星期一我才能贴上来.1.查询数据库,得到记录集rs
    2.从rs中取得除图片以外的其它相关数据
    3.以ADODB.Stream方式读取rs中的图片数据并利用ADODB.Stream的save方法将它存储为一个图片文件
    4.后继的其它处理,其中一个是利用loadpicture方法及picture属性将存储的图片文件显于界面上的控件之中.
      

  6.   

    参考
    http://topic.csdn.net/u/20070521/12/aebdd5f2-eb70-4207-925b-fae1ccfda3ae.html
      

  7.   

    想了一下 如果不是要加密存储  数组不一定是必要的读文件 读到数组
    可以用
    ADODB.Stream 的LoadFromFile  代替
    写入数据库
    也可以直接
    .Fields("photo") = iStm.Read  iStm是 一个ADODB.Stream对象
    读数据库里的图片数据(其实就是二进制数组)到picture框
    可以用 Fields("photo") 或者 Fields("photo").value 来代替(表示)数组
    如果用了 ADODB.Stream 可以用 iStm.Read 来代替数组
      

  8.   

      '**   引用   Microsoft   ActiveX   Data   Objects   2.5   Library   及以上版本
      '2.5版本以下不支持Stream对象
     Option Explicit
      Dim iConcstr     As String
      Dim iConc     As ADODB.Connection
          
        
      '保存文件到数据库中
      Sub s_SaveFile()
              Dim iStm     As ADODB.Stream
              Dim iRe     As ADODB.Recordset
              Dim iConcstr     As String
        
              '读取文件到内容
              Set iStm = New ADODB.Stream
              With iStm
                      .Type = adTypeBinary           '二进制模式
                      .Open
                      .LoadFromFile App.Path + "\test.jpg"
              End With
              
        
              '打开保存文件的表
              Set iRe = New ADODB.Recordset
              With iRe
                      .Open "select   *   from   img", iConc, 1, 3
                      .AddNew                   '新增一条记录
                      .Fields("photo") = iStm.Read
                      .Update
              End With
              
        
            '完成后关闭对象
              iRe.Close
              iStm.Close
      End Sub
        
        
      Sub s_ReadFile()
              Dim iStm     As ADODB.Stream
              Dim iRe     As ADODB.Recordset
              '打开表
      Set iRe = New ADODB.Recordset
      '得到最新添加的纪录
              iRe.Open "select   top   1   *   from   img   order   by   id   desc", iConc, adOpenKeyset, adLockReadOnly
              '保存到文件
              Set iStm = New ADODB.Stream
              With iStm
                      .Mode = adModeReadWrite
                      .Type = adTypeBinary
                      .Open
                      .Write iRe("photo")
      '这里注意了,如果当前目录下存在test1.jpg,会报一个文件写入失败的错误.
                      .SaveToFile App.Path & "\test1.jpg"
              End With
              
        
              Image1.Picture = LoadPicture(App.Path & "\test1.jpg")
      '关闭对象
              iRe.Close
              iStm.Close
      End Sub
          
        
      Private Sub Command1_Click()
      Call s_ReadFile
      End Sub
        
        
      Private Sub Command2_Click()
      Call s_SaveFile
      End Sub
        
        
      Private Sub Form_Load()
      '数据库连接字符串
      iConcstr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & App.Path & "\img.mdb"
        
      '下面的语句是连接sqlserver数据库的.
              'iConcstr   =   "Provider=SQLOLEDB.1;Persist   Security   Info=True;"   & _
      '  "User   ID=sa;Password=;Initial   Catalog=test;Data   Source=yang"
          
        
            Set iConc = New ADODB.Connection
            iConc.Open iConcstr
      Command1.Caption = "read"
      Command2.Caption = "save"
      
      End Sub
          
        
      Private Sub Form_Unload(Cancel As Integer)
      iConc.Close
      Set iConc = Nothing
      End Sub
      网上的一个代码 准备改这个代码
      

  9.   

      '引用 olelib.tlb
      '**   引用   Microsoft   ActiveX   Data   Objects   2.5   Library   及以上版本
      '2.5版本以下不支持Stream对象
     Option Explicit
      Dim iConcstr     As String
      Dim iConc     As ADODB.Connection
          
        
      '保存文件到数据库中
      Private Sub s_SaveFile()
              Dim iStm     As ADODB.Stream
              Dim iRe     As ADODB.Recordset
              Dim iConcstr     As String
        
              '读取文件到内容
              Set iStm = New ADODB.Stream
              With iStm
                      .Type = adTypeBinary           '二进制模式
                      .Open
                      .LoadFromFile App.Path + "\test.jpg"
              End With
              
        
              '打开保存文件的表
              Set iRe = New ADODB.Recordset
              With iRe
                      .Open "select   *   from   img", iConc, 1, 3
                      .AddNew                   '新增一条记录
                      .Fields("photo") = iStm.Read
                      .Update
              End With
              
        
            '完成后关闭对象
              iRe.Close
              iStm.Close
      End Sub
        
        
      Private Sub s_ReadFile()
              Dim iStm     As ADODB.Stream
              Dim iRe     As ADODB.Recordset
              '打开表
              Set iRe = New ADODB.Recordset
      '得到最新添加的纪录
              iRe.Open "select   top   1   *   from   img   order   by   id   desc", iConc, adOpenKeyset, adLockReadOnly
              Set Image1.Picture = LoadImage(iRe("photo").GetChunk(iRe("photo").ActualSize))
              iRe.Close
      End Sub
          
        
      Private Sub Command1_Click()
      Call s_ReadFile
      End Sub
        
        
      Private Sub Command2_Click()
      Call s_SaveFile
      End Sub
        
        
      Private Sub Form_Load()
      '数据库连接字符串
      iConcstr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & App.Path & "\img.mdb"
        
      '下面的语句是连接sqlserver数据库的.
              'iConcstr   =   "Provider=SQLOLEDB.1;Persist   Security   Info=True;"   & _
      '  "User   ID=sa;Password=;Initial   Catalog=test;Data   Source=yang"
          
        
            Set iConc = New ADODB.Connection
            iConc.Open iConcstr
      Command1.Caption = "read"
      Command2.Caption = "save"
      
      End Sub
          
        
      Private Sub Form_Unload(Cancel As Integer)
      iConc.Close
      Set iConc = Nothing
      End Sub
      '--------------------------------------------------------
    ' Procedure : SaveImage
    ' Purpose   : Saves a StdPicture object in a byte array.
    '--------------------------------------------------------
    '
    Private Function SaveImage( _
       ByVal image As StdPicture) As Byte()
    Dim abData() As Byte
    Dim oPersist As IPersistStream
    Dim oStream As IStream
    Dim lSize As Long
    Dim tStat As STATSTG     ' Get the image IPersistStream interface
       Set oPersist = image
         
         ' Create a stream on global memory
       Set oStream = CreateStreamOnHGlobal(0, True)
         
         ' Save the picture in the stream
       oPersist.Save oStream, True
            
         ' Get the stream info
       oStream.Stat tStat, STATFLAG_NONAME
            
         ' Get the stream size
       lSize = tStat.cbSize * 10000
         
         ' Initialize the array
       ReDim abData(0 To lSize - 1)
         
         ' Move the stream position to
         ' the start of the stream
       oStream.Seek 0, STREAM_SEEK_SET
         
         ' Read all the stream in the array
       oStream.Read abData(0), lSize
         
         ' Return the array
       SaveImage = abData
         
         ' Release the stream object
       Set oStream = NothingEnd Function'--------------------------------------------------------
    ' Procedure : LoadImage
    ' Purpose   : Creates a StdPicture object from a byte array.
    '--------------------------------------------------------
    '
    Private Function LoadImage( _
       ImageBytes() As Byte) As StdPicture
    Dim oPersist As IPersistStream
    Dim oStream As IStream
    Dim lSize As Long
        
         ' Calculate the array size
       lSize = UBound(ImageBytes) - LBound(ImageBytes) + 1
         
         ' Create a stream object
         ' in global memory
       Set oStream = CreateStreamOnHGlobal(0, True)
         
         ' Write the header to the stream
       oStream.Write &H746C&, 4&
         
         ' Write the array size
       oStream.Write lSize, 4&
         
         ' Write the image data
       oStream.Write ImageBytes(LBound(ImageBytes)), lSize
         
         ' Move the stream position to
         ' the start of the stream
       oStream.Seek 0, STREAM_SEEK_SET
            
         ' Create a new empty picture object
       Set LoadImage = New StdPicture
         
         ' Get the IPersistStream interface
         ' of the picture object
       Set oPersist = LoadImage
         
         ' Load the picture from the stream
       oPersist.Load oStream
            
         ' Release the streamobject
       Set oStream = Nothing
         
    End Function这样就不用保存到文件了 这个代码引用了 olelib.tlb
    感觉上 还可以继续缩短 
    ISTREAM 和adodb.STREAM 应该是差不多的东西