//制图输出函数 public void OutputPhoto(string fileName) { double iScreenDispalyResolution = axPageLayoutControl1.ActiveView.ScreenDisplay.DisplayTransformation.Resolution; ESRI.ArcGIS.Output.IExport pExport = new ESRI.ArcGIS.Output.ExportPNGClass(); //设置输出文件路径及名称 pExport.ExportFileName = fileName; // 设置输出分辨率 pExport.Resolution = (short)iScreenDispalyResolution; // 获取输出范围,获取视图框架对象,进而得到视图范围 tagRECT deviceRect = axPageLayoutControl1.ActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame(); IEnvelope pDeviceEnvelop = new EnvelopeClass(); // 设置一个边框范围 pDeviceEnvelop.PutCoords(deviceRect.left, deviceRect.bottom, deviceRect.right, deviceRect.top); // 将打印像素范围设置给输出对象 pExport.PixelBounds = pDeviceEnvelop; // 设置跟踪取消对象 ITrackCancel pCancle = new CancelTrackerClass(); // 进行视图控件的视图输出操作,设置对应参数 axPageLayoutControl1.ActiveView.Output(pExport.StartExporting(), (int)pExport.Resolution, ref deviceRect, axPageLayoutControl1.ActiveView.Extent, pCancle); Application.DoEvents(); pExport.FinishExporting(); pExport.Cleanup(); }
//输出当前地图至指定的文件 public static void ExportActiveView(IActiveView pView, Size outRect, string outPath) { try { //参数检查 if (pView == null) { throw new Exception("输入参数错误,无法生成图片文件!"); } //根据给定的文件扩展名,来决定生成不同类型的对象 ESRI.ArcGIS.Output.IExport export = null; if (outPath.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase)) { export = new ESRI.ArcGIS.Output.ExportJPEGClass(); } else if (outPath.EndsWith(".tiff", StringComparison.OrdinalIgnoreCase)) { export = new ESRI.ArcGIS.Output.ExportTIFFClass(); } else if (outPath.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase)) { export = new ESRI.ArcGIS.Output.ExportBMPClass(); } else if (outPath.EndsWith(".emf", StringComparison.OrdinalIgnoreCase)) { export = new ESRI.ArcGIS.Output.ExportEMFClass(); } else if (outPath.EndsWith(".png", StringComparison.OrdinalIgnoreCase)) { export = new ESRI.ArcGIS.Output.ExportPNGClass(); } else if (outPath.EndsWith(".gif", StringComparison.OrdinalIgnoreCase)) { export = new ESRI.ArcGIS.Output.ExportGIFClass(); } SetOutputQuality(pView, 1); export.ExportFileName = outPath; IEnvelope pEnvelope = pView.Extent; //导出参数 export.Resolution = 300; tagRECT exportRect = new tagRECT(); exportRect.left = exportRect.top = 0; exportRect.right = outRect.Width; exportRect.bottom = (int)(exportRect.right * pEnvelope.Height / pEnvelope.Width); ESRI.ArcGIS.Geometry.IEnvelope envelope = new ESRI.ArcGIS.Geometry.EnvelopeClass(); //输出范围 envelope.PutCoords(exportRect.left, exportRect.top, exportRect.right, exportRect.bottom); export.PixelBounds = envelope; //可用于取消操作 ITrackCancel pCancel = new CancelTrackerClass(); export.TrackCancel = pCancel; pCancel.Reset(); //点击ESC键时,中止转出 pCancel.CancelOnKeyPress = true; pCancel.CancelOnClick = false; pCancel.ProcessMessages = true; //获取handle System.Int32 hDC = export.StartExporting(); //开始转出 pView.Output(hDC, (System.Int32)export.Resolution, ref exportRect, pEnvelope, pCancel); bool bContinue = pCancel.Continue(); //捕获是否继续 if (bContinue) { export.FinishExporting(); export.Cleanup(); } else { export.Cleanup(); } bContinue = pCancel.Continue(); } catch (Exception e) { //错误信息提示 } }
protected override void OnClick() { // Get the min/max zoom from user input int minzoom = 0; int maxzoom = 6; Ecotrust.Form1 form1 = new Ecotrust.Form1(); if (form1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { minzoom = (int)form1.numericUpDown1.Value; maxzoom = (int)form1.numericUpDown2.Value; } else { return; //TODO } // Use the FolderBrowserDialog Class to choose export folder System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog(); folderDialog.Description = "Select output folder for map tiles..."; string exportDir = ""; if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { // The returned string will be the full path, filename and file-extension for the chosen shapefile. Example: "C:\test\cities.shp" exportDir = folderDialog.SelectedPath; if (exportDir == "") return; // TODO raise error } else { return; //TODO } ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument = ArcMap.Application.Document as ESRI.ArcGIS.ArcMapUI.IMxDocument; // Dynamic Cast ESRI.ArcGIS.Carto.IActiveView activeView = mxDocument.ActiveView; ESRI.ArcGIS.Carto.IMap map = activeView.FocusMap; ESRI.ArcGIS.Geometry.IEnvelope mapaoi = activeView.Extent; // Set up export object and tile pixel coordinates int tileSizeX = 256; int tileSizeY = 256; // set up exporter with transparent background ESRI.ArcGIS.Output.IExportPNG pngexport = new ESRI.ArcGIS.Output.ExportPNGClass(); ESRI.ArcGIS.Display.IColor tcolor = new ESRI.ArcGIS.Display.RgbColorClass(); // Warning: 254,254,254 will be set to transparent; don't use in any of map styling ((IRgbColor)tcolor).Red = 254; ((IRgbColor)tcolor).Green = 254; ((IRgbColor)tcolor).Blue = 254; ((ExportPNG)pngexport).BackgroundColor = tcolor; pngexport.TransparentColor = tcolor; ESRI.ArcGIS.Output.IExport export = (ESRI.ArcGIS.Output.IExport)pngexport; ESRI.ArcGIS.esriSystem.tagRECT exportRECT; exportRECT.left = 0; exportRECT.top = 0; exportRECT.right = tileSizeX; exportRECT.bottom = tileSizeY; ESRI.ArcGIS.Geometry.IEnvelope envelope = new ESRI.ArcGIS.Geometry.EnvelopeClass(); envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom); export.PixelBounds = envelope; map.DelayDrawing(true); // Turn off all layers for (int i = 0; i < map.LayerCount; i++) map.get_Layer(i).Visible = false; // Calculate total number of tiles needed GlobalMercator mercator = new GlobalMercator(); GlobalMercator.Coords tempmins; GlobalMercator.Coords tempmaxs; Double numTiles = 0; for (int tz = minzoom; tz <= maxzoom; tz++) { tempmins = mercator.MetersToTile(mapaoi.XMin, mapaoi.YMin, tz); tempmaxs = mercator.MetersToTile(mapaoi.XMax, mapaoi.YMax, tz); numTiles += ((tempmaxs.y - tempmins.y)+1) * ((tempmaxs.x - tempmins.x)+1); } numTiles *= map.LayerCount; ESRI.ArcGIS.esriSystem.IStatusBar statusBar = ArcMap.Application.StatusBar; statusBar.set_Message(0, "Rendering " + numTiles.ToString() + " tiles"); // Create a CancelTracker ESRI.ArcGIS.esriSystem.ITrackCancel trackCancel = new ESRI.ArcGIS.Display.CancelTrackerClass(); ESRI.ArcGIS.Framework.IProgressDialogFactory progressDialogFactory = new ESRI.ArcGIS.Framework.ProgressDialogFactoryClass(); // Set the properties of the Step Progressor System.Int32 int32_hWnd = ArcMap.Application.hWnd; ESRI.ArcGIS.esriSystem.IStepProgressor stepProgressor = progressDialogFactory.Create(trackCancel, int32_hWnd); stepProgressor.MinRange = 0; stepProgressor.MaxRange = (int)numTiles; stepProgressor.StepValue = 1; stepProgressor.Message = "Calculating " + numTiles.ToString() + " tiles"; // Create the ProgressDialog. This automatically displays the dialog ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog2 = (ESRI.ArcGIS.Framework.IProgressDialog2)stepProgressor; // Explict Cast // Set the properties of the ProgressDialog progressDialog2.CancelEnabled = true; progressDialog2.Description = "Rendering " + numTiles.ToString() + " map tiles"; progressDialog2.Title = "Creating map tiles..."; progressDialog2.Animation = ESRI.ArcGIS.Framework.esriProgressAnimationTypes.esriDownloadFile; System.Boolean boolean_Continue = true; int tileCount = 0; for (int lyrnum = 0; lyrnum < map.LayerCount; lyrnum++) { // Turn on the layer of interest ESRI.ArcGIS.Carto.ILayer layer = map.get_Layer(lyrnum); layer.Visible = true; // Set extents //ESRI.ArcGIS.Geometry.IEnvelope layeraoi = layer.AreaOfInterest; ESRI.ArcGIS.Geometry.IEnvelope aoi = new ESRI.ArcGIS.Geometry.EnvelopeClass(); // Create layer directory if it doesn't exist DirectoryInfo dir = new DirectoryInfo(exportDir + "\\" + layer.Name); if (!dir.Exists) dir.Create(); DateTime startTime = DateTime.Now; // Loop through zoom levels, rows, cols for (int tz = minzoom; tz <= maxzoom; tz++) { GlobalMercator.Coords mins = mercator.MetersToTile(mapaoi.XMin, mapaoi.YMin, tz); GlobalMercator.Coords maxs = mercator.MetersToTile(mapaoi.XMax, mapaoi.YMax, tz); // Create zoom directory if it doesn't exist DirectoryInfo dir2 = new DirectoryInfo(dir.FullName + "\\" + tz); if (!dir2.Exists) dir2.Create(); for (int tx = (int)mins.x; tx <= (int)maxs.x; tx++) { // Create X directory if it doesn't exist DirectoryInfo dir3 = new DirectoryInfo(dir2.FullName + "\\" + tx); if (!dir3.Exists) dir3.Create(); for (int ty = (int)mins.y; ty <= (int)maxs.y; ty++) { // Flip y-axis for output tile name int invertTy = (int) ((Math.Pow(2, tz) - 1) - ty); tileCount += 1; // TODO Calculate time and set new message // TimeSpan timeElapsed = TimeSpan.FromTicks(DateTime.Now.Subtract(startTime).Ticks); // * ((double)tileCount - (numTiles + 1)) / (numTiles + 1)); // double timeRemaining = (timeElapsed.TotalSeconds / (tileCount / numTiles)) - timeElapsed.TotalSeconds; //(" + ((int)timeRemaining).ToString() +" remaining)"; stepProgressor.Message = layer.Name + "\\" + tz + "\\" + tx + "\\" + invertTy + ".png (" + tileCount + " of " + numTiles + ")"; export.ExportFileName = dir3.FullName + "\\" + invertTy + ".png"; GlobalMercator.Bounds bnd = mercator.TileBounds(tx, ty, tz); aoi.PutCoords(bnd.minx, bnd.miny, bnd.maxx, bnd.maxy); aoi.SpatialReference = map.SpatialReference; // TODO aoi spatial reference == mercator? // Use FullExtent instead of Extent to make the extent independent of the activeView ratio activeView.FullExtent = aoi; // Export System.Int32 hDC = export.StartExporting(); activeView.Output(hDC, (System.Int16)export.Resolution, ref exportRECT, null, null); // Explicit Cast and 'ref' keyword needed export.FinishExporting(); export.Cleanup(); stepProgressor.Position = tileCount; //Check if the cancel button was pressed. If so, break out of row boolean_Continue = trackCancel.Continue(); if (!boolean_Continue) break; } //Check if the cancel button was pressed. If so, break out of col boolean_Continue = trackCancel.Continue(); if (!boolean_Continue) break; } //Check if the cancel button was pressed. If so, break out of layers boolean_Continue = trackCancel.Continue(); if (!boolean_Continue) break; // Write log using (System.IO.StreamWriter file = new System.IO.StreamWriter( exportDir + "\\log.txt", true)) { file.WriteLine(layer.Name + ", zoom " + tz + ", numtiles " + tileCount + ":" + mins.x + " " + mins.y + " " + maxs.x + " " + maxs.y); } } // Turn it off layer.Visible = false; } map.DelayDrawing(false); // Turn ON all layers for (int i = 0; i < map.LayerCount; i++) map.get_Layer(i).Visible = true; // restore extent activeView.FullExtent = mapaoi; activeView.Refresh(); // Done trackCancel = null; stepProgressor = null; progressDialog2.HideDialog(); progressDialog2 = null; }
protected override void OnClick() { // Get the min/max zoom from user input int minzoom = 0; int maxzoom = 6; Ecotrust.Form1 form1 = new Ecotrust.Form1(); if (form1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { minzoom = (int)form1.numericUpDown1.Value; maxzoom = (int)form1.numericUpDown2.Value; } else { return; //TODO } // Use the FolderBrowserDialog Class to choose export folder System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog(); folderDialog.Description = "Select output folder for map tiles..."; string exportDir = ""; if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { // The returned string will be the full path, filename and file-extension for the chosen shapefile. Example: "C:\test\cities.shp" exportDir = folderDialog.SelectedPath; if (exportDir == "") { return; // TODO raise error } } else { return; //TODO } ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument = ArcMap.Application.Document as ESRI.ArcGIS.ArcMapUI.IMxDocument; // Dynamic Cast ESRI.ArcGIS.Carto.IActiveView activeView = mxDocument.ActiveView; ESRI.ArcGIS.Carto.IMap map = activeView.FocusMap; ESRI.ArcGIS.Geometry.IEnvelope mapaoi = activeView.Extent; // Set up export object and tile pixel coordinates int tileSizeX = 256; int tileSizeY = 256; // set up exporter with transparent background ESRI.ArcGIS.Output.IExportPNG pngexport = new ESRI.ArcGIS.Output.ExportPNGClass(); ESRI.ArcGIS.Display.IColor tcolor = new ESRI.ArcGIS.Display.RgbColorClass(); // Warning: 254,254,254 will be set to transparent; don't use in any of map styling ((IRgbColor)tcolor).Red = 254; ((IRgbColor)tcolor).Green = 254; ((IRgbColor)tcolor).Blue = 254; ((ExportPNG)pngexport).BackgroundColor = tcolor; pngexport.TransparentColor = tcolor; ESRI.ArcGIS.Output.IExport export = (ESRI.ArcGIS.Output.IExport)pngexport; ESRI.ArcGIS.esriSystem.tagRECT exportRECT; exportRECT.left = 0; exportRECT.top = 0; exportRECT.right = tileSizeX; exportRECT.bottom = tileSizeY; ESRI.ArcGIS.Geometry.IEnvelope envelope = new ESRI.ArcGIS.Geometry.EnvelopeClass(); envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom); export.PixelBounds = envelope; map.DelayDrawing(true); // Turn off all layers for (int i = 0; i < map.LayerCount; i++) { map.get_Layer(i).Visible = false; } // Calculate total number of tiles needed GlobalMercator mercator = new GlobalMercator(); GlobalMercator.Coords tempmins; GlobalMercator.Coords tempmaxs; Double numTiles = 0; for (int tz = minzoom; tz <= maxzoom; tz++) { tempmins = mercator.MetersToTile(mapaoi.XMin, mapaoi.YMin, tz); tempmaxs = mercator.MetersToTile(mapaoi.XMax, mapaoi.YMax, tz); numTiles += ((tempmaxs.y - tempmins.y) + 1) * ((tempmaxs.x - tempmins.x) + 1); } numTiles *= map.LayerCount; ESRI.ArcGIS.esriSystem.IStatusBar statusBar = ArcMap.Application.StatusBar; statusBar.set_Message(0, "Rendering " + numTiles.ToString() + " tiles"); // Create a CancelTracker ESRI.ArcGIS.esriSystem.ITrackCancel trackCancel = new ESRI.ArcGIS.Display.CancelTrackerClass(); ESRI.ArcGIS.Framework.IProgressDialogFactory progressDialogFactory = new ESRI.ArcGIS.Framework.ProgressDialogFactoryClass(); // Set the properties of the Step Progressor System.Int32 int32_hWnd = ArcMap.Application.hWnd; ESRI.ArcGIS.esriSystem.IStepProgressor stepProgressor = progressDialogFactory.Create(trackCancel, int32_hWnd); stepProgressor.MinRange = 0; stepProgressor.MaxRange = (int)numTiles; stepProgressor.StepValue = 1; stepProgressor.Message = "Calculating " + numTiles.ToString() + " tiles"; // Create the ProgressDialog. This automatically displays the dialog ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog2 = (ESRI.ArcGIS.Framework.IProgressDialog2)stepProgressor; // Explict Cast // Set the properties of the ProgressDialog progressDialog2.CancelEnabled = true; progressDialog2.Description = "Rendering " + numTiles.ToString() + " map tiles"; progressDialog2.Title = "Creating map tiles..."; progressDialog2.Animation = ESRI.ArcGIS.Framework.esriProgressAnimationTypes.esriDownloadFile; System.Boolean boolean_Continue = true; int tileCount = 0; for (int lyrnum = 0; lyrnum < map.LayerCount; lyrnum++) { // Turn on the layer of interest ESRI.ArcGIS.Carto.ILayer layer = map.get_Layer(lyrnum); layer.Visible = true; // Set extents //ESRI.ArcGIS.Geometry.IEnvelope layeraoi = layer.AreaOfInterest; ESRI.ArcGIS.Geometry.IEnvelope aoi = new ESRI.ArcGIS.Geometry.EnvelopeClass(); // Create layer directory if it doesn't exist DirectoryInfo dir = new DirectoryInfo(exportDir + "\\" + layer.Name); if (!dir.Exists) { dir.Create(); } DateTime startTime = DateTime.Now; // Loop through zoom levels, rows, cols for (int tz = minzoom; tz <= maxzoom; tz++) { GlobalMercator.Coords mins = mercator.MetersToTile(mapaoi.XMin, mapaoi.YMin, tz); GlobalMercator.Coords maxs = mercator.MetersToTile(mapaoi.XMax, mapaoi.YMax, tz); // Create zoom directory if it doesn't exist DirectoryInfo dir2 = new DirectoryInfo(dir.FullName + "\\" + tz); if (!dir2.Exists) { dir2.Create(); } for (int tx = (int)mins.x; tx <= (int)maxs.x; tx++) { // Create X directory if it doesn't exist DirectoryInfo dir3 = new DirectoryInfo(dir2.FullName + "\\" + tx); if (!dir3.Exists) { dir3.Create(); } for (int ty = (int)mins.y; ty <= (int)maxs.y; ty++) { // Flip y-axis for output tile name int invertTy = (int)((Math.Pow(2, tz) - 1) - ty); tileCount += 1; // TODO Calculate time and set new message // TimeSpan timeElapsed = TimeSpan.FromTicks(DateTime.Now.Subtract(startTime).Ticks); // * ((double)tileCount - (numTiles + 1)) / (numTiles + 1)); // double timeRemaining = (timeElapsed.TotalSeconds / (tileCount / numTiles)) - timeElapsed.TotalSeconds; //(" + ((int)timeRemaining).ToString() +" remaining)"; stepProgressor.Message = layer.Name + "\\" + tz + "\\" + tx + "\\" + invertTy + ".png (" + tileCount + " of " + numTiles + ")"; export.ExportFileName = dir3.FullName + "\\" + invertTy + ".png"; GlobalMercator.Bounds bnd = mercator.TileBounds(tx, ty, tz); aoi.PutCoords(bnd.minx, bnd.miny, bnd.maxx, bnd.maxy); aoi.SpatialReference = map.SpatialReference; // TODO aoi spatial reference == mercator? // Use FullExtent instead of Extent to make the extent independent of the activeView ratio activeView.FullExtent = aoi; // Export System.Int32 hDC = export.StartExporting(); activeView.Output(hDC, (System.Int16)export.Resolution, ref exportRECT, null, null); // Explicit Cast and 'ref' keyword needed export.FinishExporting(); export.Cleanup(); stepProgressor.Position = tileCount; //Check if the cancel button was pressed. If so, break out of row boolean_Continue = trackCancel.Continue(); if (!boolean_Continue) { break; } } //Check if the cancel button was pressed. If so, break out of col boolean_Continue = trackCancel.Continue(); if (!boolean_Continue) { break; } } //Check if the cancel button was pressed. If so, break out of layers boolean_Continue = trackCancel.Continue(); if (!boolean_Continue) { break; } // Write log using (System.IO.StreamWriter file = new System.IO.StreamWriter(exportDir + "\\log.txt", true)) { file.WriteLine(layer.Name + ", zoom " + tz + ", numtiles " + tileCount + ":" + mins.x + " " + mins.y + " " + maxs.x + " " + maxs.y); } } // Turn it off layer.Visible = false; } map.DelayDrawing(false); // Turn ON all layers for (int i = 0; i < map.LayerCount; i++) { map.get_Layer(i).Visible = true; } // restore extent activeView.FullExtent = mapaoi; activeView.Refresh(); // Done trackCancel = null; stepProgressor = null; progressDialog2.HideDialog(); progressDialog2 = null; }