public static bool AddWatermarkDataToMD(string MDWorkspaceFolder, string MDName, string watermarkImagePath, double blendPercentage, esriWatermarkLocation watermarklocation, 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; // the blending percentage, rasterFunctionArguments.BlendPercentage = blendPercentage; // and the watermark location. rasterFunctionArguments.WatermarkLocation = watermarklocation; // 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); } }
Bitmap myWatermarkImage; // Watermark Image object. #endregion public WatermarkFunction() { myWatermarkImagePath = ""; myBlendPercentage = 50.00; // Default value for the blending percentage. blendValue = 0.50; // Default value for the blend value. myWatermarkLocation = esriWatermarkLocation.esriWatermarkBottomRight; myName = "WatermarkFunction"; myPixeltype = rstPixelType.PT_UNKNOWN; // Default value for the pixel type. myDescription = "Add a watermark to the request."; myValidFlag = true; myFunctionHelper = new RasterFunctionHelperClass(); myWatermarkImage = null; myUID = new UIDClass(); myUID.Value = "{" + "168721E7-7010-4a36-B886-F644437B164D" + "}"; }
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, a Standard or Advanced license is required. esriLicenseStatus licStatus = aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced); 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 { // Flags to specify the operation to perform bool addToRD = true; // Create Watermark Function Raster Dataset bool addToMD = false; // Add Watermark Function to MD bool writeTemplateToXml = false; // Serialize a template form of the NDVI Custom Funtion to Xml. bool getfromXml = false; // Get a template object back from its serialized xml. #region Specify inputs. // Raster Dataset parameters string workspaceFolder = @"f:\data\RasterDataset\LACounty\"; string rasterDatasetName = "6466_1741c.tif"; // Output parameters for Function Raster Dataset string outputFolder = @"c:\temp\CustomFunction"; string outputName = "WatermarkSample.afr"; // Mosaic Dataset parameters // GDB containing the Mosaic Dataset string mdWorkspaceFolder = @"c:\temp\CustomFunction\SampleGdb.gdb"; // Name of the Mosaic Dataset string mdName = "SampleMD"; // Watermark Parameters string watermarkImagePath = @"e:\Dev\SDK\Raster\NET\Samples\CustomRasterFunction\CSharp\TestWatermarkFunction\Sample.png"; double blendPercentage = 80.00; esriWatermarkLocation wmLocation = esriWatermarkLocation.esriWatermarkCenter; // Xml file path to save to or read from xml. string xmlFilePath = @"c:\temp\CustomFunction\Xml\Watermark.RFT.xml"; #endregion if (addToRD) { // 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, wmLocation); // Cleanup workspaceFactory = null; rasterWorkspace = null; rasterDataset = null; } if (addToMD) { AddWatermarkDataToMD(mdWorkspaceFolder, mdName, watermarkImagePath, blendPercentage, wmLocation, true); } if (writeTemplateToXml && xmlFilePath != "") { // Create a template with the Watermark Function. IRasterFunctionTemplate watermarkFunctionTemplate = CreateWatermarkTemplate(watermarkImagePath, blendPercentage, wmLocation); // Serialize the template to an xml file. bool status = WriteToXml(watermarkFunctionTemplate, xmlFilePath); } if (getfromXml && xmlFilePath != "") { // Create a RasterFunctionTemplate object from the serialized xml. object serializedObj = ReadFromXml(xmlFilePath); if (serializedObj is IRasterFunctionTemplate) { Console.WriteLine("Success."); } else { Console.WriteLine("Failed."); } } 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 IRasterFunctionTemplate CreateWatermarkTemplate(string watermarkImagePath, double blendPercentage, esriWatermarkLocation watermarklocation) { #region Setup Raster Function Vars IRasterFunctionVariable watermarkRasterRFV = new RasterFunctionVariableClass(); watermarkRasterRFV.Name = "Raster"; watermarkRasterRFV.IsDataset = true; IRasterFunctionVariable watermarkImagePathRFV = new RasterFunctionVariableClass(); watermarkImagePathRFV.Name = "WatermarkImagePath"; watermarkImagePathRFV.Value = watermarkImagePath; watermarkImagePathRFV.IsDataset = false; IRasterFunctionVariable watermarkBlendPercRFV = new RasterFunctionVariableClass(); watermarkBlendPercRFV.Name = "BlendPercentage"; watermarkBlendPercRFV.Value = blendPercentage; IRasterFunctionVariable watermarkLocationRFV = new RasterFunctionVariableClass(); watermarkLocationRFV.Name = "Watermarklocation"; watermarkLocationRFV.Value = watermarklocation; #endregion #region Setup Raster Function Template // Create the Watermark Function Arguments object IRasterFunctionArguments rasterFunctionArguments = new CustomFunction.WatermarkFunctionArguments(); // Set the WatermarkImagePath rasterFunctionArguments.PutValue("WatermarkImagePath", watermarkImagePathRFV); // the blending percentage, rasterFunctionArguments.PutValue("BlendPercentage", watermarkBlendPercRFV); // and the watermark location. rasterFunctionArguments.PutValue("WatermarkLocation", watermarkLocationRFV); // Set the Raster Dataset as the input raster rasterFunctionArguments.PutValue("Raster", watermarkRasterRFV); IRasterFunction watermarkFunction = new CustomFunction.WatermarkFunction(); IRasterFunctionTemplate watermarkFunctionTemplate = new RasterFunctionTemplateClass(); watermarkFunctionTemplate.Function = watermarkFunction; watermarkFunctionTemplate.Arguments = rasterFunctionArguments; #endregion return(watermarkFunctionTemplate); }
public static bool AddWatermarkToRD(IRasterDataset RasterDataset, string OutputFolder, string OutputName, string watermarkImagePath, double blendPercentage, esriWatermarkLocation watermarklocation) { 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; // the blending percentage, rasterFunctionArguments.BlendPercentage = blendPercentage; // and the watermark location. rasterFunctionArguments.WatermarkLocation = watermarklocation; // 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 = System.IO.Path.Combine(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 IRasterFunctionTemplate CreateWatermarkTemplate(string watermarkImagePath, double blendPercentage, esriWatermarkLocation watermarklocation) { #region Setup Raster Function Vars IRasterFunctionVariable watermarkRasterRFV = new RasterFunctionVariableClass(); watermarkRasterRFV.Name = "Raster"; watermarkRasterRFV.IsDataset = true; IRasterFunctionVariable watermarkImagePathRFV = new RasterFunctionVariableClass(); watermarkImagePathRFV.Name = "WatermarkImagePath"; watermarkImagePathRFV.Value = watermarkImagePath; watermarkImagePathRFV.IsDataset = false; IRasterFunctionVariable watermarkBlendPercRFV = new RasterFunctionVariableClass(); watermarkBlendPercRFV.Name = "BlendPercentage"; watermarkBlendPercRFV.Value = blendPercentage; IRasterFunctionVariable watermarkLocationRFV = new RasterFunctionVariableClass(); watermarkLocationRFV.Name = "Watermarklocation"; watermarkLocationRFV.Value = watermarklocation; #endregion #region Setup Raster Function Template // Create the Watermark Function Arguments object IRasterFunctionArguments rasterFunctionArguments = new CustomFunction.WatermarkFunctionArguments(); // Set the WatermarkImagePath rasterFunctionArguments.PutValue("WatermarkImagePath", watermarkImagePathRFV); // the blending percentage, rasterFunctionArguments.PutValue("BlendPercentage", watermarkBlendPercRFV); // and the watermark location. rasterFunctionArguments.PutValue("WatermarkLocation", watermarkLocationRFV); // Set the Raster Dataset as the input raster rasterFunctionArguments.PutValue("Raster", watermarkRasterRFV); IRasterFunction watermarkFunction = new CustomFunction.WatermarkFunction(); IRasterFunctionTemplate watermarkFunctionTemplate = new RasterFunctionTemplateClass(); watermarkFunctionTemplate.Function = watermarkFunction; watermarkFunctionTemplate.Arguments = rasterFunctionArguments; #endregion return watermarkFunctionTemplate; }
public static bool AddWatermarkDataToMD(string MDWorkspaceFolder, string MDName, string watermarkImagePath, double blendPercentage, esriWatermarkLocation watermarklocation, 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; // the blending percentage, rasterFunctionArguments.BlendPercentage = blendPercentage; // and the watermark location. rasterFunctionArguments.WatermarkLocation = watermarklocation; // 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; } }
public static bool AddWatermarkToRD(IRasterDataset RasterDataset, string OutputFolder, string OutputName, string watermarkImagePath, double blendPercentage, esriWatermarkLocation watermarklocation) { 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; // the blending percentage, rasterFunctionArguments.BlendPercentage = blendPercentage; // and the watermark location. rasterFunctionArguments.WatermarkLocation = watermarklocation; // 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 = System.IO.Path.Combine(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; } }
/// <summary> /// Initialize the Raster function using the argument object. This is one of the two /// main functions to implement for a custom Raster function. The raster object is /// dereferenced if required and given to the RasterFuntionHelper object to bind. /// </summary> /// <param name="pArguments">Arguments object used for initialization</param> public void Bind(object pArguments) { try { // Check if the Arguments object is of the correct type. IWatermarkFunctionArguments watermarkFuncArgs = null; if (pArguments is IWatermarkFunctionArguments) { watermarkFuncArgs = (IWatermarkFunctionArguments)pArguments; myBlendPercentage = watermarkFuncArgs.BlendPercentage; myWatermarkImagePath = watermarkFuncArgs.WatermarkImagePath; myWatermarkLocation = watermarkFuncArgs.WatermarkLocation; object inputRaster = watermarkFuncArgs.Raster; if (watermarkFuncArgs.Raster is IRasterFunctionVariable) { IRasterFunctionVariable rasterFunctionVariable = (IRasterFunctionVariable)watermarkFuncArgs.Raster; inputRaster = rasterFunctionVariable.Value; } // Call the Bind method of the Raster Function Helper object. myFunctionHelper.Bind(inputRaster); } else { // Throw an error if incorrect arguments object is passed. throw new System.Exception( "Incorrect arguments object. Expected: IWatermarkFunctionArguments"); } // Get the raster info and Pixel Type from the RasterFunctionHelper object. myRasterInfo = myFunctionHelper.RasterInfo; myPixeltype = myRasterInfo.PixelType; // Convert blending percentage to blending value. if (myBlendPercentage >= 0.0 && myBlendPercentage <= 100.0) blendValue = myBlendPercentage / 100.0; else /// A value of 50% is used as default. blendValue = 0.50; if (myWatermarkImagePath != "") { // Load the watermark image from the path provided myWatermarkImage = new Bitmap(myWatermarkImagePath); // and check the pixel type of the loaded image to see if its compatible. if (myWatermarkImage.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb && myWatermarkImage.PixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb) { // Throw error if the image is not compatible. throw new System.Exception( "Invalid watermark image. Please provide one with 8 bits per band in ARGB or RGB format."); } // Cleanup myWatermarkImage.Dispose(); myWatermarkImage = null; } } catch (Exception exc) { #region Cleanup if (myWatermarkImage != null) myWatermarkImage.Dispose(); myWatermarkImage = null; #endregion System.Exception myExc = new System.Exception( "Exception caught in Bind method of Watermark Function. " + exc.Message, exc); throw myExc; } }