Results 1 to 3 of 3

Thread: Windows form and images

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    164

    Windows form and images

    hi i have a form designed to store user Details. it connects to a MS Access database to get the intformation, update, delete , add etc.

    if i wanted to add a picture to all my users records how would i go about this, e.g where would image be stored and what sort of code would be needed.

    is there any way wb.net can display an image from the net, e.g can it display an image stored on some webspace ?

  2. #2
    Hyperactive Member ..:RUDI:..'s Avatar
    Join Date
    Aug 2005
    Location
    Yorkshire, England! c0d: Da Vinci
    Posts
    344

    Re: Windows form and images

    I think that you'd just add another field with the image's URL. Then set VB.NET to get the data, and use a picturebox, then call the image location as that field's contents

    Hope that helps.



    My Personal Home | MageBB Home | Elders Online
    Yes, Noteme is my father.

  3. #3
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718

    Re: Windows form and images

    Depends on how you want to store the data. Microsoft Access (And I guess any database) allows you to store binary data in a database, which is what I do, although you can also just store a path if you wish.

    If you wish to store the actual image, you need to set the field up as an OLE Object field, then in your code, you'll need to convert the image data to/from a data type that the database can read/write.

    To store an image, it's simply a matter of converting the image into a Byte array.
    VB Code:
    1. '
    2. 'Visual Basic 2005
    3. Private Shared Function ToByteArray(ByVal data As Image) As Byte()
    4.     If data IsNot Nothing Then
    5.         Dim dataStream As New IO.MemoryStream()
    6.  
    7.         Using dataStream
    8.             data.Save(dataStream, data.RawFormat)
    9.         End Using
    10.  
    11.         Return dataStream.GetBuffer
    12.  
    13.    Else
    14.         Return Nothing
    15.     End If
    16. End Function
    17.  
    18. '
    19. 'Visual Basic 2003
    20. Private Shared Function ToByteArray(ByVal data As Image) As Byte()
    21.     If Not (data Is Nothing) Then
    22.         Dim dataStream As New IO.MemoryStream()
    23.  
    24.         data.Save(dataStream, data.RawFormat)
    25.         dataStream.Close()
    26.  
    27.         Return dataStream.GetBuffer
    28.  
    29.    Else
    30.         Return Nothing
    31.     End If
    32. End Function
    then after you've done that, store that in the column for the record:
    VB Code:
    1. '
    2. 'Visual Basic 2005
    3. Dim myConnection As New OleDbConnection(...)
    4. myConnection.Open()
    5.  
    6. Using myConnection
    7.     Dim myCommand As New OleDbCommand(... , myConnection)
    8.  
    9.     With myCommand.Parameters
    10.         If [i]imageObject[/i] IsNot Nothing Then
    11.             .AddWithValue("?", ToByteArray([i]imageObject[/i]))
    12.  
    13.         Else
    14.             .AddWithValue("?", DBNull.Value)
    15.         End If
    16.     End With
    17.  
    18.     myCommand.ExecuteNonQuery()
    19. End Using
    20.  
    21. '
    22. 'Visual Basic 2003
    23. Dim myConnection As New OleDbConnection(...)
    24. myConnection.Open()
    25.  
    26. Dim myCommand As New OleDbCommand(... , myConnection)
    27.  
    28. With myCommand.Parameters
    29.     If Not ([i]imageObject[/i] Is Nothing) Then
    30.         .AddWithValue("?", ToByteArray([i]imageObject[/i]))
    31.  
    32.     Else
    33.         .AddWithValue("?", DBNull.Value)
    34.     End If
    35. End With
    36.  
    37. myCommand.ExecuteNonQuery()
    38. myConnection.Close()

    Now that that's complete, we have to get the data back as an Image.
    VB Code:
    1. '
    2. 'Visual Basic 2005
    3. Private Shared Function ToImage(ByVal data As Byte()) As Image
    4.     Dim dataStream As New IO.MemoryStream(data)
    5.  
    6.     Using dataStream
    7.         Return Image.FromStream(dataStream)
    8.     End Using
    9. End Function
    10.  
    11. '
    12. 'Visual Basic 2003
    13. Private Shared Function ToImage(ByVal data As Byte()) As Image
    14.     Dim dataStream As New IO.MemoryStream(data)
    15.     Dim ConvertedImage As Image
    16.  
    17.     ConvertedImage = Image.FromStream(dataStream)
    18.     dataStream.Close()
    19.  
    20.     Return ConvertedImage
    21. End Function
    and when you get it from the database using a select statement
    VB Code:
    1. '
    2. 'Visual Basic 2005
    3. Dim myConnection As New OleDbConnection(...)
    4. myConnection.Open()
    5.  
    6. Dim myCommand As New OleDbCommand(... , myConnection)
    7. Dim myDataReader As OleDbDataReader = myCommand.ExecuteReader
    8.  
    9. Using myConnection
    10.     Using myDataReader
    11.         myDataReader.Read()
    12.  
    13.         If myDataReader("[i]columnName[/i]") IsNot DBNull.Value Then
    14.             [i]variable[/i] = ToImage(CType(myDataReader("[i]columnName[/i]"), Byte()))
    15.         End If
    16.  
    17.     End Using
    18. End Using
    19.  
    20. '
    21. 'Visual Basic 2003
    22. Dim myConnection As New OleDbConnection(...)
    23. myConnection.Open()
    24.  
    25. Dim myCommand As New OleDbCommand(... , myConnection)
    26. Dim myDataReader As OleDbDataReader = myCommand.ExecuteReader
    27.  
    28. myDataReader.Read()
    29.  
    30. If Not (myDataReader("[i]columnName[/i]") Is DBNull.Value) Then
    31.     [i]variable[/i] = ToImage(CType(myDataReader("[i]columnName[/i]"), Byte()))
    32. End If
    33.  
    34. myDataReader.Close()
    35. myConnection.Close()

    Obviously you need to replace the italicised items with proper values, however this should work.

    There is however one bug in it, which is more to do with the way Windows handles images using GDI+, when I find out more about it, I'll let you know, but I think it has something to do with loading and then saving the image in the same session.

    Another note, I did this is Visual Basic 2005, hence all the VB2005 code, I've taken out the obvious syntax changes that were introduced, so hopefully the remaining functionality should work as expected in Visual Basic .NET 2003.
    Last edited by Ideas Man; Feb 16th, 2006 at 09:19 AM.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

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