Results 1 to 13 of 13

Thread: [RESOLVED] How to load an image from a db WITHOUT databinding??

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Resolved [RESOLVED] How to load an image from a db WITHOUT databinding??

    Ok - just put lots of images into a database table.

    Using this method right now to get them into an image control.

    Code:
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    rs.Open "Exec GetStuPhoto_P '" & s3 & "'", gCn, adOpenForwardOnly, adLockReadOnly, adCmdText
    If rs.EOF Then
        rs.Close
        Set rs = Nothing
    End If
    Set .imgImage.DataSource = rs
    .imgImage.DataField = rs.Fields(0).Name
    rs.Close
    Set rs = Nothing
    This works fine - takes the place of the old code:

    Code:
    .imgImage.Picture = LoadPicture(s1)
    But it requires binding - which requires MSBIND.DLL be on the users machine and registered. I've got over a 1000 PC's that would need this install - not a very happy moment for the IT department!

    So .Net does it this way:

    Code:
    Drc.CommandText = "Select ActualImage From Student Where ActualImage is not null and StuId=" & strId
    Try
        Dim PictureData As Byte() = DirectCast(Drc.ExecuteScalar, Byte())
        Using stream As New IO.MemoryStream(PictureData)
            Dim im As Image = New Bitmap(stream)
            pbxStudent.Image = im
            pbxStudent.Visible = True
            pbxStudent.BringToFront()
        End Using
    Catch ex As Exception
        pbxStudent.Visible = False
    End Try
    Nice and sweet - picturedata comes into a byte array - memorystream with bitmap turns it back into an image - and it's loaded into the image control.

    How could I do this same thing in VB6???

    I do not want to take the binary data and create a temp jpg file for loadpicture - that is not an option.

    Thanks!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2

  3. #3
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: How to load an image from a db WITHOUT databinding??

    Maybe this thread's name should be "ByteArray to Picture" because that coonversion is the real problem, the rest can be done. This code will do everything, the problem is that conversion, i know you don't want to create a temp file but i'll do just that and maybe we can think how to replace just that part:
    (I assume you already have Connection and some Image stored somewhere..)
    Code:
        Set lRs = New ADODB.Recordset
        lRs.Open "SELECT TOP 1 ImageCol FROM TableImages", CN, adOpenKeyset, adLockOptimistic
        Me.Picture = GetPictureFromRecordset(lRs) 'Do something with it
        Set lRs = Nothing
    Code:
    Private Function GetPictureFromRecordset(pRs As ADODB.Recordset) As Picture
        Dim mStream     As ADODB.Stream
        Dim PicBits     As Variant
        
        Set mStream = New ADODB.Stream
        With mStream
            .Type = adTypeBinary
            .Open
            .Write pRs("ImageCol") 'Assuming ImageCol is the Image Column name
            .Position = 0          'Place at start position
            PicBits = .Read()      'Read all, PicBits will be our byte array
            
            'This code needs to be replaced (bytearray to picture conversion needed) 
            .SaveToFile App.Path & "\TempPic.jpg", adSaveCreateOverWrite
            Set GetPictureFromRecordset = LoadPicture(App.Path & "\TempPic.jpg")
    
        End With
        Set mStream = Nothing
    End Function

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How to load an image from a db WITHOUT databinding??

    As Steve pointed out LoadPicture is not an option - we already suggested it in a slightly different way but he rejected it. Check out his other thread fo more info.
    Quote Originally Posted by szlamany
    I do not want to take the binary data and create a temp jpg file for loadpicture - that is not an option.

  5. #5
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: How to load an image from a db WITHOUT databinding??

    Already know that, that's why i said "This code needs to be replaced (bytearray to picture conversion needed)". It will be easier to solve if we reduce it to find another way to make that conversion.

  6. #6

  7. #7
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: How to load an image from a db WITHOUT databinding??

    I couldn't find anything about this byte array to bitmap conversion, but I found this method to save/load from/to DB using a property bag.
    Save to Table:
    Code:
    Private Sub SaveImageToDB(pPic As Picture, rs As ADODB.Recordset, pColName As String)
        Dim pb As PropertyBag
        
        Set pb = New PropertyBag
        pb.WriteProperty "MyImage", pPic
        rs.Fields(pColName).AppendChunk pb.Contents
        rs.Update
        Set pb = Nothing
    End Sub
    Get From Recordset:
    Code:
    Private Function GetPictureFromRecordset(rs As ADODB.Recordset, _
                                             pColName As String) As Picture
        Dim pb As PropertyBag
    
        Set pb = New PropertyBag
        pb.Contents = rs.Fields(pColName).GetChunk(rs.Fields(pColName).ActualSize)
        Set GetPictureFromRecordset = pb.ReadProperty("MyImage")
        Set pb = Nothing
    End Function
    Last edited by jcis; Aug 14th, 2007 at 12:48 AM.

  8. #8

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to load an image from a db WITHOUT databinding??

    I'm guessing that means I would need to change how the image was originally saved - right?

    At this time the image were loaded by opening the .JPG file as BINARY and simply loading the binary data.

    Looks to me like this method takes a picture control with an image in it and saves that to a property bag. Is that correct?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How to load an image from a db WITHOUT databinding??

    No, I don't think you have to change anything - that sample simply doesn't work as is. It needs some work and I'm trying few things as we speak but no success yet.
    I saw that code before (here is the original by Mattias Sjögren - I know the guys since VB-Zone days).

    If I get anything working I will post it. Hopefully you get it first.

  10. #10

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to load an image from a db WITHOUT databinding??

    I've gone with RB's suggestion to just go with temp files for now - until I can work out the details of memory methods that DO NOT REQUIRE new DEPENDENCIES!

    I wish I was in .Net right now

    At any rate - I used JCIS's code - only real change was to .Close before being able to use LOADPICTURE - otherwise it was blowing up.

    Code:
                    Dim rs As ADODB.Recordset
                    Set rs = New ADODB.Recordset
                    rs.Open "Exec " & Mid(s1, 2) & " '" & s3 & "'", gCn, adOpenForwardOnly, adLockReadOnly, adCmdText
                    If rs.EOF Then
                        rs.Close
                        Set rs = Nothing
                    End If
                    Dim mStream As ADODB.Stream
                    Dim PicBits As Variant
                    
                    Set mStream = New ADODB.Stream
                    With mStream
                        .Type = adTypeBinary
                        .Open
                        .Write rs(0)
                        .Position = 0
                        PicBits = .Read()
                        .SaveToFile App.Path & "\" & CStr(glngConnId) & ".jpg", adSaveCreateOverWrite
                        .Close
                    End With
                    Set mStream = Nothing
                    rs.Close
                    Set rs = Nothing
                    .imgImage.Picture = LoadPicture(App.Path & "\" & CStr(glngConnId) & ".jpg")
                    Kill (App.Path & "\" & CStr(glngConnId) & ".jpg")
    PS - Si just posted in the other thread - I might go re-visit the memory idea right now...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  11. #11

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to load an image from a db WITHOUT databinding??

    Ok - this is now resolved - sorry for having two threads running on this issue.

    Resolution is in this thread:

    http://www.vbforums.com/showthread.php?t=482854

    All done without temp files and without adding install references to the project.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: How to load an image from a db WITHOUT databinding??

    Ok it's resolved, but i'll post some example code of the Property Bag approach. It receives a simple Picture as parameter, not a PictureBox.
    Code:
    'This assumes there is a Table named TableImages with a Column named ImageCol (Image)
    Private Sub Command1_Click()
        Dim CN As ADODB.Connection
        Dim lRs As ADODB.Recordset
    
        Set CN = New ADODB.Connection
        CN.ConnectionString = "Driver={SQL Server};Server=JCI1;Database=Master;Uid=;Pwd=;"
        CN.Open
    
        Set lRs = New ADODB.Recordset
        lRs.Open "SELECT ImageCol FROM TableImages", CN, adOpenKeyset, adLockOptimistic
    
        SaveImageToDB Picture1.Picture, lRs, "ImageCol"             'Saves the Picture in Picture1
                                                                    'Parameters: Pic, Recordset, Column Name in DB
    
        'Recreate lRs, it's not needed for this example to run but anyway..
        lRs.Close
        Set lRs = New ADODB.Recordset
        lRs.Open "SELECT ImageCol FROM TableImages", CN, adOpenKeyset, adLockOptimistic
    
        Picture2.Picture = GetPictureFromRecordset(lRs, "ImageCol") 'Loads the Picture from lRs to Picture2
                                                                    'Parameters: Recordset, Column Name in DB
    
        lRs.Close
        Set lRs = Nothing
    
        CN.Close
        Set CN = Nothing
    End Sub
    
    Private Sub SaveImageToDB(pPic As Picture, rs As ADODB.Recordset, pColName As String)
        Dim pb As PropertyBag
    
        Set pb = New PropertyBag
        pb.WriteProperty "MyImage", pPic
        rs.Fields(pColName).AppendChunk pb.Contents
        rs.Update
        Set pb = Nothing
    End Sub
    
    
    Private Function GetPictureFromRecordset(rs As ADODB.Recordset, pColName As String) As Picture
        Dim pb As PropertyBag
    
        Set pb = New PropertyBag
        pb.Contents = rs.Fields(pColName).GetChunk(rs.Fields(pColName).ActualSize)
        Set GetPictureFromRecordset = pb.ReadProperty("MyImage")
        Set pb = Nothing
    End Function
    Last edited by jcis; Aug 14th, 2007 at 06:18 PM.

  13. #13

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to load an image from a db WITHOUT databinding??

    Thanks for following up on that approach - always good to present several options.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

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



Click Here to Expand Forum to Full Width