How to download images from an image service


Summary
This topic shows how to download and use unprocessed images (that is, images in their original formats) from an image service, and how to clip and download source rasters to tagged image file format (TIFF), National Imagery Transmission Format (NITF), or ERDAS IMAGINE image file format.


Downloading native format source rasters

The following code example shows how to download native format source rasters from an image service:
[C#]
public static void DownloadRasters(IImageServer imageServer)
{
    //Cast.
    IImageServer3 imageServer3 = (IImageServer3)imageServer;

    //Create a fidset that contains the OBJECTID of the target rasters.
    IFIDSet fidSet = new FIDSetClass();
    fidSet.Add(1);
    fidSet.Add(2);

    //Define the Web client.
    System.Net.WebClient webClient = new System.Net.WebClient();

    //Download the source rasters.
    IImageServerDownloadResults isDownloadResults = imageServer3.Download(fidSet,
        null, null);
    IImageServerDownloadResult isDownloadResult;
    string url, fileName;
    for (int i = 0; i < isDownloadResults.Count; i++)
    {
        isDownloadResult = isDownloadResults.get_Element(i);
        url = imageServer3.GetFile(isDownloadResult);
        fileName = "c:\\temp\\" + url.Substring(url.LastIndexOf("/") + 1);
        webClient.DownloadFile(url, fileName);
    }
}
[VB.NET]
Public Shared Sub DownloadRasters(ByVal imageServer As IImageServer)
'Cast.
Dim imageServer3 As IImageServer3 = CType(imageServer, IImageServer3)

'Create a fidset that contains the OBJECTID of the target rasters.
Dim fidSet As IFIDSet = New FIDSetClass()
fidSet.Add(1)
fidSet.Add(2)

'Define the Web client.
Dim webClient As New System.Net.WebClient()

'Download the source rasters.
Dim isDownloadResults As IImageServerDownloadResults = imageServer3.Download(fidSet, Nothing, Nothing)
Dim isDownloadResult As IImageServerDownloadResult
Dim url As String, fileName As String
For i As Integer = 0 To isDownloadResults.Count - 1
    isDownloadResult = isDownloadResults.Element(i)
    url = imageServer3.GetFile(isDownloadResult)
    fileName = "c:\temp\" & url.Substring(url.LastIndexOf("/") + 1)
    webClient.DownloadFile(url, fileName)
Next
End Sub

Clipping and downloading source rasters to TIFF

The following code example shows how to clip source rasters based on a geometry and download them to TIFF:
[C#]
public static void ClipDownloadRasters(IImageServer imageServer, IGeometry
    clipGeometry)
{
    //Cast.
    IImageServer3 imageServer3 = (IImageServer3)imageServer;

    //Query the image service using the clip geometry.
    ISpatialFilter spatFilter = new ImageQueryFilterClass();
    spatFilter.Geometry = clipGeometry;
    spatFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelEnvelopeIntersects;
    IFIDSet fidSet = imageServer3.GetCatalogItemIDs((IQueryFilter)spatFilter);

    //Define the Web client.
    System.Net.WebClient webClient = new System.Net.WebClient();

    //Clip and download rasters to TIFF images. The format can be TIFF, NITF, or Imagine image file format.
    IImageServerDownloadResults isDownloadResults = imageServer3.Download(fidSet,
        clipGeometry, "TIFF");
    IImageServerDownloadResult isDownloadResult;
    string url, fileName;
    for (int i = 0; i < isDownloadResults.Count; i++)
    {
        isDownloadResult = isDownloadResults.get_Element(i);
        url = imageServer3.GetFile(isDownloadResult);
        fileName = "c:\\temp\\" + url.Substring(url.LastIndexOf("/") + 1);
        webClient.DownloadFile(url, fileName);
    }
}
[VB.NET]
Public Shared Sub ClipDownloadRasters(ByVal imageServer As IImageServer, ByVal clipGeometry As IGeometry)
'Cast.
Dim imageServer3 As IImageServer3 = CType(imageServer, IImageServer3)

'Query the image service using the clip geometry.
Dim spatFilter As ISpatialFilter = New ImageQueryFilterClass()
spatFilter.Geometry = clipGeometry
spatFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelEnvelopeIntersects
Dim fidSet As IFIDSet = imageServer3.GetCatalogItemIDs(DirectCast(spatFilter, IQueryFilter))

'Define the Web client.
Dim webClient As New System.Net.WebClient()

'Clip and download rasters to TIFF images. The format can be TIFF, NITF, or Imagine image file format.
Dim isDownloadResults As IImageServerDownloadResults = imageServer3.Download(fidSet, clipGeometry, "TIFF")
Dim isDownloadResult As IImageServerDownloadResult
Dim url As String, fileName As String
For i As Integer = 0 To isDownloadResults.Count - 1
    isDownloadResult = isDownloadResults.Element(i)
    url = imageServer3.GetFile(isDownloadResult)
    fileName = "c:\temp\" & url.Substring(url.LastIndexOf("/") + 1)
    webClient.DownloadFile(url, fileName)
Next
End Sub






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 Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
Engine Developer Kit Engine