从数据库的image字段如何显示到一个Picture或者Image里面?用Stream的SaveToFile时,保存的文件比原来的文件大了几K
保存的文件可以用AcdSee浏览,但是其他的程序无法打开为什么?

解决方案 »

  1.   

    现成的控件不知道有没有直接读内存的?
    你可以自己读blob字段,然后自己绘制!现在可选则的是gdi+,比较方便。
      

  2.   

    一般的方法是,将图片文件用二进制方式open,写入一个变量,存到一个OLE字段。显示图片时,把字段内容读入变量,写成一个临时文件,然后用
    set picture1.picture = loadpicture("temp.jpg")
    之类的语句加载。
    这样做的优点:可以把图片文件包含在数据库文件中。
    缺点:1 数据库会迅速膨胀。2 频繁写、删除临时文件。我的意见:
    在数据库表中只增加一个文本字段,保存图片文件的文件名。
    当用户在界面上选定图片后确定保存时,
    1 保存文件名(要检查是否重名,作必要处理)
    2 把文件复制到特定文件夹下。
    显示时,从数据库取出文件名,直接
    set picture1.picture = loadpicture("filename.jpg")
    优点:
    1 数据库轻便
    2 不必读写临时文件数据,运行更快。
    缺点:
    图片文件不再数据库中,备份时需要另外操作。但是你可以写一段备份程序,Copy图片文件。
      

  3.   

    Private Sub cmdInsert_Click() ’插入数据库
        If pathRet <> "" Then
            Dim stm As New ADODB.Stream
            stm.Mode = adModeReadWrite
            stm.Type = adTypeBinary
            stm.Open
            stm.LoadFromFile (pathRet)
            rst.LockType = adLockPessimistic
            rst.Open "select top 0 * from ProductImage"
            rst.AddNew
            rst.Fields(0).value = 1
            rst.Fields(2).AppendChunk stm.Read
            rst.Update
            rst.Close
            stm.Close
            Set stm = Nothing
        End If
    End SubPrivate Sub cmdRead_Click()  '从数据库读取
        Dim stm As New ADODB.Stream
        Dim stm1 As New ADODB.Stream
        stm.Type = adTypeBinary
        stm.Mode = adModeReadWrite
        stm.Charset = 1
        stm.Open
        rst.Open "select NormalImage from ProductImage where ProductID=1"
        rst.Save stm
        rst.Close
        stm1.Mode = adModeReadWrite
        stm1.Type = adTypeBinary
        stm1.Open
        stm.Position = 331 '
        stm.CopyTo stm1
        stm.Close
        stm1.SaveToFile RUNTIME_IMAGE_CACHE_DIR & "1.jpg"
        stm1.Close
        Set stm = Nothing
        Set stm1 = Nothing
        imgOut.Picture = LoadPicture(RUNTIME_IMAGE_CACHE_DIR & "1.jpg")
    End Sub比较上传之前和保存之后的文件以后发现是保存的时候前面多了331字节,
    所以用stm.Position = 331 吧前面331直接去掉,但是这样处理以后还是不行
    处理以后的直接数对了,但是还是在AcdSee里面能显示,在其他里面不能显示
      

  4.   

    可以参考下面具体的实现代码:'Use ADODB.Stream Method
    'After ADO 2.6
    'Import the Image to SQLServer
    Private Sub ImportBLOB(cn As ADODB.Connection)
        
        Dim rs As New ADODB.Recordset
        Dim stm As ADODB.Stream
        
        Set stm = New ADODB.Stream
        
        ' Skip any table not found errors
        On Error Resume Next
        cn.Execute "drop table BinaryObject"
        
        On Error GoTo 0
        'Create the BinaryObject table
        cn.Execute "create table BinaryObject " & _
                     "(blob_id int IDENTITY(1,1), " & _
                      "blob_filename varchar(256), " & _
                      "blob_object image)"
                    
        rs.Open "Select * from BinaryObject where 1=2", cn, adOpenKeyset, adLockOptimistic
        'Read the binary files from disk
        stm.Type = adTypeBinary
        stm.Open
        stm.LoadFromFile App.Path & "\BLOBsample.jpg"
        
        rs.AddNew
        rs!blob_filename = App.Path & "\BLOBsample.jpg"
        rs!blob_object = stm.Read
        
        'Insert the binary object in the table
        rs.Update
        
        rs.Close
        stm.Close
        
        Set rs = Nothing
        Set stm = Nothing
        
    End Sub
    'Display the image on image control
    Private Sub DisplayBLOB(cn As ADODB.Connection)    Dim rs As New ADODB.Recordset
        
        ' Select the only image in the table
        rs.Open "Select * from BinaryObject where blob_id = 1", cn
        
        ' Set the DataSource to the recordset
        Set imgBinaryData.DataSource = rs
        'Set the DataField to the BLOB field
        imgBinaryData.DataField = rs!blob_object.Name
        
        'Release the recordset
        rs.Close
        Set rs = NothingEnd Sub
      

  5.   

    搞定了
    ==================
    Private Sub cmdRead_Click()
        Dim p() As Byte
        Dim lngToWrite As Long
        rst.Open "select NormalImage from ProductImage where ProductID=1"
        lngToWrite = rst(0).ActualSize
        p = rst(0).GetChunk(lngToWrite)
        rst.Close
        '开始写文件
        Dim lngFile As Long
        Dim lngErr As Long
        Dim lngWrited As Long
        Dim ofTemp As OFSTRUCT
        lngFile = OpenFile(RUNTIME_IMAGE_CACHE_DIR & "1.jpg", ofTemp, OF_CREATE)
        lngErr = WriteFile(lngFile, p(0), lngToWrite, lngWrited, 0)
        If lngErr = 0 Then
            lngErr = GetLastError()
            MsgBox lngErr
        End If
        If lngToWrite <> lngWrited Then
            MsgBox lngToWrite & " : " & lngWrited
        End If
        CloseHandle (lngFile)
        imgOut.Picture = LoadPicture(RUNTIME_IMAGE_CACHE_DIR & "1.jpg")
    End Sub