Images and BOs

Images and BOs

Old forum URL: forums.lhotka.net/forums/t/181.aspx


zman88a posted on Tuesday, May 23, 2006

I was hoping someone could help me with an image problem I am having.  I have a BO that stores Client Account information including a Logo (to be displayed in reports, etc.)  It is being stored in the database as bytes and converted to image in the BO.

Private mLogo As Byte()

Public Property Logo() As Image
Get
CanReadProperty(True)
If Not mLogo Is Nothing Then
      'Convert byte array in variable to memory stream and the convert to image
      
Return System.Drawing.Image.FromStream(New System.IO.MemoryStream(mLogo))
   Else
      Return My.Resources.NoLogo 'Load No Logo Image
   End If
End Get
Set(ByVal value As Image)
   CanWriteProperty(
True)
   Dim tempMemStream As New System.IO.MemoryStream
   'Convert Image to MemoryStream (Format JPEG)
   value.Save(tempMemStream, System.Drawing.Imaging.ImageFormat.Jpeg)
   'Set Start position to read from
   tempMemStream.Position = 0
   'Create byte array large enough to hold stream
   Dim photoBytes(CType(tempMemStream.Length, Integer) - 1) As Byte
   'Convert memorystream to byte array
   tempMemStream.Read(photoBytes, 0, CType(tempMemStream.Length, Integer))
   'Add byte array to the member variable
   If Not mLogo.Equals(photoBytes) Then
      mLogo = photoBytes
      'Mark Dirty
      PropertyHasChanged()
   End If
   'Close the Memory Stream
   tempMemStream.Close()
End Set
End Property

I have a picture box on a winform that is bound to this property and it displays the "no logo" resource fine when there is no data, but when there is an actual image stored in the database it just displays a empty box. I've debugged both the fetching and saving of the image and everything seems to work fine. I've used this exact code in projects based on CSLA 1.5 and it worked fine. 

If anyone has any ideas they would be greatly appreciated.

Thanks

RockfordLhotka replied on Wednesday, May 24, 2006

If I recall correctly, this is one of those cases where streams don't work like they should. I think there's an issue where the FromStream method of Image doesn't properly read from a MemoryStream, and you need to do some manual copying of bytes. I could be mistaken on this point, but about 3 years ago when Magenic wrote the VB Toolkit book I remember addressing an issue along these lines...

zman88a replied on Wednesday, May 24, 2006

Well, it seems that there was nothing wrong with the code afterall.  Silly me, when I set up the database for this new project I set the logo field type set as byte instead of image.  Once I changed it everything worked like it was supposed to.

Copyright (c) Marimer LLC