Software Expressions

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

Goto

Categories

 

Serializing images


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

Leave a Reply

Couldn't find your convert utility. Check that you have ImageMagick installed.