IAIS Client Sample.cs
//-----------------------------------------------------------------------------
//
// Copyright (c) 2006 Zanja Technologies, Inc
//
// TRADE SECRETS: PROPRIETARY AND CONFIDENTIAL
// Unpublished material - all rights reserved under the
// Copyright Laws of the United States.
//
// Distributed under license by ESRI, Inc.
// For additional information, contact:
// Environmental Systems Research Institute, Inc.
// 380 New York Street
// Redlands, California, USA 92373
//-----------------------------------------------------------------------------/*
This sample code in C# creates and initializes the Client connection object, loads an image service, which is published on a server, and requests an image given the extents and the number of rows and columns.
For this sample to work, open a C# standard EXE project. Add in references:
• | Image Server Client 1.0 Type Library [ISClient.Dll] |
using System;
using System.Collections.Generic;
using System.Text;
namespace sampleCSClient
{
class Program
{
[STAThread]
static void Main(string[] args)
{
try
{
/// Insufficient number of arguments
if (args.Length < 9)
{
Console.WriteLine("Sample Client Application");
Console.WriteLine("\nUsage: ");
Console.WriteLine("\n\t Output Type <Image Service name> <XMin> <YMin> <XMax> <YMax> <Cols> <Rows> <Output File>");
Console.WriteLine("\n Output Type can take value 'buffer' or 'file' ");
Console.WriteLine("\nPress any key to exit...");
return;
}
/// Reading the command line arguments
/// outputType accepted as command line argument
string outputType = args[0];
/// image service name accepted as command line argument
string imageServiceName = args[1];
/// Area of Interest accepted as command line argument
double xMin, yMin, xMax, yMax;
int nCols, nRows;
xMin = Convert.ToDouble(args[2]);
yMin = Convert.ToDouble(args[3]);
xMax = Convert.ToDouble(args[4]);
yMax = Convert.ToDouble(args[5]);
nCols = Convert.ToInt32(args[6]);
nRows = Convert.ToInt32(args[7]);
/// The output file where the image is to be exported
string outputFile = args[8];
bool Result;
///Creating and initializing the Client connection object
ESRI.ImageServer.ISClient.IAISClientObject
clientObject = new ESRI.ImageServer.ISClient.AISClientObjectClass();
if (!clientObject.Init(""))
{
Console.WriteLine("Error: " + clientObject.Status);
clientObject.Close();
return;
}
///Opening the image service specified as command line arguments
ESRI.ImageServer.ISClient.IAISImageService
imageServiceObject = clientObject.OpenImageService(imageServiceName, "");
if (imageServiceObject == null)
{
Console.WriteLine("Error: " + clientObject.Status);
clientObject.Close();
return;
}
if(string.Compare(outputType, "file")==0)
{
if (string.Compare(outputFile, "") == 0)
{
Console.WriteLine("\n Please specify the output file");
return;
}
///Creating and initializing the Image writer object
ESRI.ImageServer.ISClient.IAISImageWriter
imageWriter = new ESRI.ImageServer.ISClient.AISImageWriterClass();
Result = imageWriter.Init(imageServiceObject);
if (!Result)
{
Console.WriteLine("Failed to initialize the Image Writer object");
}
///Forming the Export properties XML
string exportPropertiesXML = MakeExportPropsXML(xMin, xMax, yMin, yMax, nRows, nCols, outputFile);
///Saving the Image to file
Result = imageWriter.Save(exportPropertiesXML);
if (!Result)
{
Console.WriteLine("Failed to save image to file");
return;
}
else
Console.WriteLine("Image is saved to file");
}
else
{
if(string.Compare(outputType, "buffer")==0)
{
///Extracting the desired Area Of Interest
Result = imageServiceObject.ExtractAoi(xMin, yMin, xMax, yMax, nCols, nRows);
if (!Result)
{
Console.WriteLine("Failed to extract the required AOI");
}
///Creating the buffer
long size = imageServiceObject.GetBufferSize();
byte[] buffer = new byte[size];
System.Runtime.InteropServices.GCHandle GC = System.Runtime.InteropServices.GCHandle.Alloc(buffer, System.Runtime.InteropServices.GCHandleType.Pinned);
if (!GC.IsAllocated)
{
Console.WriteLine("Cannot Create Buffer in memory.");
}
IntPtr bufferPtr = GC.AddrOfPinnedObject();
/// Getting Image data into the buffer
Result = imageServiceObject.GetImageInBuffer(bufferPtr.ToInt32());
GC.Free();
if (!Result)
{
Console.WriteLine("Failed to get image in the buffer");
return;
}
else
Console.WriteLine("Image is saved in buffer");
}
else
{
Console.WriteLine( "\n Please specify valid output type");
return;
}
}
///Closing the Client connection object and the Image service object
imageServiceObject.Close();
clientObject.Close();
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
}
}
/// <summary>
/// Function that forms and returns the Export Properties XML
/// </summary>
/// <param name="xMin"></param>
/// <param name="xMax"></param>
/// <param name="yMin"></param>
/// <param name="yMax"></param>
/// <param name="nRows"></param>
/// <param name="nCols"></param>
/// <param name="outputFile"></param>
/// <returns></returns>
///
private static string MakeExportPropsXML(double xMin, double xMax, double yMin, double yMax, int nRows, int nCols, string outputFile)
{
string exportPropertiesXML;
exportPropertiesXML = "<ImageServer><Client><GetImage>";
exportPropertiesXML += "<Format>TIFF</Format>";
exportPropertiesXML += "<FileName>";
exportPropertiesXML += string.Format(@"{0}", outputFile);
exportPropertiesXML += "</FileName><OutputType>File</OutputType>";
exportPropertiesXML += "<CompressionMethod>jpeg</CompressionMethod>";
exportPropertiesXML += "<CompressionQuality>75</CompressionQuality>";
exportPropertiesXML += "<TileSize>512</TileSize>";
/// Set the write geo keys parameter to true
/// so as to enable writing out of geo-tiffs
exportPropertiesXML += "<WriteGeoKeys>True</WriteGeoKeys>";
/// Set the extents
exportPropertiesXML += string.Format("<Left>{0}</Left><Bottom>{1}</Bottom>", xMin, yMin);
exportPropertiesXML += string.Format("<Right>{0}</Right><Top>{1}</Top>", xMax, yMax);
exportPropertiesXML += string.Format("<Cols>{0}</Cols><Rows>{1}</Rows>", nCols, nRows);
exportPropertiesXML += "</GetImage></Client></ImageServer>";
return exportPropertiesXML;
}
}
}