Saving picture from DB
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Saving picture from DB

  1. #1
    Join Date
    Jan 2001
    Location
    Ukraine
    Posts
    185

    Saving picture from DB

    Hi!
    How can save picture (Bitmap image) from database (Access)?

    Best regards,
    Yura.

  2. #2
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167

    Bits and Bytes :)

    Here is a sample code that you can work with.

    I've tested this with a table called tblImage that has 2 columns. First column is ID (type: autonumber) and the second one is IMGDATA (type: OLE Data).

    On Form1, I have 2 buttons, Button1 and Button2 to IMPORT from IMAGE file and EXPORT from DB respectively. I also have an ADO DATA CONTROL (Adodc1) that is pointing to the MDB and the .RECORDSOURCE set to tblImage.
    Code:
    ' saves from IMG file into DB
    Private Sub Button1_Click()
      ' get file name
      dim szFile as string
      szFile = InputBox("Enter image path")
        
      ' open file and read data
      dim imgByte() as Byte
      open szFile for Binary access read for #1
      redim imgByte(LOF(1))
    
      dim nPos as long
      nPos = 0
      while (not eof(1))
        get #1, nPos+1, imgByte(nPos)
        nPos = nPos + 1
      wend
    
      ' save into db
      dim adoRS as ADODB.RecordSet
      set adoRS = Adodc1.RecordSet
    
      With adoRS
        .AddNew
        .Fields(1).Value = imgByte
      end with
    
      ' close everything
      close #1
      redim imgByte(0)
      set adoRS = Nothing
    End Sub
    
    ' Saves from DB into file
    Private Sub Button2_Click()
      ' get file name
      dim szFile as string
      szFile = InputBox("Enter new image path")
    
      ' get current record
      dim adoRS as ADODB.RecordSet
      set adoRS = Adodc1.RecordSet
    
      ' get data
      dim imgByte() as Byte
      redim imgByte(adors.Fields(1).actualsize)
      imgByte = adoRs.Fields(1).value
    
      ' write data
      open szfile for binary access write as #1
      put #1, , imgByte
      close #1
    
      set adoRS = nothing
    End Sub
    Good Luck,
    -Cool Bizs

    [Cimperiali colorized for better reading]
    Last edited by Cimperiali; December 7th, 2002 at 04:39 AM.

  3. #3
    Join Date
    Jan 2001
    Location
    Ukraine
    Posts
    185
    Thanks a lot!

  4. #4
    Join Date
    Jan 2001
    Location
    Ukraine
    Posts
    185

    Re: Bits and Bytes :)

    I don`t understand where I can take Adodc1.

    I tryed to do all this using simply Database and recordset. Everythig was perfectly except results ) Files that appears on my disk are not files that I have put into OLE field. It include ole object as I understand.

    Code:
            Dim byProg() As Byte
            byProg = rsSystem.Fields(3).Value
            
                    
            Dim nFreeFiles As Integer
            nFreeFiles = FreeFile
            
            Dim sFileName As String
            sFileName = sFolder & "\" & sDir & "\" & sName
           
            Open sFileName For Binary Access Write As #nFreeFiles
            Put #nFreeFiles, , byProg
            Close #nFreeFiles
    How can I do it?

    Regards,
    Yura.

  5. #5
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167

    Maybe a sample will help you out ...

    Here is a sample for you.

    Good luck,
    -Cool Bizs
    Attached Files Attached Files

  6. #6
    Join Date
    Jan 2001
    Location
    Ukraine
    Posts
    185
    I`m sorry for my stupid question, but how have you added Binary Data into the OLE object field?

    Yura

  7. #7
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167
    Just assign the BYTE array to the recordset value. For example,
    Code:
    Sub SaveBinary(adoRS as RecordSet)
      ' byte array
      dim bytData(100) as Byte
      .... fill up the bytData with data ...
    
      ' assuming the field index 1 is OLE DATA type
      adoRS.Fields(1).Value = bytData
    End Sub
    Have fun,
    -Cool Bizs

  8. #8
    Join Date
    May 2003
    Location
    Northern Ireland
    Posts
    8
    I also have created an application that reads photos from a database and can add them too. Its a different solution, to the one mentioned above.

    I used common dialog 1 to retrieve the filename of the picture and embed the picture in the ole object. As follows

    Code:
    Private Sub olePhoto_Click()
    
        CommonDialog1.Filter = "Photo Files(*.bmp,*.jpg,*.gif)|*.bmp;*.jpg;*.gif"
        CommonDialog1.CancelError = True
        On Error GoTo errorhandler
        CommonDialog1.ShowOpen
        olePhoto.CreateEmbed CommonDialog1.FileName
        Exit Sub
        
    errorhandler:
        Exit Sub
        
    End Sub
    I had already set up an access 2000 database with an ole photo field to store the picture and linked this to my application using a data control called dtaControl. Then I set the datasource of olePhoto to dtaControl, and set the datafield to photo (the ole photo field set up with access). The only other thing to do is update the database. I did this with a command button as follows:

    Code:
    Private Sub cmdUpdate_Click()
        dtaControl.UpdateRecord
    End Sub
    Hope this helps someone

  9. #9
    Join Date
    May 2003
    Posts
    1

    how to save a picture from ole object to a file?

    hello.
    I've done all the things that coolbiz have shown in his message, and I used also the database sample that he has posted in your previos reply.
    first of all - thanks a lot, coolbiz.
    second, it works well, but only if I save the picture into an access data base with the code that coolbiz gave, or import an image within MS access.
    my problem is that I have allready many many images in an access 2000 table, in an ole object field, and all the images where saved with cut & paste.
    in the access table inside the ole object field - "picture" is written, while in coolbiz's database "long bynary data" is written.
    when I use his code to save an image file from the "long bynary data", to a file, it saves the picture ok,
    BUT when I use this code to save images that I inserted with cut & paste, where "picture" is written inside the ole object field, i recieve a file of unknown format. it does save the file with the correct size, but image applications cannot show the file.


    please, does anybody how can I export all my images from my access 2000 DataBase, to files??

    thaks a lot
    shmid

  10. #10
    Join Date
    Oct 2004
    Posts
    26

    Re: Saving picture from DB

    how about using the insert statment instead of the addnew? thanx.

  11. #11
    Join Date
    Mar 2005
    Posts
    3

    Talking Re: Saving picture from DB

    Hi!
    I'm a newbie of VB programming. What is the difference between the two methods above and, e.g., to save the path and filename in a text field of db?

    Thanks!

    Alexspin

  12. #12
    Join Date
    Jul 2005
    Posts
    3

    Re: Maybe a sample will help you out ...

    I have downloaded sample in C drive, it is not adding any new picture in the table when I click save from file .. I have given image file name also please help. it is working for savetofile method....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center