Results 1 to 3 of 3

Thread: update image

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2002
    Location
    Florida
    Posts
    24

    update image

    does anyone know how to update an image from vb.net to a sql server 2000 table?

    thanks

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Do you mean store the image in the database?

    VB Code:
    1. Public Sub ImportImageToDB(ByVal dr As DataRow, ByVal Field As String, ByVal Img As Image)
    2.         '---------------------------------------------------------------------------------------
    3.         ' Author    : Eddiem
    4.         ' Purpose   : Writes an unknown file type to a database.
    5.         ' Syntax    : ImportImgagToDB(dr,"Picture",PictureBox1.Image)
    6.         '---------------------------------------------------------------------------------------
    7.         Dim ms As MemoryStream = New MemoryStream()
    8.         Img.Save(ms, Img.RawFormat)
    9.         Dim data(ms.Length) As Byte
    10.         ms.Position = 0
    11.  
    12.         ms.Read(data, 0, ms.Length)
    13.         dr.Item(Field) = data
    14.         ms.Close()
    15.     End Sub
    16.  
    17.     Public Function ExportImageFromDB(ByVal dr As DataRow, ByVal Field As String) As Image
    18.         '---------------------------------------------------------------------------------------
    19.         ' Author    : Eddiem
    20.         ' Purpose   : Gets an Image from a database.
    21.         '---------------------------------------------------------------------------------------
    22.         Dim ms As MemoryStream = New MemoryStream()
    23.         Dim data() As Byte
    24.         Dim img As Image
    25.         Try
    26.             data = CType(dr.Item(Field), Byte())
    27.             ms.Write(data, 0, data.GetUpperBound(0))
    28.  
    29.             img = Image.FromStream(ms)
    30.         Catch ex As Exception
    31.             'handle any errors other than no picture set in db yet
    32.             If Not TypeOf ex Is ArgumentOutOfRangeException Then
    33.                 Throw ex
    34.             End If
    35.         Finally
    36.             ms.Close()
    37.             ms = Nothing
    38.         End Try
    39.  
    40.         Return img
    41.     End Function
    42.  
    43.     Public Function ExportImageFromDB(ByVal dr As SqlDataReader, ByVal Field As String) As Image
    44.         '---------------------------------------------------------------------------------------
    45.         ' Author    : Eddiem
    46.         ' Purpose   : Gets an Image from a database.
    47.         '---------------------------------------------------------------------------------------
    48.         Dim ms As MemoryStream = New MemoryStream()
    49.         Dim data() As Byte
    50.         Dim img As Image
    51.         Try
    52.             data = CType(dr.Item(Field), Byte())
    53.             ms.Write(data, 0, data.GetUpperBound(0))
    54.  
    55.             img = Image.FromStream(ms)
    56.         Catch ex As Exception
    57.             'handle any errors other than no picture in db
    58.             If Not TypeOf ex Is ArgumentOutOfRangeException Then
    59.                 Throw ex
    60.             End If
    61.         Finally
    62.             ms.Close()
    63.             ms = Nothing
    64.         End Try
    65.  
    66.         Return img
    67.     End Function

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2002
    Location
    Florida
    Posts
    24

    thanks, works great

    thanks

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