Business object mapper and images


Author
Message
GRocchi
GRocchi
StrataFrame Beginner (48 reputation)StrataFrame Beginner (48 reputation)StrataFrame Beginner (48 reputation)StrataFrame Beginner (48 reputation)StrataFrame Beginner (48 reputation)StrataFrame Beginner (48 reputation)StrataFrame Beginner (48 reputation)StrataFrame Beginner (48 reputation)StrataFrame Beginner (48 reputation)
Group: Forum Members
Posts: 18, Visits: 56
I have an image in my database, I setup the business object mapper with CustomObject Type = System.Drawing.Bitmap and the flag serialize to true, null value option= don't allow null. I rebuild and get the following working code:
Set(ByVal value As System.Drawing.Bitmap)
Dim loStream As New MemoryStream()
Me.BinaryFormatter.Serialize(loStream, value)
Me.CurrentRow.Item("Immagine") = loStream.ToArray()
End Set

When I change the option null value option= return alternate on null/Set null on alternate(reference type) the code generated is the following:

Set(ByVal value As System.Drawing.Bitmap)

Dim loStream As New MemoryStream()

if value IsNot Nothing Then

Me.CurrentRow.Item("Immagine") = loStream.ToArray()

Else

Me.CurrentRow.Item("Immagine") = DBNull.Value

End If

End Set

It seems that the row

Me.BinaryFormatter.Serialize(loStream, value)   

gets missing. In fact I added it manually and all works fine.

Is there a better way to achieve this? Is this by design?

Thanks in advance


Gianpaolo Rocchi

Reply
Trent Taylor
Trent Taylor
StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 7K
Really, if you are going to store a bitmap in a database, I recomemnd against using serialization.  It is much larger than storing a byte array of the saved JPG or PNG.  Generally I will create a custom fiedl that accepts an Image and updates the underlying Byte() strong-typed property (database column).  I will also craete an empty or default image for a Get in the case that the image has not been set (Null could be taken into account as well).  Here is an example of one of my custom properties in our medical app:

<Browsable(False), _
BusinessFieldDisplayInEditor(), _
Description("Signature as an Image"), _
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public ReadOnly Property cct_SignatureAsImage() As System.Drawing.Image
Get
    '-- Establish Locals
    Dim r As Bitmap

    '-- See if there is an image to return
    If cct_Signature.Length = 0 Then
        '-- Create a new image appropriatly sized
        r = New Bitmap(Preferences.POSPreferences.SignatureImageWidth, Preferences.POSPreferences.SignatureImageHeight)

        '-- Just clear the image to a white canvas
        Dim g As Graphics = Graphics.FromImage(r)
        g.Clear(Color.White)

        '-- Clean Up
        g.Dispose()
    Else
        '-- Create a memory stream to house the image
        Dim mem As New System.IO.MemoryStream

        '-- Write the signature bytes to a memory stream
        mem.Write(cct_Signature, 0, cct_Signature.Length)
        mem.Seek(0, SeekOrigin.Begin)
        r = New Bitmap(mem)

        '-- Clean Up
        mem.Dispose()
    End If

    '-- Return the results
    Return r
End Get
End Property


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