ArcObjects Library Reference  

ThumbnailBuilder

About the Create a custom raster type Sample

[C#]

ThumbnailBuilder.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ADF.CATIDs;
using System.Runtime.InteropServices;

namespace CustomRasterBuilder
{
    interface IThumbnailBuilder : IRasterBuilder
    {
        #region ThumbnailBuilder Members
        ///<summary>
        /// Raster Builder to which the ThumbnailBuilder is attached.
        ///</summary>
        IRasterBuilder InnerRasterBuilder
        {
            get;
            set;
        }
        #endregion
    };

    /// <summary>
    /// The Raster Type Factory class that is used to create the Custom 
    /// Raster Type.
    /// </summary>
    [Guid("C6629CC4-B301-451a-9481-4D7751E9701C")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("CustomRasterBuilder.ThumbnailFactory")]
    [ComVisible(true)]
    public class ThumbnailFactory : IRasterTypeFactory
    {
        #region Private Members
        IStringArray myRasterTypeNames; // List of Raster Types that the factory can create.
        UID myUID; // UID for the Thumbnail Raster type.
        #endregion

        #region IRasterTypeFactory Members

        public ThumbnailFactory()
        {
            // The Raster Type name should follow the pattern 
            // 'Thumbnail ' follwed by the name of the built-in 
            // Raster Type to attach the Thumbnail Builder to.            
            myRasterTypeNames = new StrArrayClass();
            myRasterTypeNames.Add("Thumbnail Raster Dataset");
            myRasterTypeNames.Add("Thumbnail QuickBird");

            myUID = new UIDClass();
            myUID.Value = "{C6629CC4-B301-451a-9481-4D7751E9701C}";
        }

        /// <summary>
        /// The UID for the factory.
        /// </summary>
        public UID CLSID
        {
            get { return myUID; }
        }

        /// <summary>
        /// The main function which creates the Raster Type object.
        /// </summary>
        /// <param name="RasterTypeName">Name of the Raster Type to create.</param>
        /// <returns></returns>
        public IRasterType CreateRasterType(string RasterTypeName)
        {
            try
            {
                switch (RasterTypeName)
                {
                    case "Thumbnail Raster Dataset":
                        {
                            // Create a Raster Type Name object.
                            IRasterTypeName theRasterTypeName = new RasterTypeNameClass();
                            // Assign the name of the built-in Raster Type to the name object.
                            // The Name field accepts a path to an .art file as well 
                            // the name for a built-in Raster Type.
                            theRasterTypeName.Name = RasterTypeName.Replace("Thumbnail ", "");
                            // Use the Open function from the IName interface to get the Raster Type object.
                            IRasterType theRasterType = (IRasterType)(((IName)theRasterTypeName).Open());
                            if (theRasterType == null)
                            {
                                Console.WriteLine("Error:Raster Type not found " + theRasterTypeName.Name);
                                return null;
                            }

                            // Create a new ThumbnailBuilder object and set it's InnerRasterBuilder property to 
                            // the RasterBuilder from the RasterType object. Then set the thumbnail builder to 
                            // be the RasterBuilder for the RasterType object. This inserts the thumbnail builder in 
                            // between the RasterType and it's RasterBuilder.

                            // Create the Thumbnail Builder 
                            IRasterBuilder thumbnailBuilder = new ThumbnailBuilder();
                            //  Set the InnerRasterBuilder property with current Raster Type's Raster Builder
                            ((ThumbnailBuilder)thumbnailBuilder).InnerRasterBuilder = theRasterType.RasterBuilder;
                            // Set the Raster Builder of theRasterType to the above created thumbnail builder.
                            theRasterType.RasterBuilder = thumbnailBuilder;
                            IName theName = theRasterType.FullName;
                            ((IRasterTypeName)theName).Name = "Thumbnail Raster Dataset";
                            return theRasterType;
                        }

                    case "Thumbnail QuickBird":
                        {
                            // Create a Raster Type Name object.
                            IRasterTypeName theRasterTypeName = new RasterTypeNameClass();
                            // Assign the name of the built-in Raster Type to the name object.
                            // The Name field accepts a path to an .art file as well 
                            // the name for a built-in Raster Type.
                            theRasterTypeName.Name = RasterTypeName.Replace("Thumbnail ", "");
                            // Use the Open function from the IName interface to get the Raster Type object.
                            IRasterType theRasterType = (IRasterType)(((IName)theRasterTypeName).Open());
                            if (theRasterType == null)
                            {
                                Console.WriteLine("Error:Raster Type not found " + theRasterTypeName.Name);
                                return null;
                            }

                            // Create a new TumbnailBuilder object and set it's InnerRasterBuilder property to 
                            // the RasterBuilder from the RasterType object. Then set the thumbnail builder to 
                            // be the RasterBuilder for the RasterType object. This inserts the thumbnail builder in 
                            // between the RasterType and it's RasterBuilder.

                            // Create the Thumbnail Builder 
                            IRasterBuilder thumbnailBuilder = new ThumbnailBuilder();
                            //  Set the InnerRasterBuilder property with current Raster Type's Raster Builder
                            ((ThumbnailBuilder)thumbnailBuilder).InnerRasterBuilder = theRasterType.RasterBuilder;
                            // Set the Raster Builder of theRasterType to the above created thumbnail builder.
                            theRasterType.RasterBuilder = thumbnailBuilder;
                            IName theName = theRasterType.FullName;
                            ((IRasterTypeName)theName).Name = "Thumbnail QuickBird";
                            return theRasterType;
                        }

                    default:
                        return null;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error: Failed to create " + RasterTypeName + ": " + ex.Message);
            }
        }

        /// <summary>
        /// Name of the Raster Type factory.
        /// </summary>
        public string Name
        {
            get { return "Thumbnail Raster Type Factory"; }
        }

        /// <summary>
        /// List of Raster Types which the factory can create.
        /// </summary>
        public IStringArray RasterTypeNames
        {
            get { return myRasterTypeNames; }
        }
        #endregion

        #region COM Registration Function(s)
        [ComRegisterFunction()]
        static void Reg(string regKey)
        {
            ESRI.ArcGIS.ADF.CATIDs.RasterTypeFactory.Register(regKey);
        }

        [ComUnregisterFunction()]
        static void Unreg(string regKey)
        {
            ESRI.ArcGIS.ADF.CATIDs.RasterTypeFactory.Unregister(regKey);
        }
        #endregion
    }

    ///<summary>
    /// This class implements the interface IThumbnailBuilder, IPersistVariant.
    ///</summary>
    [Guid("5CACFBA7-F865-4873-B57C-BE2A46E1C5E3")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("CustomRasterBuilder.ThumbnailBuilder")]
    [ComVisible(true)]
    public class ThumbnailBuilder : IThumbnailBuilder, IPersistVariant, IRasterBuilderInit
    {
        #region Private Members
        public IRasterBuilder innerRasterBuilder; // Inner Raster Builder 
        UID myUID; // UID for the Thumbnail Builder.

        /// <summary>
        /// The following function creates a field called "ThumbNail" and adds it to the existing
        /// fields in the mosaic dataset catalog as a blob.
        /// </summary>
        /// <param name="myFields">List of fields added to the Mosaic Catalog</param>
        private static void AddingNewField(IFields myFields)
        {
            IField pField = new FieldClass(); // Create a new field object
            IFieldEdit objectIDFieldEditor = (IFieldEdit)pField; // Set the field editor for this field
            objectIDFieldEditor.Name_2 = "ThumbNail"; // Set the name of the field
            objectIDFieldEditor.Type_2 = esriFieldType.esriFieldTypeBlob; // Set the type of the field as Blob
            IFieldsEdit fieldsEditor = (IFieldsEdit)myFields; // Add the newly created field to list of existing fields
            fieldsEditor.AddField(pField);
            return;
        }

        /// <summary>
        /// Each BuilderItem contains a Function Raster Dataset which is used to create the thumbnail.
        /// The thumbnail is created by Nearest Neighbor resampling of the Function Raster Dataset. 
        /// The resampled raster is exported as a byte array and saved Into the IMemoryBlobStreamVariant. 
        /// The blob is then inserted into the Thumbnail field.
        /// </summary>
        /// <param name="mybuilderItem">Item containing the Function Dataset to be added to the Mosaic Dataset</param>
        private void Resampling(IBuilderItem mybuilderItem)
        {
            try
            {
                IFunctionRasterDataset pFuncRasterDataset = mybuilderItem.Dataset; // Get the FunctionRasterDataset from mybuilderItem
                IRasterDataset pRasterDataset;
                pRasterDataset = (IRasterDataset)pFuncRasterDataset; // Cast the FunctionRasterDataset to Raster Dataset
                IPropertySet thePropSet = pFuncRasterDataset.Properties; // Get the properties of the raster Dataset
                IRaster praster = pRasterDataset.CreateDefaultRaster(); // Create default raster from the above raster dataset
                praster.ResampleMethod = rstResamplingTypes.RSP_NearestNeighbor; // The raster is resampled by RSP_NearestNeighbor
                IRasterProps pRasterProps = (IRasterProps)praster; // Raster properties are used to update the height, width of the raster
                pRasterProps.Height = 256;
                pRasterProps.Width = 256;

                IRasterExporter pConverter = new RasterExporterClass(); // IRasterExporter object is used to convert the raster to byte array.
                byte[] pBytesArr;
                pBytesArr = pConverter.ExportToBytes(praster, "TIFF"); // Convert the resampled Raster to a Byte array 
                IMemoryBlobStream memBlobStream = new MemoryBlobStream(); // Create new IMemoryBlobStream
                IMemoryBlobStreamVariant varBlobStream = (IMemoryBlobStreamVariant)memBlobStream; // Assign to IMemoryBlobStreamVariant
                object anObject = pBytesArr;
                varBlobStream.ImportFromVariant(anObject); // IMemoryBlobStreamVariant object is assigned the byte array
                thePropSet.SetProperty("ThumbNail", memBlobStream); // and saved to the property "ThumbNail"
            }
            catch (Exception ex)
            {
                System.Exception myExc = new System.Exception(
                    "Error: Failed to Re-Sampled the raster.Thumbnails will not be created." + ex.Message, ex);
                throw myExc;
            }
            return;

        }
        #endregion

        #region ThumbnailBuilder Members
        /// <summary>
        /// This property gets and sets the raster type of the Thumbnail Builder.
        /// </summary>
        public IRasterBuilder InnerRasterBuilder
        {
            get
            {
                return innerRasterBuilder;
            }
            set
            {
                innerRasterBuilder = value;
            }
        }

        public ThumbnailBuilder()
        {
            innerRasterBuilder = null;

            myUID = new UIDClass();
            myUID.Value = "{" + "5CACFBA7-F865-4873-B57C-BE2A46E1C5E3" + "}";
        }
        #endregion

        #region IRasterBuilder Members
        /// <summary>
        /// AuxiliaryFieldAlias property gets and sets the Auxiliary fields Alias
        /// present in the raster builder
        /// </summary>
        public ESRI.ArcGIS.esriSystem.IPropertySet AuxiliaryFieldAlias
        {
            get
            {
                return innerRasterBuilder.AuxiliaryFieldAlias;
            }
            set
            {
                innerRasterBuilder.AuxiliaryFieldAlias = value;
            }
        }

        /// <summary>
        /// Flag to specify whether the Raster Builder can build items in place.
        /// </summary>
        public bool CanBuildInPlace
        {
            get { return innerRasterBuilder.CanBuildInPlace; }
        }

        /// <summary>
        /// For adding Thumbnails, a new field called "Thumbnail" of type blob is created
        /// and added to the Auxiliary Fields in raster builder.
        /// </summary>
        public IFields AuxiliaryFields
        {
            get
            {
                ESRI.ArcGIS.DataSourcesRaster.IRasterBuilder pRasterBuilder = innerRasterBuilder;
                IFields myFields = pRasterBuilder.AuxiliaryFields; // Existing Fields present in the Raster Builder. 
                try
                {
                    int count = myFields.FindField("ThumbNail"); // Check if the Field "ThumbNail" already exists.
                    if (count == -1)
                    {
                        AddingNewField(myFields); // If not add the new field "ThumbNail"
                        pRasterBuilder.AuxiliaryFields = myFields; // Assign the updated Fields back to the raster builder.
                    }
                }
                catch (Exception ex)
                {
                    System.Exception myExc = new System.Exception("Failed to add the field ThumbNail." + ex.Message, ex);
                    throw myExc;
                }
                return innerRasterBuilder.AuxiliaryFields; // Return the updated fields.
            }
            set
            {
                innerRasterBuilder.AuxiliaryFields = value;
            }
        }

        /// <summary>
        /// Prepare the Raster Type for generating Crawler items
        /// </summary>
        /// <param name="pCrawler">Crawler to use to generate the crawler items</param>
        public void BeginConstruction(IDataSourceCrawler pCrawler)
        {
            innerRasterBuilder.BeginConstruction(pCrawler);
        }

        /// <summary>
        /// Call the build function of the inner Raster Type and generate a thumbnail from the Builder Item created.
        /// </summary>
        /// <param name="pItemURI">URI of the Item to be built</param>
        /// <returns></returns>
        public IBuilderItem Build(IItemURI pItemURI)
        {
            IBuilderItem pbuilderItem = innerRasterBuilder.Build(pItemURI); // Generate the IBuilderItem
            Resampling(pbuilderItem); // Generate the Thumbnail from the item.
            return pbuilderItem;
        }

        /// <summary>
        /// Construct a Unique Resource Identifier (URI)
        /// for each crawler item
        /// </summary>
        /// <param name="crawlerItem">Crawled Item from which the URI is generated</param>
        public void ConstructURIs(object crawlerItem)
        {
            innerRasterBuilder.ConstructURIs(crawlerItem);
        }

        /// <summary>
        /// Finish Construction of the URI's
        /// </summary>
        /// <returns></returns>
        public IItemURIArray EndConstruction()
        {
            return innerRasterBuilder.EndConstruction();
        }

        /// <summary>
        /// Generate the next URI.
        /// </summary>
        /// <returns>The URI generated.</returns>
        public IItemURI GetNextURI()
        {
            return innerRasterBuilder.GetNextURI();
        }

        /// <summary>
        /// Get the recommended data crawler for the Raster Type based on 
        /// properties provided by the user.
        /// </summary>
        /// <param name="pDataSourceProperties">List of properties provided by the user</param>
        /// <returns></returns>
        public IDataSourceCrawler GetRecommendedCrawler(IPropertySet pDataSourceProperties)
        {
            return innerRasterBuilder.GetRecommendedCrawler(pDataSourceProperties);
        }

        /// <summary>
        /// Check if the item provided is "stale" or not valid
        /// </summary>
        /// <param name="pItemURI">URI for the item to be checked</param>
        /// <returns></returns>
        public bool IsStale(IItemURI pItemURI)
        {
            return innerRasterBuilder.IsStale(pItemURI);
        }

        /// <summary>
        /// Properties associated with the Raster Type
        /// </summary>
        public IPropertySet Properties
        {
            get
            {
                return innerRasterBuilder.Properties;
            }
            set
            {
                innerRasterBuilder.Properties = value;
            }
        }


        #endregion

        #region IPersistVariant Members
        /// <summary>
        /// UID for the object implementing the Persist Variant
        /// </summary>
        UID IPersistVariant.ID
        {
            get { return myUID; }
        }

        /// <summary>
        /// Load the object from the stream provided
        /// </summary>
        /// <param name="Stream">Stream that represents the serialized Raster Type</param>
        void IPersistVariant.Load(IVariantStream Stream)
        {
            string name = (string)Stream.Read();
            //if (innerRasterBuilder is IPersistVariant)
            //    ((IPersistVariant)innerRasterBuilder).Load(Stream);
            innerRasterBuilder = (IRasterBuilder)Stream.Read(); // Load the innerRasterBuilder from the stream.
        }

        /// <summary>
        /// Same the Raster Type to the stream provided
        /// </summary>
        /// <param name="Stream">Stream to serialize the Raster Type into</param>
        void IPersistVariant.Save(IVariantStream Stream)
        {
            Stream.Write("ThmbnailBuilder");
            //if (innerRasterBuilder is IPersistVariant)
            //    ((IPersistVariant)innerRasterBuilder).Save(Stream);
            Stream.Write(innerRasterBuilder); // Save the innerRasterBuilder into the stream.
        }
        #endregion

        #region IRasterBuilderInit Members
        /// <summary>
        /// Default spatial reference for the MD.
        /// </summary>
        ISpatialReference IRasterBuilderInit.DefaultSpatialReference
        {
            get
            {
                return ((IRasterBuilderInit)innerRasterBuilder).DefaultSpatialReference;
            }
            set
            {
                ((IRasterBuilderInit)innerRasterBuilder).DefaultSpatialReference = value;
            }
        }

        /// <summary>
        /// Parent mosaic dataset for the Raster Builder.
        /// </summary>
        IMosaicDataset IRasterBuilderInit.MosaicDataset
        {
            get
            {
                return ((IRasterBuilderInit)innerRasterBuilder).MosaicDataset;
            }
            set
            {
                ((IRasterBuilderInit)innerRasterBuilder).MosaicDataset = value;
            }
        }

        /// <summary>
        /// The raster type operation helper object associated with this raster type.
        /// </summary>
        IRasterTypeOperation IRasterBuilderInit.RasterTypeOperation
        {
            get
            {
                return ((IRasterBuilderInit)innerRasterBuilder).RasterTypeOperation;
            }
            set
            {
                ((IRasterBuilderInit)innerRasterBuilder).RasterTypeOperation = value;
            }
        }

        /// <summary>
        /// Tracker for when cancel is pressed.
        /// </summary>
        ITrackCancel IRasterBuilderInit.TrackCancel
        {
            get
            {
                return ((IRasterBuilderInit)innerRasterBuilder).TrackCancel;
            }
            set
            {
                ((IRasterBuilderInit)innerRasterBuilder).TrackCancel = value;
            }
        }
        #endregion
    }
}




[Visual Basic .NET]

ThumbnailBuilder.vb

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports ESRI.ArcGIS.DataSourcesRaster
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.ADF.CATIDs
Imports System.Runtime.InteropServices

Namespace CustomRasterBuilder
    Interface IThumbnailBuilder
        Inherits IRasterBuilder
#Region "ThumbnailBuilder Members"
        '''<summary>
        ''' Raster Builder to which the ThumbnailBuilder is attached.
        '''</summary>
        Property InnerRasterBuilder() As IRasterBuilder
#End Region
    End Interface

    ''' <summary>
    ''' The Raster Type Factory class that is used to create the Custom 
    ''' Raster Type.
    ''' </summary>
    <Guid("C6629CC4-B301-451a-9481-4D7751E9701C")> _
    <ClassInterface(ClassInterfaceType.None)> _
    <ProgId("CustomRasterBuilder.ThumbnailFactory")> _
    <ComVisible(True)> _
    Public Class ThumbnailFactory
        Implements IRasterTypeFactory
#Region "Private Members"
        Private myRasterTypeNames As IStringArray
        ' List of Raster Types that the factory can create.
        Private myUID As UID
        ' UID for the Thumbnail Raster type.
#End Region

#Region "IRasterTypeFactory Members"

        Public Sub New()
            ' The Raster Type name should follow the pattern 
            ' 'Thumbnail ' follwed by the name of the built-in 
            ' Raster Type to attach the Thumbnail Builder to.            
            myRasterTypeNames = New StrArrayClass()
            myRasterTypeNames.Add("Thumbnail Raster Dataset")
            myRasterTypeNames.Add("Thumbnail QuickBird")

            myUID = New UIDClass()
            myUID.Value = "{C6629CC4-B301-451a-9481-4D7751E9701C}"
        End Sub

        ''' <summary>
        ''' The UID for the factory.
        ''' </summary>
        Public ReadOnly Property CLSID() As UID Implements IRasterTypeFactory.CLSID
            Get
                Return myUID
            End Get
        End Property

        ''' <summary>
        ''' The main function which creates the Raster Type object.
        ''' </summary>
        ''' <param name="RasterTypeName">Name of the Raster Type to create.</param>
        ''' <returns></returns>
        Public Function CreateRasterType(ByVal RasterTypeName As String) As IRasterType Implements IRasterTypeFactory.CreateRasterType
            Try
                Select Case RasterTypeName
                    Case "Thumbnail Raster Dataset"
                        If True Then
                            ' Create a Raster Type Name object.
                            Dim theRasterTypeName As IRasterTypeName = New RasterTypeNameClass()
                            ' Assign the name of the built-in Raster Type to the name object.
                            ' The Name field accepts a path to an .art file as well 
                            ' the name for a built-in Raster Type.
                            theRasterTypeName.Name = RasterTypeName.Replace("Thumbnail ", "")
                            ' Use the Open function from the IName interface to get the Raster Type object.
                            Dim theRasterType As IRasterType = DirectCast(DirectCast(theRasterTypeName, IName).Open(), IRasterType)
                            If theRasterType Is Nothing Then
                                Console.WriteLine("Error:Raster Type not found " & theRasterTypeName.Name)
                                Return Nothing
                            End If

                            ' Create a new ThumbnailBuilder object and set it's InnerRasterBuilder property to 
                            ' the RasterBuilder from the RasterType object. Then set the thumbnail builder to 
                            ' be the RasterBuilder for the RasterType object. This inserts the thumbnail builder in 
                            ' between the RasterType and it's RasterBuilder.

                            ' Create the Thumbnail Builder 
                            Dim thumbnailBuilder As IRasterBuilder = New ThumbnailBuilder()
                            '  Set the InnerRasterBuilder property with current Raster Type's Raster Builder
                            DirectCast(thumbnailBuilder, ThumbnailBuilder).InnerRasterBuilder = theRasterType.RasterBuilder
                            ' Set the Raster Builder of theRasterType to the above created thumbnail builder.
                            theRasterType.RasterBuilder = thumbnailBuilder
                            Dim theName As IName = theRasterType.FullName
                            DirectCast(theName, IRasterTypeName).Name = "Thumbnail Raster Dataset"
                            Return theRasterType
                        End If

                    Case "Thumbnail QuickBird"
                        If True Then
                            ' Create a Raster Type Name object.
                            Dim theRasterTypeName As IRasterTypeName = New RasterTypeNameClass()
                            ' Assign the name of the built-in Raster Type to the name object.
                            ' The Name field accepts a path to an .art file as well 
                            ' the name for a built-in Raster Type.
                            theRasterTypeName.Name = RasterTypeName.Replace("Thumbnail ", "")
                            ' Use the Open function from the IName interface to get the Raster Type object.
                            Dim theRasterType As IRasterType = DirectCast(DirectCast(theRasterTypeName, IName).Open(), IRasterType)
                            If theRasterType Is Nothing Then
                                Console.WriteLine("Error:Raster Type not found " & theRasterTypeName.Name)
                                Return Nothing
                            End If

                            ' Create a new TumbnailBuilder object and set it's InnerRasterBuilder property to 
                            ' the RasterBuilder from the RasterType object. Then set the thumbnail builder to 
                            ' be the RasterBuilder for the RasterType object. This inserts the thumbnail builder in 
                            ' between the RasterType and it's RasterBuilder.

                            ' Create the Thumbnail Builder 
                            Dim thumbnailBuilder As IRasterBuilder = New ThumbnailBuilder()
                            '  Set the InnerRasterBuilder property with current Raster Type's Raster Builder
                            DirectCast(thumbnailBuilder, ThumbnailBuilder).InnerRasterBuilder = theRasterType.RasterBuilder
                            ' Set the Raster Builder of theRasterType to the above created thumbnail builder.
                            theRasterType.RasterBuilder = thumbnailBuilder
                            Dim theName As IName = theRasterType.FullName
                            DirectCast(theName, IRasterTypeName).Name = "Thumbnail QuickBird"
                            Return theRasterType
                        End If
                    Case Else
                        Return Nothing
                End Select
            Catch ex As Exception
                Throw New Exception("Error: Failed to create " & RasterTypeName & ": " & ex.Message)
            End Try
        End Function

        ''' <summary>
        ''' Name of the Raster Type factory.
        ''' </summary>
        Public ReadOnly Property Name() As String Implements IRasterTypeFactory.Name
            Get
                Return "Thumbnail Raster Type Factory"
            End Get
        End Property

        ''' <summary>
        ''' List of Raster Types which the factory can create.
        ''' </summary>
        Public ReadOnly Property RasterTypeNames() As IStringArray Implements IRasterTypeFactory.RasterTypeNames
            Get
                Return myRasterTypeNames
            End Get
        End Property
#End Region

#Region "COM Registration Function(s)"
        <ComRegisterFunction()> _
        Private Shared Sub Reg(ByVal regKey As String)
            ESRI.ArcGIS.ADF.CATIDs.RasterTypeFactory.Register(regKey)
        End Sub

        <ComUnregisterFunction()> _
        Private Shared Sub Unreg(ByVal regKey As String)
            ESRI.ArcGIS.ADF.CATIDs.RasterTypeFactory.Unregister(regKey)
        End Sub
#End Region
    End Class

    '''<summary>
    ''' This class implements the interface IThumbnailBuilder, IPersistVariant.
    '''</summary>
    <Guid("CB37C3B0-5080-4bec-9065-AF036CEBA0F9")> _
    <ClassInterface(ClassInterfaceType.None)> _
    <ProgId("CustomRasterBuilder.ThumbnailBuilder")> _
    <ComVisible(True)> _
    Public Class ThumbnailBuilder
        Implements IThumbnailBuilder
        Implements IPersistVariant
        Implements IRasterBuilderInit
#Region "Private Members"
        Public m_innerRasterBuilder As IRasterBuilder
        ' Inner Raster Builder 
        Private myUID As UID
        ' UID for the Thumbnail Builder.
        ''' <summary>
        ''' The following function creates a field called "ThumbNail" and adds it to the existing
        ''' fields in the mosaic dataset catalog as a blob.
        ''' </summary>
        ''' <param name="myFields">List of fields added to the Mosaic Catalog</param>
        Private Shared Sub AddingNewField(ByVal myFields As IFields)
            Dim pField As IField = New FieldClass()
            ' Create a new field object
            Dim objectIDFieldEditor As IFieldEdit = DirectCast(pField, IFieldEdit)
            ' Set the field editor for this field
            objectIDFieldEditor.Name_2 = "ThumbNail"
            ' Set the name of the field
            objectIDFieldEditor.Type_2 = esriFieldType.esriFieldTypeBlob
            ' Set the type of the field as Blob
            Dim fieldsEditor As IFieldsEdit = DirectCast(myFields, IFieldsEdit)
            ' Add the newly created field to list of existing fields
            fieldsEditor.AddField(pField)
            Return
        End Sub

        ''' <summary>
        ''' Each BuilderItem contains a Function Raster Dataset which is used to create the thumbnail.
        ''' The thumbnail is created by Nearest Neighbor resampling of the Function Raster Dataset. 
        ''' The resampled raster is exported as a byte array and saved Into the IMemoryBlobStreamVariant. 
        ''' The blob is then inserted into the Thumbnail field.
        ''' </summary>
        ''' <param name="mybuilderItem">Item containing the Function Dataset to be added to the Mosaic Dataset</param>
        Private Sub Resampling(ByVal mybuilderItem As IBuilderItem)
            Try
                Dim pFuncRasterDataset As IFunctionRasterDataset = mybuilderItem.Dataset
                ' Get the FunctionRasterDataset from mybuilderItem
                Dim pRasterDataset As IRasterDataset
                pRasterDataset = DirectCast(pFuncRasterDataset, IRasterDataset)
                ' Cast the FunctionRasterDataset to Raster Dataset
                Dim thePropSet As IPropertySet = pFuncRasterDataset.Properties
                ' Get the properties of the raster Dataset
                Dim praster As IRaster = pRasterDataset.CreateDefaultRaster()
                ' Create default raster from the above raster dataset
                praster.ResampleMethod = rstResamplingTypes.RSP_NearestNeighbor
                ' The raster is resampled by RSP_NearestNeighbor
                Dim pRasterProps As IRasterProps = DirectCast(praster, IRasterProps)
                ' Raster properties are used to update the height, width of the raster
                pRasterProps.Height = 256
                pRasterProps.Width = 256

                Dim pConverter As IRasterExporter = New RasterExporterClass()
                ' IRasterExporter object is used to convert the raster to byte array.
                Dim pBytesArr As Byte()
                pBytesArr = pConverter.ExportToBytes(praster, "TIFF")
                ' Convert the resampled Raster to a Byte array 
                Dim memBlobStream As IMemoryBlobStream = New MemoryBlobStream()
                ' Create new IMemoryBlobStream
                Dim varBlobStream As IMemoryBlobStreamVariant = DirectCast(memBlobStream, IMemoryBlobStreamVariant)
                ' Assign to IMemoryBlobStreamVariant
                Dim anObject As Object = pBytesArr
                varBlobStream.ImportFromVariant(anObject)
                ' IMemoryBlobStreamVariant object is assigned the byte array
                ' and saved to the property "ThumbNail"
                thePropSet.SetProperty("ThumbNail", memBlobStream)
            Catch ex As Exception
                Dim myExc As New System.Exception("Error: Failed to Re-Sampled the raster.Thumbnails will not be created." & ex.Message, ex)
                Throw myExc
            End Try
            Return

        End Sub
#End Region

#Region "ThumbnailBuilder Members"
        ''' <summary>
        ''' This property gets and sets the raster type of the Thumbnail Builder.
        ''' </summary>
        Public Property InnerRasterBuilder() As IRasterBuilder Implements IThumbnailBuilder.InnerRasterBuilder
            Get
                Return m_innerRasterBuilder
            End Get
            Set(ByVal value As IRasterBuilder)
                m_innerRasterBuilder = value
            End Set
        End Property

        Public Sub New()
            m_innerRasterBuilder = Nothing

            myUID = New UIDClass()
            myUID.Value = "{" & "CB37C3B0-5080-4bec-9065-AF036CEBA0F9" & "}"
        End Sub
#End Region

#Region "IRasterBuilder Members"
        ''' <summary>
        ''' AuxiliaryFieldAlias property gets and sets the Auxiliary fields Alias
        ''' present in the raster builder
        ''' </summary>
        Public Property AuxiliaryFieldAlias() As ESRI.ArcGIS.esriSystem.IPropertySet Implements IRasterBuilder.AuxiliaryFieldAlias
            Get
                Return m_innerRasterBuilder.AuxiliaryFieldAlias
            End Get
            Set(ByVal value As ESRI.ArcGIS.esriSystem.IPropertySet)
                m_innerRasterBuilder.AuxiliaryFieldAlias = value
            End Set
        End Property

        ''' <summary>
        ''' Flag to specify whether the Raster Builder can build items in place.
        ''' </summary>
        Public ReadOnly Property CanBuildInPlace() As Boolean Implements IRasterBuilder.CanBuildInPlace
            Get
                Return m_innerRasterBuilder.CanBuildInPlace
            End Get
        End Property

        ''' <summary>
        ''' For adding Thumbnails, a new field called "Thumbnail" of type blob is created
        ''' and added to the Auxiliary Fields in raster builder.
        ''' </summary>
        Public Property AuxiliaryFields() As IFields Implements IRasterBuilder.AuxiliaryFields
            Get
                Dim pRasterBuilder As ESRI.ArcGIS.DataSourcesRaster.IRasterBuilder = m_innerRasterBuilder
                Dim myFields As IFields = pRasterBuilder.AuxiliaryFields
                ' Existing Fields present in the Raster Builder. 
                Try
                    Dim count As Integer = myFields.FindField("ThumbNail")
                    ' Check if the Field "ThumbNail" already exists.
                    If count = -1 Then
                        AddingNewField(myFields)
                        ' If not add the new field "ThumbNail"
                        ' Assign the updated Fields back to the raster builder.
                        pRasterBuilder.AuxiliaryFields = myFields
                    End If
                Catch ex As Exception
                    Dim myExc As New System.Exception("Failed to add the field ThumbNail." & ex.Message, ex)
                    Throw myExc
                End Try
                ' Return the updated fields.
                Return m_innerRasterBuilder.AuxiliaryFields
            End Get
            Set(ByVal value As IFields)
                m_innerRasterBuilder.AuxiliaryFields = value
            End Set
        End Property

        ''' <summary>
        ''' Prepare the Raster Type for generating Crawler items
        ''' </summary>
        ''' <param name="pCrawler">Crawler to use to generate the crawler items</param>
        Public Sub BeginConstruction(ByVal pCrawler As IDataSourceCrawler) Implements IRasterBuilder.BeginConstruction
            m_innerRasterBuilder.BeginConstruction(pCrawler)
        End Sub

        ''' <summary>
        ''' Call the build function of the inner Raster Type and generate a thumbnail from the Builder Item created.
        ''' </summary>
        ''' <param name="pItemURI">URI of the Item to be built</param>
        ''' <returns></returns>
        Public Function Build(ByVal pItemURI As IItemURI) As IBuilderItem Implements IRasterBuilder.Build
            Dim pbuilderItem As IBuilderItem = m_innerRasterBuilder.Build(pItemURI)
            ' Generate the IBuilderItem
            Resampling(pbuilderItem)
            ' Generate the Thumbnail from the item.
            Return pbuilderItem
        End Function

        ''' <summary>
        ''' Construct a Unique Resource Identifier (URI)
        ''' for each crawler item
        ''' </summary>
        ''' <param name="crawlerItem">Crawled Item from which the URI is generated</param>
        Public Sub ConstructURIs(ByVal crawlerItem As Object) Implements IRasterBuilder.ConstructURIs
            m_innerRasterBuilder.ConstructURIs(crawlerItem)
        End Sub

        ''' <summary>
        ''' Finish Construction of the URI's
        ''' </summary>
        ''' <returns></returns>
        Public Function EndConstruction() As IItemURIArray Implements IRasterBuilder.EndConstruction
            Return m_innerRasterBuilder.EndConstruction()
        End Function

        ''' <summary>
        ''' Generate the next URI.
        ''' </summary>
        ''' <returns>The URI generated.</returns>
        Public Function GetNextURI() As IItemURI Implements IRasterBuilder.GetNextURI
            Return m_innerRasterBuilder.GetNextURI()
        End Function

        ''' <summary>
        ''' Get the recommended data crawler for the Raster Type based on 
        ''' properties provided by the user.
        ''' </summary>
        ''' <param name="pDataSourceProperties">List of properties provided by the user</param>
        ''' <returns></returns>
        Public Function GetRecommendedCrawler(ByVal pDataSourceProperties As IPropertySet) As IDataSourceCrawler Implements IRasterBuilder.GetRecommendedCrawler
            Return m_innerRasterBuilder.GetRecommendedCrawler(pDataSourceProperties)
        End Function

        ''' <summary>
        ''' Check if the item provided is "stale" or not valid
        ''' </summary>
        ''' <param name="pItemURI">URI for the item to be checked</param>
        ''' <returns></returns>
        Public Function IsStale(ByVal pItemURI As IItemURI) As Boolean Implements IRasterBuilder.IsStale
            Return m_innerRasterBuilder.IsStale(pItemURI)
        End Function

        ''' <summary>
        ''' Properties associated with the Raster Type
        ''' </summary>
        Public Property Properties() As IPropertySet Implements IRasterBuilder.Properties
            Get
                Return m_innerRasterBuilder.Properties
            End Get
            Set(ByVal value As IPropertySet)
                m_innerRasterBuilder.Properties = value
            End Set
        End Property


#End Region

#Region "IPersistVariant Members"
        ''' <summary>
        ''' UID for the object implementing the Persist Variant
        ''' </summary>
        Private ReadOnly Property IPersistVariant_ID() As UID Implements IPersistVariant.ID
            Get
                Return myUID
            End Get
        End Property

        ''' <summary>
        ''' Load the object from the stream provided
        ''' </summary>
        ''' <param name="Stream">Stream that represents the serialized Raster Type</param>
        Private Sub IPersistVariant_Load(ByVal Stream As IVariantStream) Implements IPersistVariant.Load
            Dim name As String = DirectCast(Stream.Read(), String)
            'if (innerRasterBuilder is IPersistVariant)
            '    ((IPersistVariant)innerRasterBuilder).Load(Stream);
            m_innerRasterBuilder = DirectCast(Stream.Read(), IRasterBuilder)
            ' Load the innerRasterBuilder from the stream.
        End Sub

        ''' <summary>
        ''' Same the Raster Type to the stream provided
        ''' </summary>
        ''' <param name="Stream">Stream to serialize the Raster Type into</param>
        Private Sub IPersistVariant_Save(ByVal Stream As IVariantStream) Implements IPersistVariant.Save
            Stream.Write("ThmbnailBuilder")
            'if (innerRasterBuilder is IPersistVariant)
            '    ((IPersistVariant)innerRasterBuilder).Save(Stream);
            Stream.Write(m_innerRasterBuilder)
            ' Save the innerRasterBuilder into the stream.
        End Sub
#End Region

#Region "IRasterBuilderInit Members"
        ''' <summary>
        ''' Default spatial reference for the MD.
        ''' </summary>
        Private Property IRasterBuilderInit_DefaultSpatialReference() As ISpatialReference Implements IRasterBuilderInit.DefaultSpatialReference
            Get
                Return DirectCast(m_innerRasterBuilder, IRasterBuilderInit).DefaultSpatialReference
            End Get
            Set(ByVal value As ISpatialReference)
                DirectCast(m_innerRasterBuilder, IRasterBuilderInit).DefaultSpatialReference = value
            End Set
        End Property

        ''' <summary>
        ''' Parent mosaic dataset for the Raster Builder.
        ''' </summary>
        Private Property IRasterBuilderInit_MosaicDataset() As IMosaicDataset Implements IRasterBuilderInit.MosaicDataset
            Get
                Return DirectCast(m_innerRasterBuilder, IRasterBuilderInit).MosaicDataset
            End Get
            Set(ByVal value As IMosaicDataset)
                DirectCast(m_innerRasterBuilder, IRasterBuilderInit).MosaicDataset = value
            End Set
        End Property

        ''' <summary>
        ''' The raster type operation helper object associated with this raster type.
        ''' </summary>
        Private Property IRasterBuilderInit_RasterTypeOperation() As IRasterTypeOperation Implements IRasterBuilderInit.RasterTypeOperation
            Get
                Return DirectCast(m_innerRasterBuilder, IRasterBuilderInit).RasterTypeOperation
            End Get
            Set(ByVal value As IRasterTypeOperation)
                DirectCast(m_innerRasterBuilder, IRasterBuilderInit).RasterTypeOperation = value
            End Set
        End Property

        ''' <summary>
        ''' Tracker for when cancel is pressed.
        ''' </summary>
        Private Property IRasterBuilderInit_TrackCancel() As ITrackCancel Implements IRasterBuilderInit.TrackCancel
            Get
                Return DirectCast(m_innerRasterBuilder, IRasterBuilderInit).TrackCancel
            End Get
            Set(ByVal value As ITrackCancel)
                DirectCast(m_innerRasterBuilder, IRasterBuilderInit).TrackCancel = value
            End Set
        End Property
#End Region
    End Class
End Namespace