Load and save an image to database


Author
Message
Juan Carlos Pazos
Juan Carlos Pazos
StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)
Group: Forum Members
Posts: 144, Visits: 227
Hi

I need to add some images to my records, I review and follow all the steps I can find in the sam´ples (CRM smaple), and at this point I have my SQL 2005 database with the same type of field property as your sample field for the image.

Also modified the BO to match exactly as yours (for the images) and add the code to my form.

I can load the image, but if I save the record and navigate to another or close and open the form the image not show.

I'm using SF controls only.

Can you please post a sample of how to save an load an image in a maintenance form.

Kindest regards

Smile Everything is possible, just keep trying...

Juan Carlos Pazos
Juan Carlos Pazos
StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)
Group: Forum Members
Posts: 144, Visits: 227
Hi

Sorry, I check the forum before and not found an answer, after the post I check again and found the solution:

You have to customize the field within the BOMapper and do two things:

1) Set the data type to System.Drawing.Bitmap
2) Check the "Serialized" box.

From this post: http://forum.strataframe.net/Topic2710-14-1.aspx

Thanks anyway.



Smile Everything is possible, just keep trying...
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Yeah, you can do it that way or you can save it as a byte array depending on what will better fit your needs.  When you use serialization things tend to bloat a little, so it will definitely work, but it will take up less space if you use a byte array instead of a serialized image.  You can still expose the property as an Image so that you are always dealing with it in that manner, but within the property you can save it as a byte array if you wanted to:

Public Property MyImageField() As Image
    Get
       '-- Establish Locals
       Dim r As Bitmap
       Dim stream As System.IO.MemoryStream

       '-- See if there is anything to return
       If CurrentRow.Item("MyImageField") IsNot DbNull.Value AndAlso _
          CType(CurrentRow.Item("MyImageField"), Byte()).Length > 0 Then
            stream = New System.IO.MemoryStream(CTYpe(CurrentRow.Item("MyImageField"), Byte()))
            r = Bitmap.FromStream(stream)
            stream.Close()
       Else
            '-- You can create a blank image if you would like so a valid image is always returned.  Or possibly
            '    a default image.
            r = New Bitmap(16,16)
       End If

       Return CType(r. Image)
    End Get
    Set(ByVal value As Image)
        '-- Establish Locals
        Dim stream As New System.IO.MemoryStream()

        '-- Save the image to a stream
        r.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg)

        '-- Now save the stream as a byte array
        Me.CurrentRow.Item("MyImageField") = StreamToByteArray(stream)

        '-- Clean Up
        stream.Close()
    End Set
End Property

''' <summary>
    ''' Converts a stream into a byte array
    ''' </summary>
    ''' <param name="MediaStream"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function StreamToByteArray(ByVal MediaStream As System.IO.Stream) As Byte()
        '-- Establish Locals
        Dim loReturn(CType(MediaStream.Length, Integer)) As Byte
        Dim laBuffer(30000) As Byte
        Dim lnBytesRead As Integer
        Dim lnCurrent As Integer = 0
        '-- Move to the beginning of the stream
        MediaStream.Seek(0, IO.SeekOrigin.Begin)
        '-- Read the first chunk
        lnBytesRead = MediaStream.Read(laBuffer, 0, laBuffer.Length)
        While lnBytesRead <> 0
            '-- Write to the return buffer
            Array.Copy(laBuffer, 0, loReturn, lnCurrent, lnBytesRead)
            '-- Update the current index
            lnCurrent += lnBytesRead
            '-- Read the next chunk
            lnBytesRead = MediaStream.Read(laBuffer, 0, laBuffer.Length)
        End While
        '-- Return the array
        Return loReturn
    End Function


GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search