Software Expressions

.Net / Software / Visual Studio / TFS / other ramblings
 

Goto

Categories

 

Archive for the 'CSLA' Category

11 14th, 2008

Serializing images

Author: Troy

The System.Drawing.Image class is not serializable by default. This class can be used to serialize images as well as load the image from the IDataRecord.

   1:  Imports System.Drawing
   2:  Imports System.Runtime.Serialization
   3:  Imports System.Drawing.Imaging
   4:  Imports System.IO
   5:   
   6:  <Serializable()> _
   7:  Public Class SerializableImage
   8:      Implements ISerializable
   9:   
  10:      Private _master As MasterEnum = MasterEnum.Image
  11:      Private _imageBuffer As Byte()
  12:      Private _image As Image
  13:      Private _serializationFormat As ImageFormat = ImageFormat.Png
  14:   
  15:      Public Property Image() As Image
  16:          Get
  17:              If _master <> MasterEnum.Image Then
  18:                  CreateImageFromBuffer()
  19:              End If
  20:              Return _image
  21:          End Get
  22:          Set(ByVal value As Image)
  23:              If (value Is _image) Then Return
  24:              _image = value
  25:              ‘The image becomes the master once the setter is used
  26:              _master = MasterEnum.Image
  27:              _imageBuffer = Nothing
  28:          End Set
  29:      End Property
  30:   
  31:      Private Sub CreateImageFromBuffer()
  32:          ‘Once this method is called, the image becomes the master
  33:          _master = MasterEnum.Image
  34:          ‘Check if there is a buffer
  35:          If _imageBuffer Is Nothing OrElse _imageBuffer.Length = 0 Then
  36:              Image = Nothing
  37:          Else
  38:              ‘http://support.microsoft.com/?id=814675
  39:              Using s As New MemoryStream(_imageBuffer)
  40:                  If Not s Is System.IO.Stream.Null Then
  41:                      Dim tempImage As Image = _
  42:                          DirectCast(Bitmap.FromStream(s), Bitmap)
  43:                      Image = New Bitmap(tempImage)
  44:                      Dim g As Graphics = Graphics.FromImage(tempImage)
  45:                      g.DrawImage(Image, New PointF(0, 0))
  46:                      g.Dispose()
  47:                      tempImage.Dispose()
  48:                  End If
  49:                  s.Close()
  50:              End Using
  51:          End If
  52:      End Sub
  53:   
  54:      Private Property ImageBuffer() As Byte()
  55:          Get
  56:              Select Case _master
  57:                  Case MasterEnum.Image
  58:                      ‘Used for serialization
  59:                      Return CreateBufferFromImage()
  60:                  Case MasterEnum.Buffer
  61:                      Return _imageBuffer
  62:              End Select
  63:              Return _imageBuffer
  64:          End Get
  65:          Set(ByVal value As Byte())
  66:              If (value Is _imageBuffer) Then Return
  67:              _imageBuffer = value
  68:              ‘The Buffer becomes the master
  69:              _master = MasterEnum.Buffer
  70:              _image = Nothing
  71:          End Set
  72:      End Property
  73:   
  74:      Private Function CreateBufferFromImage() As Byte()
  75:          If _image Is Nothing Then Return Nothing
  76:          ‘Convert the image to a byte array using the serialization format
  77:          Using stream As New MemoryStream
  78:              _image.Save(stream, SerializationFormat)
  79:              Dim result As Byte() = stream.ToArray()
  80:              stream.Close()
  81:              Return result
  82:          End Using
  83:      End Function
  84:   
  85:      Public Property SerializationFormat() As ImageFormat
  86:          Get
  87:              Return _serializationFormat
  88:          End Get
  89:          Set(ByVal value As ImageFormat)
  90:              _serializationFormat = value
  91:          End Set
  92:      End Property
  93:   
  94:      Public Sub New()
  95:   
  96:      End Sub
  97:   
  98:      Public Sub New(ByVal image As Image)
  99:          Me.Image = image
 100:      End Sub
 101:   
 102:      Public Sub New(ByVal record As IDataRecord, ByVal fieldIndex As Integer)
 103:          Load(record, fieldIndex)
 104:      End Sub
 105:   
 106:      Public Sub New(ByVal record As IDataRecord, ByVal fieldName As String)
 107:          Load(record, fieldName)
 108:      End Sub
 109:   
 110:      Private Sub Load(ByVal record As IDataRecord, ByVal fieldName As String)
 111:          If record Is Nothing Then Throw New ArgumentNullException(“record”)
 112:          Load(record, record.GetOrdinal(fieldName))
 113:      End Sub
 114:   
 115:      Private Sub Load(ByVal record As IDataRecord, ByVal fieldIndex As Integer)
 116:          If record Is Nothing Then Throw New ArgumentNullException(“record”)
 117:          If record.IsDBNull(fieldIndex) Then Return
 118:          ImageBuffer = DirectCast(record.GetValue(fieldIndex), Byte())
 119:      End Sub
 120:   
 121:      Public Sub New(ByVal info As SerializationInfo, _
 122:          ByVal context As StreamingContext)
 123:          ‘Image
 124:          Dim data As Byte() = _
 125:              DirectCast(info.GetValue(“image”, GetType(Byte())), Byte())
 126:          If data IsNot Nothing AndAlso data.Length > 0 Then
 127:              ‘http://support.microsoft.com/?id=814675
 128:              Using s As New MemoryStream(data)
 129:                  If Not s Is System.IO.Stream.Null Then
 130:                      Dim tempImage As Image = _
 131:                          DirectCast(Bitmap.FromStream(s), Bitmap)
 132:                      Image = New Bitmap(tempImage)
 133:                      Dim g As Graphics = Graphics.FromImage(tempImage)
 134:                      g.DrawImage(Image, New PointF(0, 0))
 135:                      g.Dispose()
 136:                      tempImage.Dispose()
 137:                  End If
 138:                  s.Close()
 139:              End Using
 140:          End If
 141:      End Sub
 142:   
 143:      Public Sub GetObjectData(ByVal info As SerializationInfo, +
 144:          ByVal context As StreamingContext) Implements ISerializable.GetObjectData
 145:          info.AddValue(“image”, ImageBuffer, GetType(Byte()))
 146:      End Sub
 147:   
 148:      Private Enum MasterEnum
 149:          Buffer
 150:          Image
 151:      End Enum
 152:   
 153:  End Class

   1:  Public Class Project
   2:      Inherits BusinessBase(Of Project)
   3:   
   4:      Public ReadOnly Property LineItems() As LineItemsCollection
   5:          Get
   6:              CanReadProperty(True)
   7:              If _lineItems Is Nothing Then
   8:                  If Me.IsNew Then
   9:                      _lineItems = LineItemList.NewLineItemList(Me)
  10:                  Else
  11:                      _lineItems = LineItemList.GetLineItems(Me)
  12:                  End If
  13:                  AddHandler _lineItems.ListChanged, _
  14:                  AddressOf LineItemChangedHandler
  15:              End If
  16:              Return _lineItems
  17:          End Get
  18:      End Property
  19:   
  20:      Public Sub LineItemChangedHandler(_
  21:          ByVal sender As Object, _
  22:          ByVal e As System.ComponentModel.ListChangedEventArgs)
  23:          If (e.ListChangedType = ListChangedType.ItemAdded) Then
  24:              _lineItems(e.NewIndex).Project = Me
  25:          End If
  26:      End Sub
  27:   
  28:   
  29:  End Class
  30:   
  31:  Public Class LineItem
  32:      Inherits BusinessBase(Of LineItem)
  33:   
  34:      <NotUndoable()> <NonSerialized()> Private _project As Project
  35:   
  36:      Public Property Project() As Project
  37:          Get
  38:              If _project Is Nothing Then
  39:                  _project = Project.GetProject(_projectId)
  40:              End If
  41:              Return _project
  42:          End Get
  43:          Friend Set(ByVal value As Project)
  44:              If Not value.ID = _projectId Then
  45:                 _project = proj
  46:              ProjectId = proj.ID
  47:              End If
  48:          End Set
  49:      End Property
  50:   
  51:      Public Property ProjectId() As Guid
  52:          Get
  53:              CanReadProperty(True)
  54:              Return _projectId
  55:          End Get
  56:          Private Set(ByVal value As Guid)
  57:              If _projectId <> value Then
  58:                  _projectId = value
  59:                  PropertyHasChanged()
  60:              End If
  61:          End Set
  62:      End Property
  63:   
  64:  End Class