26 October 2015

Generating Image from Binary Data and setting its Properties

Sometimes we have a requirement of recreating images from binary data stored in database and assign properties to it. Recreating images from binary data can be easily done using .NET in-built functions. To assign or read properties of images, we can use Microsoft DSOFile library which lets you edit office document properties. The DLL provided by this library is – DsoFile.dll

Implementation

We need to first add the DsoFile.dll in our project. We then need to add its namespace in the class where we want to use the functionality of this class.

To achieve the functionality, we have to take following steps:-

Step 1 - First create the image from binary data. We wrote a function for this that receives binary data in form of Byte array as input and provides an Image as output. Here Image is .NET class from namespace System.Drawing. The logic is as follows –
  • Create an object of type MemoryStream.
  • Create an object of type Image
  • Check if byte array having binary data is not empty.
  • Fill the memory stream with binary data from byte array
  • After that call the .NET inbuilt function Image.FromStream. This function takes MemoryStream as input and returns System.Drawing.Image as output.
  • Our Image is ready from binary data.

This logic is shown in following code.

Public Function Byte2Image(ByVal ByteArr() As Byte) As Image

        Dim ImageStream As MemoryStream
        Dim NewImage As Image

        If ByteArr.GetUpperBound(0) > 0 Then
            ImageStream = New MemoryStream(ByteArr)
            NewImage = Image.FromStream(ImageStream)
        Else
            NewImage = Nothing
        End If

        Return NewImage

    End Function

Step 2 - Once we have the image ready, we can set the date and time when photo was taken. To achieve this, following logic is to be used
  • Create object of PropertyItem
  • It is difficult to set property items, because the PropertyItem class has no public constructors. One way to work around this restriction is to obtain a PropertyItem by retrieving the PropertyItems property value or calling the GetPropertyItem method of an Image that already has property items. Then you can set the fields of the PropertyItem and pass it to SetPropertyItem.
  • To do this, assign this object to image property item having some random property id of 20624. 
  • Once object is created, changed its property id to 36867 which is id of property ‘Image date and Time taken’.
  • Images have many properties each with a property id. Complete list of all property Ids is available at this link - http://msdn.microsoft.com/en-us/library/system.drawing.imaging.propertyitem.id.aspx
  • Now change the type of this property to 2.
  • Assign the value of this property to date and time of image. Before assigning value, you need to convert the ‘Date-Time’ available as string in form of Byte array. This is done by creating a function which is explained in next step.

Dim imgProperty As PropertyItem = newImage.GetPropertyItem(20624)

       imgProperty.Id = 36867
       imgProperty.Type = 2
       imgProperty.Value = ConvertStringtoByteArray(<Date>)
       newImage.SetPropertyItem(imgProperty)

Step 3 - To convert string to byte array, following function can be created.
  • This function takes string as input.
  • Create an object of type System.Text.ASCIIEncoding.
  • Create of object of type Byte.
  • Now use .NET inbuilt function System.Text.ASCIIEncoding.GetBytes which converts our string to byte array.
  • Return this byte array from function.

Public Function ConvertStringtoByteArray(ByVal strText As String) As Byte()

        Dim encText As New System.Text.ASCIIEncoding()
        Dim btText() As Byte
        btText = encText.GetBytes(strText)

        Return btText

  End Function

Once this is done, the ‘Image date and time taken’ property will be set. Now using DsoFile DLL, we can set extended properties of image like Author, title, comments, subject. These are general properties of any office document which can be seen by right clicking on document and checking its properties summary

Step 4 - To set these properties, following logic can be used
  • Create a handle object of type DSOFile.OleDocumentProperties.
  • Using this handle object, open the image using image path. To open the image in write mode, pass second parameter of function as ‘False’. 
  • Now handle object will point to our image. Under summary properties, we can set any property of image like author, title, comments, subject etc.
  • Once set, save the image by calling ‘Save’ function on handle object.
  • Close the image by calling ‘Close’ function on handle object. Pass argument to Close function as ‘True’. This ensures that image is saved before closing.

Dim dsoFileHandle As New DSOFile.OleDocumentProperties

      dsoFileHandle.Open(imagePath, False)

      dsoFileHandle.SummaryProperties.Author = “PhotoAuthor”
      dsoFileHandle.SummaryProperties.Title = “PhotoTitle”
      dsoFileHandle.SummaryProperties.Comments = “PhotoComment”
      dsoFileHandle.SummaryProperties.Subject = “PhotoSubject”

      dsoFileHandle.Save()

      dsoFileHandle.Close(True)

Once this is done, we will have our image with required properties set. The approach of using DSOFile can be used for any office document files as well.

No comments: