GOPHERSPACE.DE - P H O X Y
gophering on sdf.org
   VBMySQL.com     The Web's Leading Resource For Visual Basic and MySQL
                                        Development
               Corner                                                         
               image  HOME | ARTICLES | SAMPLECODE | FORUMS | PRESENTATIONS | 
               for                             BLOG
               bottom 
               border 

    Home Home   Members Members   Search Search   F.A.Q. F.A.Q.    Register   
    Register    Login Login                                                   
    Article Related Forums >> Visual Basic/MySQL Articles >> Displaying       
    image data immediately w/out saving to Disk                               
                                                                              
    Show: Today's Messages  :: Show                                           
    Polls :: Message Navigator               Switch to threaded view of this  
    Email to friend                    topic Create a new topic Submit Reply  
                                                                              
    Displaying image data immediately w/out     Wed, 28 May 2003 05:06 Go to  
    saving to Disk                                              next message  
    sherwin                                                                   
    Messages: 34          Occasional Poster [IMG] [IMG] [IMG]                 
    Registered: May 2003                                                      
    Location: Philippines                                                     
    Hi Mike,                                                                  
                                                                              
    I appreciate your code in "Accessing MySQL Blob colums using VB6". Is it  
    possible that after getting data from the database, we immediately load   
    or display the image without saving to the local disk?                    
                                               Report message to a moderator  
    [IMG]  Send a private message to this user                   [IMG] [IMG]  
    Blob to Image without file         Wed, 28 May 2003 07:48 Go to previous  
                                                   messageGo to next message  
    Mike                                                                      
    Messages: 946                      Power User                             
    Registered: May 2003               Site Admin                             
    Location: Calgary, Alberta, Canada                                        
    It is in fact. The following code is not my own, but I cannot track the   
    original author in order to give proper credit.                           
                                                                              
    PLACE THIS SECTION IN A MODULE                                            
                                                                              
    Private Declare Function CreateStreamOnHGlobal Lib "ole32" ( _            
    ByVal hGlobal As Long, _                                                  
    ByVal fDeleteOnRelease As Long, _                                         
    ppstm As IStream) As Long                                                 
                                                                              
    Private Declare Function GetHGlobalFromStream Lib "ole32" ( _             
    ByVal pstm As IStream, _                                                  
    phglobal As Long) As Long                                                 
                                                                              
    Private Declare Sub MoveMemory Lib "kernel32" _                           
    Alias "RtlMoveMemory" ( _                                                 
    Dest As Any, _                                                            
    src As Any, _                                                             
    ByVal cb As Long)                                                         
                                                                              
    ' Global Memory Flags                                                     
    Const GMEM_MOVEABLE = &H2                                                 
    Const GMEM_ZEROINIT = &H40                                                
                                                                              
    Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)                             
                                                                              
    Private Declare Function GlobalAlloc Lib "kernel32" ( _                   
    ByVal wFlags As Long, _                                                   
    ByVal dwBytes As Long) As Long                                            
                                                                              
    Private Declare Function GlobalSize Lib "kernel32" ( _                    
    ByVal hMem As Long) As Long                                               
                                                                              
    Private Declare Function GlobalLock Lib "kernel32" ( _                    
    ByVal hMem As Long) As Long                                               
                                                                              
    Private Declare Function GlobalUnlock Lib "kernel32" ( _                  
    ByVal hMem As Long) As Long                                               
                                                                              
    Const PictureID = &H746C&                                                 
                                                                              
    Private Type PictureHeader                                                
    Magic As Long                                                             
    Size As Long                                                              
    End Type                                                                  
                                                                              
    Const S_OK = 0                                                            
                                                                              
    '                                                                         
    ' Picture2Array                                                           
    '                                                                         
    ' Converts a Picture object to a byte array                               
    '                                                                         
    Public Sub Picture2Array(ByVal oObj As StdPicture, aBytes() As Byte)      
    Dim oIPS As IPersistStream                                                
    Dim oStream As IStream, hGlobal As Long, lPtr As Long                     
    Dim lSize As Long, Hdr As PictureHeader                                   
    Dim lRes As Long                                                          
                                                                              
    ' Get the IPersistStream interface                                        
    Set oIPS = oObj                                                           
                                                                              
    ' Create a IStream object                                                 
    ' on global memory                                                        
    lRes = CreateStreamOnHGlobal(0, True, oStream)                            
                                                                              
    If lRes = S_OK Then                                                       
                                                                              
    ' Save the picture in the stream                                          
    oIPS.Save oStream, True                                                   
                                                                              
    ' Get the global memory handle                                            
    ' from the stream                                                         
    If GetHGlobalFromStream(oStream, hGlobal) = S_OK Then                     
                                                                              
    ' Get the memory size                                                     
    lSize = GlobalSize(hGlobal)                                               
                                                                              
    ' Get a pointer to the memory                                             
    lPtr = GlobalLock(hGlobal)                                                
                                                                              
    If lPtr Then                                                              
                                                                              
    lSize = lSize - Len(Hdr)                                                  
                                                                              
    ' Redim the array                                                         
    ReDim aBytes(0 To lSize - 1)                                              
                                                                              
    ' Copy the data to the array                                              
    MoveMemory aBytes(0), ByVal lPtr + Len(Hdr), lSize                        
                                                                              
    End If                                                                    
                                                                              
    ' Release the pointer                                                     
    GlobalUnlock hGlobal                                                      
                                                                              
    End If                                                                    
                                                                              
    ' Release the IStream                                                     
    ' object                                                                  
    Set oStream = Nothing                                                     
                                                                              
    End If                                                                    
                                                                              
    End Sub                                                                   
                                                                              
    '                                                                         
    ' Array2Picture                                                           
    '                                                                         
    ' Converts a byte array (which contains a valid picture) to a             
    ' Picture object.                                                         
    '                                                                         
    Public Function Array2Picture(aBytes() As Byte) As StdPicture             
    Dim oIPS As IPersistStream                                                
    Dim oStream As IStream, hGlobal As Long, lPtr As Long                     
    Dim lSize As Long, Hdr As PictureHeader                                   
    Dim lRes As Long                                                          
                                                                              
    ' Create a new empty                                                      
    ' picture object                                                          
    Set Array2Picture = New StdPicture                                        
                                                                              
    ' Get the IPersistStream interface                                        
    Set oIPS = Array2Picture                                                  
                                                                              
    ' Calculate the array size                                                
    lSize = UBound(aBytes) - LBound(aBytes) + 1                               
                                                                              
    ' Allocate global memory                                                  
    hGlobal = GlobalAlloc(GHND, lSize + Len(Hdr))                             
                                                                              
    If hGlobal Then                                                           
                                                                              
    ' Get a pointer to the memory                                             
    lPtr = GlobalLock(hGlobal)                                                
                                                                              
    ' Initialize the header                                                   
    Hdr.Magic = PictureID                                                     
    Hdr.Size = lSize                                                          
                                                                              
    ' Write the header                                                        
    MoveMemory ByVal lPtr, Hdr, Len(Hdr)                                      
                                                                              
    ' Copy the byte array to                                                  
    ' the global memory                                                       
    MoveMemory ByVal lPtr + Len(Hdr), aBytes(0), lSize                        
                                                                              
    ' Release the pointer                                                     
    GlobalUnlock hGlobal                                                      
                                                                              
    ' Create a IStream object                                                 
    ' with the global memory                                                  
    lRes = CreateStreamOnHGlobal(hGlobal, True, oStream)                      
                                                                              
    If lRes = S_OK Then                                                       
                                                                              
    ' Load the picture                                                        
    ' from the stream                                                         
    'Set Form1.Picture1.Picture = oIPS.Load(oStream)                          
    oIPS.Load oStream                                                         
    Set frmMain.Picture1.Picture = oIPS                                       
    End If                                                                    
    ' Release the IStream                                                     
    ' object                                                                  
    Set oStream = Nothing                                                     
                                                                              
    End If                                                                    
                                                                              
    End Function[/code:1]                                                     
                                                                              
    THEN ASSUMING YOU HAVE A PICTUREBOX CALLED PICTURE1 AND YOUR STREAM       
    OBJECT IS CALLED MYSTREAM, DO EVERY STEP BUT THE WRITE TO FILE AND ADD:   
    mystream.Position = 0                                                     
    Picture1.Picture = Array2Picture(mystream.Read)[/code:1]                  
                                                                              
    and that will do it. The sample code is actually available in the zip     
    archive of my presentation in San Jose:                                   
    http://www.vbmysql.com/presentations/uc2003/presentation.exe              
                                                                              
                                                                              
    Regards,                                                                  
    Mike Hillyer                                                              
    Webmaster/Moderator                                                       
    http://www.vbmysql.com                                                    
    Read my blog at: http://www.vbmysql.com/mike/blog                         
                                               Report message to a moderator  
    [IMG]  Send a private message to this user                   [IMG] [IMG]  
    Displaying image data immediately           Wed, 28 May 2003 19:15 Go to  
    w/out saving to Disk                  previous messageGo to next message  
    sherwin                                                                   
    Messages: 34          Occasional Poster [IMG] [IMG] [IMG]                 
    Registered: May 2003                                                      
    Location: Philippines                                                     
    hi mike,                                                                  
                                                                              
    I tried the code. I placed it in a section module. but when I ran it an   
    error was generated "Compile Error: User-define type not defined." How    
    should I go about this? I think I need to defined something inside the    
    module, right?                                                            
                                               Report message to a moderator  
    [IMG]  Send a private message to this user                   [IMG] [IMG]  
    Where was the error?  Wed, 28 May 2003 19:31 Go to previous messageGo to  
                                                                next message  
    Mike                                                                      
    Messages: 946                      Power User                             
    Registered: May 2003               Site Admin                             
    Location: Calgary, Alberta, Canada                                        
    What line did the error occur on?                                         
                                                                              
                                                                              
    Regards,                                                                  
    Mike Hillyer                                                              
    Webmaster/Moderator                                                       
    http://www.vbmysql.com                                                    
    Read my blog at: http://www.vbmysql.com/mike/blog                         
                                               Report message to a moderator  
    [IMG]  Send a private message to this user                   [IMG] [IMG]  
    Displaying image data immediately           Wed, 28 May 2003 21:22 Go to  
    w/out saving to Disk                  previous messageGo to next message  
    sherwin                                                                   
    Messages: 34          Occasional Poster [IMG] [IMG] [IMG]                 
    Registered: May 2003                                                      
    Location: Philippines                                                     
    'OPEN RECORDSET TO READ BLOB                                              
    rs.Open "Select * from files WHERE files.file_id = 1", conn               
    mystream.Open                                                             
    mystream.Write rs!File 'Error here "operation is not allowed in this      
    context"                                                                  
    mystream.Position = 0                                                     
    Picture1.Picture = Array2Picture(mystream.Read)                           
    mystream.Close                                                            
    rs.Close                                                                  
                                               Report message to a moderator  
    [IMG]  Send a private message to this user                   [IMG] [IMG]  
    Displaying image data immediately           Wed, 28 May 2003 22:05 Go to  
    w/out saving to Disk                  previous messageGo to next message  
    sherwin                                                                   
    Messages: 34          Occasional Poster [IMG] [IMG] [IMG]                 
    Registered: May 2003                                                      
    Location: Philippines                                                     
    I already got it. many thanks to u mike. What I did is i included the     
    dll that i downloaded from the link you've given. I forgot also to have   
    the type of the stream and I also change the object name of the form.     
                                                                              
    More power                                                                
                                               Report message to a moderator  
    [IMG]  Send a private message to this user                   [IMG] [IMG]  
    Good             Thu, 29 May 2003 06:04 Go to previous messageGo to next  
                                                                     message  
    Mike                                                                      
    Messages: 946                      Power User                             
    Registered: May 2003               Site Admin                             
    Location: Calgary, Alberta, Canada                                        
    I forgot about that DLL...                                                
                                                                              
    I think I shall make this subject a future article.                       
                                                                              
                                                                              
    Regards,                                                                  
    Mike Hillyer                                                              
    Webmaster/Moderator                                                       
    http://www.vbmysql.com                                                    
    Read my blog at: http://www.vbmysql.com/mike/blog                         
                                               Report message to a moderator  
    [IMG]  Send a private message to this user                   [IMG] [IMG]  
    i can't retrieve blob field    Wed, 01 October 2003 04:17 Go to previous  
                                                   messageGo to next message  
    oren                                                                      
    Messages: 10             Freshman User [IMG]                              
    Registered: October 2003                                                  
    thanks to mike for the code..                                             
    i can store the image in mysql but then..                                 
    when i wanna query for search...                                          
    i can't display the image stored in databse(blob)                         
    help me please..                                                          
    thanks                                                                    
                                               Report message to a moderator  
    [IMG]  Send a private message to this user                   [IMG] [IMG]  
    Displaying image data immediately       Wed, 01 October 2003 07:18 Go to  
    w/out saving to Disk                  previous messageGo to next message  
    Mike                                                                      
    Messages: 946                      Power User                             
    Registered: May 2003               Site Admin                             
    Location: Calgary, Alberta, Canada                                        
    What does your code look like? Are you sure your BLOB got into the        
    database properly?                                                        
                                                                              
                                                                              
    Regards,                                                                  
    Mike Hillyer                                                              
    Webmaster/Moderator                                                       
    http://www.vbmysql.com                                                    
    Read my blog at: http://www.vbmysql.com/mike/blog                         
                                               Report message to a moderator  
    [IMG]  Send a private message to this user                   [IMG] [IMG]  
    blob              Wed, 01 October 2003 20:48 Go to previous messageGo to  
                                                                next message  
    oren                                                                      
    Messages: 10             Freshman User [IMG]                              
    Registered: October 2003                                                  
    ya i'm pretty sure coz, when i select the image and insert some           
    particular in database.. it's work properly.. but when i wanna query for  
    search, for example i wanna search using ic number... i can't display     
    the images for that ic number.. but i can pulled out the ic number and    
    other particulars.                                                        
                                                                              
    Dim conn As ADODB.Connection                                              
    Set conn = New ADODB.Connection                                           
                                                                              
    Dim rs As ADODB.Recordset                                                 
    Set rs = New ADODB.Recordset                                              
                                                                              
    Dim mystream As ADODB.Stream                                              
    Set mystream = New ADODB.Stream                                           
                                                                              
    conn.CursorLocation = adUseClient                                         
                                                                              
    'conn.ConnectionString ("uid=user;pwd=user;server=192.168.0.169;" & _     
    "driver={MySQL ODBC 3.51 Driver};database=image;dsn='';")                 
                                                                              
    conn.ConnectionString = "driver={MySQL ODBC 3.51                          
    Driver};database=image;dsn='';" _                                         
    & "SERVER=192.168.0.169;" _                                               
    & "UID=user;" _                                                           
    & "PWD=user;"                                                             
                                                                              
    conn.Open                                                                 
                                                                              
    Dim cari As String                                                        
    cari = txtsearch.Text                                                     
                                                                              
    rs.Open "Select * from imej WHERE imej.ic = cari ", conn                  
    mystream.Open                                                             
    'mystream.Write rs!file                                                   
                                                                              
    mystream.Position = 0                                                     
    Picture1.Picture = Array2Picture(mystream.Read)                           
    lblic = rs("ic")                                                          
    lblnama = rs("nama")                                                      
                                                                              
    mystream.Close                                                            
    rs.Close                                                                  
                                                                              
    the error = run time error '3219                                          
    operation is not allowed in this context                                  
    when i debug t he error at this line                                      
    Picture1.Picture = Array2Picture(mystream.Read)                           
                                                                              
    ------------------------                                                  
    i also try another sample but then...                                     
                                                                              
    error = run time error 545                                                
    Unable to bind to field or DataMember: 'IMAGES'                           
    (Set Image1.DataSource = adoPrimaryRS)                                    
                                                                              
    i can display the Text1                                                   
    but for Image1 can't                                                      
                                                                              
    Dim adoPrimaryRS As ADODB.Recordset                                       
    Set adoPrimaryRS = New ADODB.Recordset                                    
    Dim cnnConnection As ADODB.Connection                                     
    Set cnnConnection = New ADODB.Connection                                  
                                                                              
    cnnConnection.Open ("uid=user;pwd=user;server=192.168.0.169;" & _         
    "driver={MySQL ODBC 3.51 Driver};database=image;dsn='';")                 
                                                                              
    adoPrimaryRS.Open "Select * from tbl_images", cnnConnection, _            
    adOpenKeyset, adLockOptimistic                                            
                                                                              
    'Bind the ole controls to the data provider                               
    Set Text1.DataSource = adoPrimaryRS                                       
    Set Image1.DataSource = adoPrimaryRS                                      
                                                                              
    ---------------------------------------------------                       
                                                                              
    the reason why i want the code coz now i'm trying to develop system that  
    can access from various places. so we need to store the images in         
    database and can pull out whenever we want anywhere.                      
                                                                              
    thanks in advance                                                         
                                                                              
    ---------------                                                           
    mid                                                                       
    (the young beginner)                                                      
                                               Report message to a moderator  
    [IMG]  Send a private message to this user                   [IMG] [IMG]  
    Displaying image data immediately       Wed, 01 October 2003 21:17 Go to  
    w/out saving to Disk                  previous messageGo to next message  
    Mike                                                                      
    Messages: 946                      Power User                             
    Registered: May 2003               Site Admin                             
    Location: Calgary, Alberta, Canada                                        
    First of all, what did you use to put the image in?                       
                                                                              
    Second, I doubt that binding an image control to the recordset will       
    work.                                                                     
                                                                              
    If you save to file can you then read the file and see the picture?       
                                                                              
                                                                              
    Regards,                                                                  
    Mike Hillyer                                                              
    Webmaster/Moderator                                                       
    http://www.vbmysql.com                                                    
    Read my blog at: http://www.vbmysql.com/mike/blog                         
                                               Report message to a moderator  
    [IMG]  Send a private message to this user                   [IMG] [IMG]  
    blob              Wed, 01 October 2003 21:30 Go to previous messageGo to  
                                                                next message  
    oren                                                                      
    Messages: 10             Freshman User [IMG]                              
    Registered: October 2003                                                  
    i use the code that you give earlier(the exe file). once we select the    
    image file.. and insert particulars in database.. it's worked.            
    but i can't read the image back for the second time                       
                                                                              
    below is my database                                                      
                                                                              
    ID file_name file_size file nama ic                                       
                                                                              
    Edit Delete 10 tuty.JPG 10531 [BLOB - 10.3 KB] DD 123                     
                                                                              
    and actually i don't save the image to teh temporary file.. i just save   
    the image directly to the databse                                         
    i skipped this line                                                       
    mystream.SaveToFile Environ("temp") & "\copy.jpg", adSaveCreateOverWrite  
                                                                              
    thanks for the reply mike                                                 
                                               Report message to a moderator  
    [IMG]  Send a private message to this user                   [IMG] [IMG]  
    Displaying image data immediately       Wed, 01 October 2003 21:32 Go to  
    w/out saving to Disk                  previous messageGo to next message  
    Mike                                                                      
    Messages: 946                      Power User                             
    Registered: May 2003               Site Admin                             
    Location: Calgary, Alberta, Canada                                        
    Yes, but if you DO save to the temp file can you view the image?          
                                                                              
                                                                              
    Regards,                                                                  
    Mike Hillyer                                                              
    Webmaster/Moderator                                                       
    http://www.vbmysql.com                                                    
    Read my blog at: http://www.vbmysql.com/mike/blog                         
                                               Report message to a moderator  
    [IMG]  Send a private message to this user                   [IMG] [IMG]  
    blob         Wed, 01 October 2003 21:51 Go to previous messageGo to next  
                                                                     message  
    oren                                                                      
    Messages: 10             Freshman User [IMG]                              
    Registered: October 2003                                                  
    ya.. when i uncomment that line...                                        
    i can see the copy image stored in my temp folder in my hard disk         
                                                                              
    but i still can't displayed back the image stored in databse.. Crying or  
    Very sad                                                                  
                                               Report message to a moderator  
    [IMG]  Send a private message to this user                   [IMG] [IMG]  
    Displaying image data immediately       Wed, 01 October 2003 21:55 Go to  
    w/out saving to Disk                      previous messageGo to previous  
                                                                     message  
    Mike                                                                      
    Messages: 946                      Power User                             
    Registered: May 2003               Site Admin                             
    Location: Calgary, Alberta, Canada                                        
    And the image is not corrupt? I will have to check your code later when   
    I have time.                                                              
                                                                              
                                                                              
    Regards,                                                                  
    Mike Hillyer                                                              
    Webmaster/Moderator                                                       
    http://www.vbmysql.com                                                    
    Read my blog at: http://www.vbmysql.com/mike/blog                         
                                               Report message to a moderator  
    [IMG]  Send a private message to this user                   [IMG] [IMG]  
                                                                              
    Pages (2): [1  2  >  >>]    Switch to threaded view of this topic Create  
                                                    a new topic Submit Reply  
                                                                              
             Previous Topic: Array Dimensions Are Invalid                     
                 Next Topic: How can i Print Image in Data report using Blob  
                                                                              
    [__________]   [ Rate ]                                                   
                                                                              
    Goto Forum:                                                               
    [______________________________]   [ Go ]                                 
                                              [ Syndicate this forum (XML) ]  
                              -=] Back to Top [=-                             
                   Current Time: Fri Feb 27 16:17:10 MST 2004                 
    Total time taken to generate the page: 0.10450 seconds                    

                            .:: Contact :: Home ::.                           
                                                                              
                         Powered by: FUDforum 2.6.1RC1                        
             Copyright (c)2001-2004 Advanced Internet Designs Inc.