How to create an image server layer


Summary
The following code example shows how to create an image server layer from a uniform resource locator (URL), clip a portion of the image, and save it to a file.

To save out a raster from an image service, follow these steps:
  1. Initialize image server layer;
  2. Get the raster from the image server layer and set the desired export extent;
  3. Save the clipped raster to geodatabase or other formats.
[C#]
public static void AccessImageServerLayer()
{
    //Create an image server layer by passing a URL.
    IImageServerLayer imageserverlayer = new ImageServerLayerClass();
    string URL = "http://server/arcgis/services/serviceName/imageserver";
    imageserverlayer.Initialize(URL);

    //For services that require https/authentication, use the following
    //IImageServerLayer imageserverlayer = CreateSecuredISLayer(@"http://server:6080/arcgis/services", "serviceName");

    //Get the raster from the image server layer.
    IRaster raster = imageserverlayer.Raster;

    //The raster from an image server is normally large; 
    //define the size of the raster.
    IRasterProps rasterProps = (IRasterProps)raster;
    IEnvelope clipEnvelope = new EnvelopeClass();
    clipEnvelope.PutCoords(779000, 9628000, 786000, 9634000);
    rasterProps.Extent = clipEnvelope;
    rasterProps.Width = 256;
    rasterProps.Height = 256;

    //Save the clipped raster to the file geodatabase.
    ISaveAs saveas = (ISaveAs)raster;
    Type factoryType = Type.GetTypeFromProgID(
        "esriDataSourcesGDB.FileGDBWorkspaceFactory");
    IWorkspaceFactory workspaceFact = (IWorkspaceFactory)Activator.CreateInstance
        (factoryType);
    IWorkspace workspace = workspaceFact.OpenFromFile(@"c:\temp\fgdb.gdb", 0);
    saveas.SaveAs("clipfromimageserverlayer", workspace, "gdb");
}

private static IImageServerLayer CreateSecuredISLayer(string agsUrl, string
    serviceName)
{
    IName soname = GetImageServerName(agsUrl, serviceName);
    IImageServerLayer isLayer = new ImageServerLayerClass();
    IDataLayer data_islayer = (IDataLayer)isLayer;
    data_islayer.DataSourceName = soname;
    return isLayer;
}

private static IName GetImageServerName(string hostOrUrl, string serviceName)
{
    IPropertySet propSet = new PropertySetClass();
    propSet.SetProperty("url", hostOrUrl);
    propSet.SetProperty("ANONYMOUS", false);
    //don't hardcode user/password
    //propertySet.SetProperty("user", userName);
    //propertySet.SetProperty("password", password);
    IAGSServerConnectionName agsServerConnectName = new AGSServerConnectionNameClass
        ();
    agsServerConnectName.ConnectionProperties = propSet;
    IAGSServerObjectName agsSOName = new AGSServerObjectNameClass();
    agsSOName.AGSServerConnectionName = agsServerConnectName;
    agsSOName.Name = serviceName;
    agsSOName.Type = "ImageServer";
    return (IName)agsSOName;
}
[VB.NET]
Public Shared Sub AccessImageServerLayer()
'Create an image server layer by passing a URL.
Dim isLayer As IImageServerLayer = New ImageServerLayerClass()
Dim URL As String = "http://server/arcgis/services/serviceName/imageserver"
isLayer.Initialize(URL)

'For services that require https/authentication, use the following
'Dim isLayer As IImageServerLayer = CreateSecuredISLayer("http://server:6080/arcgis/services", "serviceName")

'Get the raster from the image server layer.
Dim raster As IRaster = isLayer.Raster

'The raster from an image server is normally large;
'define the size of the raster.
Dim rasterProps As IRasterProps = DirectCast(raster, IRasterProps)
Dim clipEnvelope As IEnvelope = New EnvelopeClass()
clipEnvelope.PutCoords(779000, 9628000, 786000, 9634000)
rasterProps.Extent = clipEnvelope
rasterProps.Width = 256
rasterProps.Height = 256

'Save the clipped raster to the file geodatabase.
Dim saveas As ISaveAs = DirectCast(raster, ISaveAs)
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")
Dim workspaceFact As IWorkspaceFactory = DirectCast(Activator.CreateInstance(factoryType), IWorkspaceFactory)
Dim workspace As IWorkspace = workspaceFact.OpenFromFile("c:\temp\fgdb.gdb", 0)
saveas.SaveAs("clipfromimageserverlayer", workspace, "gdb")
End Sub

Private Shared Function CreateSecuredISLayer(agsUrl As String, serviceName As String) As IImageServerLayer
Dim soname As IName = GetImageServerName(agsUrl, serviceName)
Dim isLayer As IImageServerLayer = New ImageServerLayerClass()
Dim data_islayer As IDataLayer = DirectCast(isLayer, IDataLayer)
data_islayer.DataSourceName = soname
Return isLayer
End Function

Private Shared Function GetImageServerName(hostOrUrl As String, serviceName As String) As IName
Dim propSet As IPropertySet = New PropertySetClass()
propSet.SetProperty("url", hostOrUrl)
propSet.SetProperty("ANONYMOUS", False)
'don't hardcode user/password
'propertySet.SetProperty("user", userName);
'propertySet.SetProperty("password", password);
Dim agsServerConnectName As IAGSServerConnectionName = New AGSServerConnectionNameClass()
agsServerConnectName.ConnectionProperties = propSet
Dim agsSOName As IAGSServerObjectName = New AGSServerObjectNameClass()
agsSOName.AGSServerConnectionName = agsServerConnectName
agsSOName.Name = serviceName
agsSOName.Type = "ImageServer"
Return DirectCast(agsSOName, IName)
End Function






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
Engine Developer Kit Engine