StrataFrame Forum

Serializing Image Data, Reporting, Etc

http://forum.strataframe.net/Topic8388.aspx

By Jerome Barnett - 4/20/2007

I'm new to serialization but I think I may have discovered an issue with generically serializing Image objects. (Then again it could all be in my head)



I have images which I am scanning into a System.Drawing.Image object and then saving into an Image column in a SQL 2005 DB.

Reading and writing the images works great...until...I started using Crystal Reports with my app and the Image fields in my report would all be blank.



I did some trouble shooting and the only thing I could do to get it working was to change the property setter/getter for my image columns in my BO class.

(I also had to rescan my images, but it's a new app, so no biggie)



I changed the property definition to the following.



Public Property [InsCardFront]() As System.Drawing.Image

Get

Try

Dim loStream As New MemoryStream()

loStream.Write(CType(CurrentRow.Item("InsCardFront"), Byte()), 0, CType(CurrentRow.Item("InsCardFront"), Byte()).Length)

Return System.Drawing.Image.FromStream(loStream, True)

Catch

Return Nothing

End Try

End Get

Set(ByVal value As System.Drawing.Image)

Dim loStream As New MemoryStream()

value.Save(loStream, System.Drawing.Imaging.ImageFormat.Jpeg)

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

End Set

End Property




It seems that when you serialize an Image object you get more than just image data and that is all that Crystal reports is looking for.



Could someone who knows a little more about serialization correct or inform me if I'm right in thinking this.



If I am right then would it be possible to add an 'Image' option to the BO designer which would change the generated code to something like the above?

It seems that storing the generic image data would be more desirable/flexible than storing a .NET Image object.



Let me know.



Thanks



Jerome
By Jerome Barnett - 4/20/2007

Here's the original generated code...



Public Property [InsCardFront]() As System.Drawing.Image

Get

Try

Return CType(Me.BinaryFormatter.Deserialize(New MemoryStream(CType(CurrentRow.Item("InsCardFront"), Byte()))), System.Drawing.Image)

Catch

Return Nothing

End Try

End Get

Set(ByVal value As System.Drawing.Image)

Dim loStream As New MemoryStream()

Me.BinaryFormatter.Serialize(loStream, value)

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

End Set

End Property
By StrataFrame Team - 4/23/2007

I'm not exactly sure what Crystal Reports is expecting, but if they're expecting just raw data for the image, then you can just return the byte array out of the field.  Then, just add a custom field with the code that you entered that would call .Save(Jpeg) on the image.  However, if Crystal is expecting a System.Drawing.Image object, then both methods will work equally well.  The serialized version will take up a bit more space in the database because it's not forcing the file to JPEG format, and it's also storing the extra stuff from the serialization context, but both versions of code return a System.Drawing.Image object, so they are roughly equivalent. 

I think your best bet would be to either use the custom code functionality and use the code that you specified, or leave the field as a Byte[] and create a custom property that would save the raw image data in the format that you specify.