About the Create a custom raster function Sample
[C#]
TestWatermarkFunction.cs
using System; using ESRI.ArcGIS; using ESRI.ArcGIS.DataSourcesGDB; using ESRI.ArcGIS.DataSourcesRaster; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Geometry; using System.IO; using CustomFunction; namespace SampleTest { /// <summary> /// TODO: Enter a description of the test. /// </summary> public class Program { [STAThread] public static void Main(string[] args) { #region Initialize License ESRI.ArcGIS.esriSystem.AoInitialize aoInit; try { Console.WriteLine("Obtaining license"); ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop); aoInit = new AoInitializeClass(); // To make changes to a Mosaic Dataset, an ArcEditor of ArcView license is required. esriLicenseStatus licStatus = aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo); Console.WriteLine("Ready with license."); } catch (Exception exc) { // If it fails at this point, shutdown the test and ignore any subsequent errors. Console.WriteLine(exc.Message); return; } #endregion try { #region Specify input directory and dataset name. // Raster Dataset parameters string workspaceFolder = @"c:\temp"; string rasterDatasetName = "Dubai_ov.tif"; // Output parameters for Function Raster Dataset // Note: Do not put a trailing \ string outputFolder = @"c:\temp"; string outputName = "WatermarkSample.afr"; // Mosaic dataset parameters // GDB containing the Mosaic Dataset string mdWorkspaceFolder = @"c:\temp\testGdb.gdb"; // Name of the mosaic dataset string mdName = "testMD"; #endregion // Flags to specify the operation to perform bool addToRD = true; bool addToMD = false; // Watermark Parameters string watermarkImagePath = @"C:\Temp\Sample.png"; double blendPercentage = 50.00; if (addToRD) // Create Watermark Function Dataset { // Open the Raster Dataset Type factoryType = Type.GetTypeFromProgID("esriDataSourcesRaster.RasterWorkspaceFactory"); IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType); IRasterWorkspace rasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(workspaceFolder, 0); IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(rasterDatasetName); AddWatermarkToRD(rasterDataset, outputFolder, outputName, watermarkImagePath, blendPercentage); // Cleanup workspaceFactory = null; rasterWorkspace = null; rasterDataset = null; } if (addToMD) // Add Watermark Function to MD AddWatermarkDataToMD(mdWorkspaceFolder, mdName, watermarkImagePath, blendPercentage, true); Console.WriteLine("Press any key..."); Console.ReadKey(); aoInit.Shutdown(); } catch (Exception exc) { Console.WriteLine("Exception Caught in Main: " + exc.Message); Console.WriteLine("Failed."); Console.WriteLine("Press any key..."); Console.ReadKey(); aoInit.Shutdown(); } } public static bool AddWatermarkToRD(IRasterDataset RasterDataset, string OutputFolder, string OutputName, string WatermarkImagePath, double BlendPercentage) { try { // Create Watermark Function IRasterFunction rasterFunction = new CustomFunction.WatermarkFunction(); // Create the Watermark Function Arguments object IWatermarkFunctionArguments rasterFunctionArguments = new WatermarkFunctionArguments(); // Set the WatermarkImagePath rasterFunctionArguments.WatermarkImagePath = WatermarkImagePath; // and the transparency. rasterFunctionArguments.BlendPercentage = BlendPercentage; // Set the Raster Dataset as the input raster rasterFunctionArguments.Raster = RasterDataset; // Create Function Dataset IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset(); // Create a Function Raster Dataset Name object IFunctionRasterDatasetName functionRasterDatasetName = (IFunctionRasterDatasetName)new FunctionRasterDatasetName(); // Set the path for the output Function Raster Dataset functionRasterDatasetName.FullName = OutputFolder + "\\" + OutputName; functionRasterDataset.FullName = (IName)functionRasterDatasetName; // Initialize the Function Raster Dataset with the function and // its arguments object functionRasterDataset.Init(rasterFunction, rasterFunctionArguments); // Save as Function Raster Dataset as an .afr file ITemporaryDataset myTempDset = (ITemporaryDataset)functionRasterDataset; myTempDset.MakePermanent(); Console.WriteLine("Generated " + OutputName + "."); Console.WriteLine("Success."); return true; } catch (Exception exc) { Console.WriteLine("Exception Caught while adding watermark to Raster Dataset: " + exc.Message); Console.WriteLine("Failed."); return false; } } public static bool AddWatermarkDataToMD(string MDWorkspaceFolder, string MDName, string WatermarkImagePath, double BlendPercentage, bool clearFunctions) { try { // Open MD Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory"); IWorkspaceFactory mdWorkspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType); IWorkspace mdWorkspace = mdWorkspaceFactory.OpenFromFile(MDWorkspaceFolder, 0); IRasterWorkspaceEx workspaceEx = (IRasterWorkspaceEx)(mdWorkspace); IMosaicDataset mosaicDataset = (IMosaicDataset)workspaceEx.OpenRasterDataset( MDName); if (clearFunctions) // Clear functions already added to MD. mosaicDataset.ClearFunction(); // Create Watermark Function IRasterFunction rasterFunction = new CustomFunction.WatermarkFunction(); // Create the Watermark Function Arguments object IWatermarkFunctionArguments rasterFunctionArguments = new WatermarkFunctionArguments(); // Set the WatermarkImagePath rasterFunctionArguments.WatermarkImagePath = WatermarkImagePath; // and the blending percentage. rasterFunctionArguments.BlendPercentage = BlendPercentage; // Add function to MD. // This function takes the name of the property corresponding to the Raster // property of the Arguments object (in this case is it called Raster itself: // rasterFunctionArguments.Raster) as its third argument. mosaicDataset.ApplyFunction(rasterFunction, rasterFunctionArguments, "Raster"); Console.WriteLine("Added Watermark to MD: " + MDName + "."); Console.WriteLine("Success."); return true; } catch (Exception exc) { Console.WriteLine("Exception Caught while adding watermark to MD: " + exc.Message); Console.WriteLine("Failed."); return false; } } } }
[Visual Basic .NET]
TestWatermarkFunction.vb
Imports System Imports ESRI.ArcGIS Imports ESRI.ArcGIS.DataSourcesGDB Imports ESRI.ArcGIS.DataSourcesRaster Imports ESRI.ArcGIS.esriSystem Imports ESRI.ArcGIS.Geodatabase Imports ESRI.ArcGIS.Geometry Imports System.IO Imports WatermarkFunction.CustomFunction Imports WatermarkFunctionUI.CustomFunction Namespace SampleTest ''' <summary> ''' TODO: Enter a description of the test. ''' </summary> Public Class Program <STAThread()> _ Public Shared Sub Main(ByVal args As String()) '#Region "Initialize License" Dim aoInit As ESRI.ArcGIS.esriSystem.AoInitialize Try Console.WriteLine("Obtaining license") ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop) aoInit = New AoInitializeClass() ' To make changes to a Mosaic Dataset, an ArcEditor of ArcView license is required. Dim licStatus As esriLicenseStatus = aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo) Console.WriteLine("Ready with license.") Catch exc As Exception ' If it fails at this point, shutdown the test and ignore any subsequent errors. Console.WriteLine(exc.Message) Exit Sub End Try '#End Region Try '#Region "Specify input directory and dataset name." ' Raster Dataset parameters Dim workspaceFolder As String = "c:\temp" Dim rasterDatasetName As String = "Dubai_ov.tif" ' Output parameters for Function Raster Dataset ' Note: Do not put a trailing \ Dim outputFolder As String = "c:\temp" Dim outputName As String = "WatermarkSampleVB.afr" ' Mosaic Dataset parameters ' GDB containing the Mosaic Dataset Dim mdWorkspaceFolder As String = "c:\temp\testGdb.gdb" ' Name of the Mosaic Dataset Dim mdName As String = "testMD" '#End Region ' Flags to specify the operation to perform Dim addToRD As Boolean = True Dim addToMD As Boolean = False ' Watermark Parameters Dim watermarkImagePath As String = "C:\Temp\Sample.png" Dim blendPercentage As Double = 50.0R If addToRD Then ' Create Watermark Function Dataset ' Open the Raster Dataset Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesRaster.RasterWorkspaceFactory") Dim workspaceFactory As IWorkspaceFactory = DirectCast(Activator.CreateInstance(factoryType), IWorkspaceFactory) Dim rasterWorkspace As IRasterWorkspace = DirectCast(workspaceFactory.OpenFromFile(workspaceFolder, 0), IRasterWorkspace) Dim rasterDataset As IRasterDataset = rasterWorkspace.OpenRasterDataset(rasterDatasetName) AddWatermarkToRD(rasterDataset, outputFolder, outputName, watermarkImagePath, blendPercentage) ' Cleanup workspaceFactory = Nothing rasterWorkspace = Nothing rasterDataset = Nothing End If If addToMD Then ' Add Watermark Function to MD AddWatermarkDataToMD(mdWorkspaceFolder, mdName, watermarkImagePath, blendPercentage, True) End If Console.WriteLine("Press any key...") Console.ReadKey() aoInit.Shutdown() Catch exc As Exception Console.WriteLine("Exception Caught in Main: " & exc.Message) Console.WriteLine("Failed.") Console.WriteLine("Press any key...") Console.ReadKey() aoInit.Shutdown() End Try End Sub Public Shared Function AddWatermarkToRD(ByVal RasterDataset As IRasterDataset, ByVal OutputFolder As String, ByVal OutputName As String, ByVal WatermarkImagePath As String, ByVal BlendPercentage As Double) As Boolean Try ' Create Watermark Function Dim rasterFunction As IRasterFunction = New WatermarkFunction.CustomFunction.WatermarkFunction() ' Create the Watermark Function Arguments object Dim rasterFunctionArguments As IWatermarkFunctionArguments = New WatermarkFunctionArguments() ' Set the WatermarkImagePath rasterFunctionArguments.WatermarkImagePath = WatermarkImagePath ' and the transparency. rasterFunctionArguments.BlendPercentage = BlendPercentage ' Set the Raster Dataset as the input raster rasterFunctionArguments.Raster = RasterDataset ' Create Function Dataset Dim functionRasterDataset As IFunctionRasterDataset = New FunctionRasterDataset() ' Create a Function Raster Dataset Name object Dim functionRasterDatasetName As IFunctionRasterDatasetName = DirectCast(New FunctionRasterDatasetName(), IFunctionRasterDatasetName) ' Set the path for the output Function Raster Dataset functionRasterDatasetName.FullName = (OutputFolder & "\") + OutputName functionRasterDataset.FullName = DirectCast(functionRasterDatasetName, IName) ' Initialize the Function Raster Dataset with the function and ' its arguments object functionRasterDataset.Init(rasterFunction, rasterFunctionArguments) ' Save as Function Raster Dataset as an .afr file Dim myTempDset As ITemporaryDataset = DirectCast(functionRasterDataset, ITemporaryDataset) myTempDset.MakePermanent() Console.WriteLine("Generated " & OutputName & ".") Console.WriteLine("Success.") Return True Catch exc As Exception Console.WriteLine("Exception Caught while adding watermark to Raster Dataset: " & exc.Message) Console.WriteLine("Failed.") Return False End Try End Function Public Shared Function AddWatermarkDataToMD(ByVal MDWorkspaceFolder As String, ByVal MDName As String, ByVal WatermarkImagePath As String, ByVal BlendPercentage As Double, ByVal clearFunctions As Boolean) As Boolean Try ' Open MD Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory") Dim mdWorkspaceFactory As IWorkspaceFactory = DirectCast(Activator.CreateInstance(factoryType), IWorkspaceFactory) Dim mdWorkspace As IWorkspace = mdWorkspaceFactory.OpenFromFile(MDWorkspaceFolder, 0) Dim workspaceEx As IRasterWorkspaceEx = DirectCast((mdWorkspace), IRasterWorkspaceEx) Dim mosaicDataset As IMosaicDataset = DirectCast(workspaceEx.OpenRasterDataset(MDName), IMosaicDataset) If clearFunctions Then ' Clear functions already added to MD. mosaicDataset.ClearFunction() End If ' Create Watermark Function Dim rasterFunction As IRasterFunction = New WatermarkFunction.CustomFunction.WatermarkFunction() ' Create the Watermark Function Arguments object Dim rasterFunctionArguments As IWatermarkFunctionArguments = New WatermarkFunctionArguments() ' Set the WatermarkImagePath rasterFunctionArguments.WatermarkImagePath = WatermarkImagePath ' and the blending percentage. rasterFunctionArguments.BlendPercentage = BlendPercentage ' Add function to MD. ' This function takes the name of the property corresponding to the Raster ' property of the Arguments object (in this case is it called Raster itself: ' rasterFunctionArguments.Raster) as its third argument. mosaicDataset.ApplyFunction(rasterFunction, rasterFunctionArguments, "Raster") Console.WriteLine("Added Watermark to MD: " & MDName & ".") Console.WriteLine("Success.") Return True Catch exc As Exception Console.WriteLine("Exception Caught while adding watermark to MD: " & exc.Message) Console.WriteLine("Failed.") Return False End Try End Function End Class End Namespace