⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scalebarprinter.cs

📁 arcgis engine地图打印任务的应用
💻 CS
字号:
// Copyright 2006 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 use restrictions at /arcgis/developerkit/userestrictions.
// 

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;

namespace PrintTask_CSharp
{
    internal class ScaleBarPrinter
    {
        /// <summary>
        /// Refreshes scalebar and retrieves the image
        /// </summary>
        /// <param name="scaleBar">ScaleBar object. Must be tied to Map with correct size/scale.</param>
        /// <returns>Bitmap of scalebar.</returns>
        public static Bitmap GetRefreshedScalebar(ScaleBar scaleBar)
        {
            Bitmap imgBitmap = null;

            scaleBar.Refresh();

            // callback results has the URL or MIME ID in the parameters
            int pos1 = -1;

            for (int cbIdx = 0; cbIdx < scaleBar.CallbackResults.Count; cbIdx++)
            {

                CallbackResult cbResult = scaleBar.CallbackResults[cbIdx];
                string cbString = cbResult.Parameters[0].ToString();
                string url = String.Empty;

                // need to extract the URL from the rest of the callback results string
                pos1 = cbString.IndexOf("switchImageSourceAndAlphaBlend");
                if (pos1 > 0)
                {
                    // move to start of the image URL, which is in single-quotes ('imgurl...')
                    pos1 = cbString.IndexOf("'", pos1);
                    if (pos1 > 0)
                    {
                        int posEnd = cbString.IndexOf("'", pos1 + 1);

                        // see if URL is a file with http/https (true for ArcIMS)
                        int posStart = cbString.IndexOf("http", pos1);
                        if (posStart > 0)
                        {
                            // download the bitmap via URL (for ArcIMS)
                            url = cbString.Substring(posStart, posEnd - posStart);
                            imgBitmap = GetBitmapFromUrl(url);
                        }
                        else
                        {
                            // Image is in MIME data ((always true for AGS, even if 
                            // RequestMimeData set to false for the primary map resource)
                            string mimeId = GetMimeId(cbString, pos1);
                            imgBitmap = GetBitmapFromSession(scaleBar.Page.Session, mimeId);
                        }
                    }
                    // found the image, so can stop examining callback results
                    break;
                }
            }

            return imgBitmap;
        }

        // downloads the Bitmap from a URL
        private static Bitmap GetBitmapFromUrl(string url)
        {
            try
            {
                // Use WebClient as shortcut to WebRequest for getting image
                System.Net.WebClient webClient = new System.Net.WebClient();
                Bitmap imgBitmap = new Bitmap(webClient.OpenRead(url));

                return imgBitmap;
            }
            catch (System.Net.WebException)
            {
                return null;
            }
        }

        // extracts MIME ID from the callback string
        private static string GetMimeId(string mimeString, int startPosition)
        {
            string mimeId = String.Empty;
            int startPos = mimeString.IndexOf("ImgID=", startPosition);
            if (startPos > 0)
            {
                startPos += 6;  // advance to start of MIME id
                int endPos = mimeString.IndexOf("&", startPos);
                mimeId = mimeString.Substring(startPos, endPos - startPos);
            }
            return mimeId;
        }

        // retrieves the image from the Session MIME data
        private static Bitmap GetBitmapFromSession(
            System.Web.SessionState.HttpSessionState session, string mimeDataSessionId)
        {
            Bitmap imgBitmap = null;

            // attempt to retrieve MIME data from the Session
            ESRI.ArcGIS.ADF.Web.MimeData mimeData = 
                session[mimeDataSessionId] as ESRI.ArcGIS.ADF.Web.MimeData;

            if (mimeData != null)
            {
                byte[] imgBytes = mimeData.Bytes;
                System.IO.MemoryStream imgStream =
                    new System.IO.MemoryStream(imgBytes);

                imgBitmap = new Bitmap(imgStream);
            }

            return imgBitmap;
        }

        //// gets the path to the page, including full http path and application path 
        // (Not needed since getting mime data directly from Session)
        //private static String GetFullApplicationPath(System.Web.HttpRequest request)
        //{
        //    Uri uri = request.Url;
        //    UriBuilder uriBld = new UriBuilder(uri.Scheme, uri.Host);
        //    if (uri.Port > 0 && uri.Port != 80 && uri.Port != 443)
        //        uriBld.Port = uri.Port;
        //    uriBld.Path = request.ApplicationPath;

        //    return uriBld.Uri.AbsoluteUri;
        //}
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -