[C#]ArcGIS 출력물 만들기

2023. 3. 24. 19:12프로그래밍

728x90

지도를 보는 프로그램을 사용하다 보면 사용자 입장에서 제링 많이 사용 하는 기능 중에 하나가 출력이 아닐까 하는 생각이 듭니다.

저의 생각으로는 GIS 프로그램 중에 젤 많이 쓰는 비싼 프로그램 중에 하나인 ArcGIS 라는 프로그램도 이런 출력 기능을 지원하는 데요

 

프로그램을 만드는 사람의 입장에서는 어떻게 이런 출력물을 만덜어 낼 수 있을 지에 대해서 알아보려고 합니다.

 

우선 현재 내가 보고 있는 지도를 출력물로 만들어 낸다는 것은

  • 지금 보고 있는 지도의 모든 레이어들을 가진 뷰 - IActiveView
  • 이 레이어 뷰들을 출력할 출력 클래스 - IPrintAndExport

일 것입니다.

 

다음 스택 오버플로우 문서를 따라가면 해당 내용들이 나오는 데요

https://gis.stackexchange.com/questions/37533/how-to-output-activeview-to-jpeg-image-using-arcobjects

///<summary>Creates a .jpg (JPEG) file from the ActiveView using a high resolution exporting option. Default values of 96 DPI are overwritten to 300 used for the image creation.</summary>
///
///<param name="activeView">An IActiveView interface</param>
///<param name="pathFileName">A System.String that the path and filename of the JPEG you want to create. Example: "C:\temp\hiResolutionTest.jpg"</param>
/// 
///<returns>A System.Boolean indicating the success</returns>
/// 
///<remarks></remarks>
public System.Boolean CreateJPEGHiResolutionFromActiveView(ESRI.ArcGIS.Carto.IActiveView activeView, System.String pathFileName)
{
  //parameter check
  if (activeView == null || !(pathFileName.EndsWith(".jpg")))
  {
    return false;
  }
  ESRI.ArcGIS.Output.IExport export = new ESRI.ArcGIS.Output.ExportJPEGClass();
  export.ExportFileName = pathFileName;

  // Because we are exporting to a resolution that differs from screen 
  // resolution, we should assign the two values to variables for use 
  // in our sizing calculations
  System.Int32 screenResolution = 96;
  System.Int32 outputResolution = 300;

  export.Resolution = outputResolution;

  ESRI.ArcGIS.Display.tagRECT exportRECT; // This is a structure
  exportRECT.left = 0;
  exportRECT.top = 0;
  exportRECT.right = activeView.ExportFrame.right * (outputResolution / screenResolution);
  exportRECT.bottom = activeView.ExportFrame.bottom * (outputResolution / screenResolution);

  // Set up the PixelBounds envelope to match the exportRECT
  ESRI.ArcGIS.Geometry.IEnvelope envelope = new ESRI.ArcGIS.Geometry.EnvelopeClass();
  envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom);
  export.PixelBounds = envelope;

  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();

  return true;
}
 
///<summary>Creates a .jpg (JPEG) file from IActiveView. Default values of 96 DPI are used for the image creation.</summary>
///
///<param name="activeView">An IActiveView interface</param>
///<param name="pathFileName">A System.String that the path and filename of the JPEG you want to create. Example: "C:\temp\test.jpg"</param>
/// 
///<returns>A System.Boolean indicating the success</returns>
/// 
///<remarks></remarks>
public System.Boolean CreateJPEGFromActiveView(ESRI.ArcGIS.Carto.IActiveView activeView, System.String pathFileName)
{
  //parameter check
  if (activeView == null || !(pathFileName.EndsWith(".jpg")))
  {
    return false;
  }
  ESRI.ArcGIS.Output.IExport export = new ESRI.ArcGIS.Output.ExportJPEGClass();
  export.ExportFileName = pathFileName;

  // Microsoft Windows default DPI resolution
  export.Resolution = 96;
  ESRI.ArcGIS.Display.tagRECT exportRECT = activeView.ExportFrame;
  ESRI.ArcGIS.Geometry.IEnvelope envelope = new ESRI.ArcGIS.Geometry.EnvelopeClass();
  envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom);
  export.PixelBounds = envelope;
  System.Int32 hDC = export.StartExporting();
  activeView.Output(hDC, (System.Int16)export.Resolution, ref exportRECT, null, null);

  // Finish writing the export file and cleanup any intermediate files
  export.FinishExporting();
  export.Cleanup();

  return true;
}
 
static void ExportJPEG(IMxDocument mxDoc)
{
	Int64 lScrRes = (Int64)mxDoc.ActiveView.ScreenDisplay.DisplayTransformation.Resolution;
	IExport pExporter = new ExportJPEGClass();


   // IExporter pExporter = new JpegExporterClass() ;
	pExporter.ExportFileName = @"C:\Workspace\Maps\CodeOut\test3.jpeg";
	pExporter.Resolution = (Int32)lScrRes;

	tagRECT deviceRect = mxDoc.ActiveView.ExportFrame; // ScreenDisplay.DisplayTransformation.get_DeviceFrame();
   // tagRECT deviceRect = mxDoc.ActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame();
	IEnvelope pDriverBounds = new EnvelopeClass();
	pDriverBounds.PutCoords(deviceRect.left, deviceRect.top, deviceRect.right, deviceRect.bottom);
  
	pExporter.PixelBounds = pDriverBounds;
  // ITrackCancel pCancel = new CancelTrackerClass();

 //   mxDoc.ActiveView.Output(pExporter.StartExporting(), (Int32)lScrRes, ref deviceRect, mxDoc.ActiveView.Extent, pCancel);
   mxDoc.ActiveView.Output(Convert.ToInt32(pExporter.StartExporting()), Convert.ToInt32 (lScrRes), ref deviceRect, null, null);
	pExporter.FinishExporting();
	pExporter.Cleanup();

}
 

위의 코드는 JPEG 파일로 내보내기 하는 것만의 코드이나, 이 IExport 인터페이스에는 여러 종류에 대한 형식을 내보내기 할 수 있습니다.

다음과 같이 IExport 타입을 정해 볼 수도 있을 것입니다:

예: string exportFormat = "PDF";

switch (exportFormat)
{
	case "PDF":
		IExportPDF pdfExport = new ExportPDFClass();
		pdfExport.ImageCompression = esriExportImageCompression.esriExportImageCompressionDeflate;
		pdfExport.EmbedFonts = true;
		pdfExport.Compressed = true;
		//IExportPDF2 pdfExport2 = (IExportPDF2)pdfExport;
		//pdfExport2.ExportPDFLayersAndFeatureAttributes = esriExportPDFLayerOptions.esriExportPDFLayerOptionsLayersOnly;
		export = (IExport)pdfExport;
		break;
	case "GIF":
		IExportGIF gifExport = new ExportGIFClass();
		export = (IExport)gifExport;
		break;
	case "JPG":
		IExportJPEG jpgExport = new ExportJPEGClass();
		export = (IExport)jpgExport;
		break;
	case "PNG":
		IExportPNG pngExport = new ExportPNGClass();
		export = (IExport)pngExport;
		break;
}
 

 

private static void ExportMap(IMapDocument map,MapExportFormat exportFormat, string outputFile)
{
	int OUTPUT_RES = 600;
	IExport export = null;

	switch (exportFormat)
	{
		case MapExportFormat.PDF:
			IExportPDF pdfExport = new ExportPDFClass();
			pdfExport.ImageCompression = esriExportImageCompression.esriExportImageCompressionDeflate;
			pdfExport.EmbedFonts = true;
			pdfExport.Compressed = true;
			//IExportPDF2 pdfExport2 = (IExportPDF2)pdfExport;
			//pdfExport2.ExportPDFLayersAndFeatureAttributes = esriExportPDFLayerOptions.esriExportPDFLayerOptionsLayersOnly;
			export = (IExport)pdfExport;
			break;
		case MapExportFormat.GIF:
			IExportGIF gifExport = new ExportGIFClass();
			export = (IExport)gifExport;
			break;
		case MapExportFormat.JPG:
			IExportJPEG jpgExport = new ExportJPEGClass();
			export = (IExport)jpgExport;
			break;
		case MapExportFormat.PNG:
			IExportPNG pngExport = new ExportPNGClass();
			export = (IExport)pngExport;
			break;

	}

	export.Resolution = OUTPUT_RES;
	export.ExportFileName = outputFile;

	tagRECT exportFrame = map.ActiveView.ExportFrame;
	IEnvelope exportEnvelope = new EnvelopeClass();
	exportEnvelope.PutCoords(exportFrame.left, exportFrame.top, exportFrame.right, exportFrame.bottom);
	export.PixelBounds = exportEnvelope;

	int hdc = export.StartExporting();
	map.ActiveView.Output(hdc, OUTPUT_RES,exportFrame,null,null);
	export.FinishExporting();
	export.Cleanup();
}
 
 
 

 

 

728x90