ArcIMS BlueViewer
ArcIMS_BlueViewer_CSharp\MakeMap.aspx.cs
// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 



using System;
using System.Configuration;
using System.Drawing;
namespace BlueViewer
{

public partial class MakeMap : System.Web.UI.Page
{
  private string imageURL;

    [System.Diagnostics.DebuggerStepThrough()]
    private void InitializeComponent()
    {

    }

   override protected void OnInit(System.EventArgs e)
    {
        base.OnInit(e);

        InitializeComponent();
    }

    override protected void OnLoad(System.EventArgs e)
    {
        base.OnLoad(e);

    bool bRequestFailed = false;
      string sError = "";

    // Test for the VALID_USER
    // key in the session (which will be there if the
    // browser session has visited default.aspx already).

    if (! ((bool)Session["VALID_USER"]))
    {
      sError = AddCRs("Error: Either your user session had timed out or this is an unauthorized client...  Please close your browser and try again.");

      SendErrorScreen(sError, System.Convert.ToInt64(Request.QueryString["WIDTH"]), System.Convert.ToInt64(Request.QueryString["HEIGHT"]));
      return;
    }

    // Get the map.  Trap for failure.

    try
    {

      bRequestFailed = false;

        string sServer = ConfigurationManager.AppSettings["DEFAULT_MAPSERVER"];
        int iPort = System.Convert.ToInt32(ConfigurationManager.AppSettings["DEFAULT_MAPPORT"]);
        string sService = ConfigurationManager.AppSettings["DEFAULT_MAPSERVICE"];
      int iWidth = System.Convert.ToInt32(Request.QueryString["WIDTH"]);
      int iHeight = System.Convert.ToInt32(Request.QueryString["HEIGHT"]);

        ESRI.ArcGIS.ADF.Connection.IMS.TCPConnection conArcIMS = new ESRI.ArcGIS.ADF.Connection.IMS.TCPConnection(sServer, iPort);
        ESRI.ArcGIS.ADF.Connection.IMS.XML.AxlRequests axlRequest = new ESRI.ArcGIS.ADF.Connection.IMS.XML.AxlRequests();
      System.Xml.XmlDocument axlResponse = new System.Xml.XmlDocument();
      conArcIMS.ServiceName = sService;

      string sAXLText = null;
      sAXLText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ARCXML version=\"1.1\">";
      sAXLText = sAXLText + "<REQUEST><GET_IMAGE><PROPERTIES>";
      sAXLText = sAXLText + "<IMAGESIZE width=\"" + iWidth + "\" height=\"" + iHeight + "\"/>";
      sAXLText = sAXLText + "<ENVELOPE minx=\"" + Request.QueryString["XMIN"] + "\" miny=\"" + Request.QueryString["YMIN"] + "\" maxx=\"" + Request.QueryString["XMAX"] + "\" maxy=\"" + Request.QueryString["YMAX"] + "\" />";
      sAXLText = sAXLText + "<LEGEND display=\"false\" />";
      sAXLText = sAXLText + "</PROPERTIES></GET_IMAGE></REQUEST></ARCXML>";

      axlResponse.LoadXml(conArcIMS.Send(sAXLText));
      if (axlResponse.GetElementsByTagName("OUTPUT").Count == 1)
      {
        System.Xml.XmlNodeList nodeOutput = axlResponse.GetElementsByTagName("OUTPUT");
        imageURL = nodeOutput[0].Attributes["url"].Value;
      }

    }
    catch (System.Exception ex)
    {

      bRequestFailed = true;
      sError = ex.Message;

    }

    // If map request failed, generate an image that reports
    // the failure.  Otherwise, redirect the client to the 
    // map.

    if (bRequestFailed)
    {

      string sFullMessage = "An error occurred while processing your map request:" + System.Environment.NewLine + AddCRs(sError);
      SendErrorScreen(sFullMessage, System.Convert.ToInt64(Request.QueryString["WIDTH"]), System.Convert.ToInt64(Request.QueryString["HEIGHT"]));
    }
    else
    {
      Response.Redirect(imageURL);
    }

  }

  private void SendErrorScreen(string sMsg, long lWidth, long lHeight)
  {

    // Purpose: Creates an on-the-fly GIF with a text message.

    Bitmap bmpMap = new Bitmap((int)lWidth, (int)lHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    Graphics graMap = Graphics.FromImage(bmpMap);
    graMap.DrawString(sMsg, new Font("Verdana", 8), Brushes.White, 10, 10);
    bmpMap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
  }


  private string AddCRs(object sIn)
  {

    // Purpose: Adds a carriage return at a given
    //          interval.

    const int INTERVAL = 60;

      string sOut = "";
    int i = 0;
    int j = 0;
    int tempFor1 = sIn.ToString().Length;
    for (i = 1; i <= tempFor1; i++)
    {
      j = j + 1;
      if (j == INTERVAL)
      {
        sOut = sOut + System.Environment.NewLine;
        j = 0;
      }
      sOut = sOut + sIn.ToString().Substring(i - 1, 1);
    }

    return sOut;

  }

}

}