View Single Post
  #2 (permalink)  
Old January 24th, 2005, 05:44 AM
Duncan Duncan is offline
Authorized User
 
Join Date: Jun 2003
Location: Norfolk, , United Kingdom.
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

I've only done this from a win forms app not a web app so you might need to change one or two things to get it to work.

To load the image from the database I retrieve the image from the DB and place it into a Byte(), on binding the datato the picture box I load the Byte() into a memorystream and then load that into the picture box.

_User.Photo has the image in binary format loaded from the stored procedure in an arraylist.

Dim msData As New MemoryStream(CType(_User.Photo, Byte()))
            'insert image into picture box from database
            picUserImage.Image = Image.FromStream(msData)


to be able to save theimage back to the database use the following function

Private Function ImageToMemory() As Byte()
'================================================= ==================='Name:Image To Memory
'Purpose:copies the image stored in a picture box and returns it to
' a memory stream so the image can be saved back to the database.
'Returns:<insert any return information here>
'Input:<insert any input parameters here>
'Output:<insert any output parmeters here>
'Author:Duncan Faulkner
'Date Created:April 2004
'================================================= ==================='Last Modified:
'<insert authors name here>
'<insert reason for change here>
'================================================= ===================
'retrieve image in picture box to stream for saving in database
Dim ms As MemoryStream = New MemoryStream
picUserImage.Image.Save(ms, Imaging.ImageFormat.Jpeg)
Dim bytBlobdata(CType(ms.Length, Int32) - 1) As Byte
ms.Position = 0
ms.Read(bytBlobdata, 0, CType(ms.Length, Int32))

Return bytBlobdata

End Function

to call this function use
_User.Photo = ImageToMemory()

then call the save stored procedure, remember to include in your paramaters collection the image length, and the image

Params(1) = New SqlParameter("@Data", SqlDbType.Image, users.Photo.Length, ParameterDirection.Input, False, 0, 0, Nothing, DataRowVersion.Current, users.Photo)

Params(1).value = users.Photo

HTH



Duncan
Reply With Quote