save uploaded image thumbnail to sql db image field

Discussion in 'ASP .Net' started by dodgeyb, Dec 14, 2007.

  1. dodgeyb

    dodgeyb Guest

    Hi there,
    Trying to allow client to upload image, thumbnail it, and save it into
    sql table image field.
    Code compiles & runs but image cannot be retrieved. any clues what I'm
    doing wrong pls !


    Dim origImage As System.Drawing.Image =
    System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream)

    ' rewind the input stream to read it again to store the
    original image in the database
    FileUpload1.PostedFile.InputStream.Seek(0, SeekOrigin.Begin)

    'make thumbnail

    Dim thumbnail As System.Drawing.Image = New
    System.Drawing.Bitmap(120, 120)
    Dim thisFormat = origImage.RawFormat
    Dim ms As MemoryStream = New MemoryStream()
    thumbnail.Save(ms, thisFormat)
    'System.Drawing.Imaging.ImageFormat.Jpeg)
    ms.Seek(0, SeekOrigin.Begin)

    Dim buffer(ms.Length) As Byte
    ms.Read(buffer, 0, buffer.Length)

    Dim cnn As New
    SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings(1).ConnectionString)
    cnn.Open()

    Dim cmd As New SqlCommand("UPDATE STAFF SET PIC=@img WHERE
    STAFF_ID=26", cnn)

    Dim ImageContent(buffer.Length) As Byte
    Dim intStatus As Integer
    intStatus = ms.Read(ImageContent, 0, buffer.Length)
    Dim prmImage As New SqlParameter("@img", SqlDbType.Image)
    prmImage.Value = ImageContent
    cmd.Parameters.Add(prmImage)

    cmd.ExecuteNonQuery()
    cmd.Dispose()
    cnn.Close()
    cnn.Dispose()
    Response.Write("done")
     
    dodgeyb, Dec 14, 2007
    #1
    1. Advertising

  2. Your code that puts image into DB looks good except i do not see anywere
    where you actually put a thumbnail image into
    variable 'thumbnail', you just create empty thumbnail and never make an
    actual picture.
    but may be you jsut skiped that code.
    then probably youe code that reads image from DB is bad

    PS: MemoryStream has GetBuffer method..

    That would save you creation of ImageContent byte array.

    George


    "dodgeyb" <> wrote in message
    news:...
    > Hi there,
    > Trying to allow client to upload image, thumbnail it, and save it into
    > sql table image field.
    > Code compiles & runs but image cannot be retrieved. any clues what I'm
    > doing wrong pls !
    >
    >
    > Dim origImage As System.Drawing.Image =
    > System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream)
    >
    > ' rewind the input stream to read it again to store the
    > original image in the database
    > FileUpload1.PostedFile.InputStream.Seek(0, SeekOrigin.Begin)
    >
    > 'make thumbnail
    >
    > Dim thumbnail As System.Drawing.Image = New
    > System.Drawing.Bitmap(120, 120)
    > Dim thisFormat = origImage.RawFormat
    > Dim ms As MemoryStream = New MemoryStream()
    > thumbnail.Save(ms, thisFormat)
    > 'System.Drawing.Imaging.ImageFormat.Jpeg)
    > ms.Seek(0, SeekOrigin.Begin)
    >
    > Dim buffer(ms.Length) As Byte
    > ms.Read(buffer, 0, buffer.Length)
    >
    > Dim cnn As New
    > SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings(1).ConnectionString)
    > cnn.Open()
    >
    > Dim cmd As New SqlCommand("UPDATE STAFF SET PIC=@img WHERE
    > STAFF_ID=26", cnn)
    >
    > Dim ImageContent(buffer.Length) As Byte
    > Dim intStatus As Integer
    > intStatus = ms.Read(ImageContent, 0, buffer.Length)
    > Dim prmImage As New SqlParameter("@img", SqlDbType.Image)
    > prmImage.Value = ImageContent
    > cmd.Parameters.Add(prmImage)
    >
    > cmd.ExecuteNonQuery()
    > cmd.Dispose()
    > cnn.Close()
    > cnn.Dispose()
    > Response.Write("done")
     
    George Ter-Saakov, Dec 14, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt G
    Replies:
    1
    Views:
    1,198
    Deepak Kumar Vasudevan
    Aug 22, 2003
  2. NATO24
    Replies:
    2
    Views:
    3,134
    Nathan
    Mar 26, 2005
  3. David Lozzi
    Replies:
    1
    Views:
    517
    john smith
    Feb 1, 2006
  4. Roberto
    Replies:
    3
    Views:
    331
  5. Sound
    Replies:
    2
    Views:
    498
    Randy Webb
    Sep 28, 2006
Loading...

Share This Page