private void ExportActiveViewParameterized(long iOutputResolution, long lResampleRatio, string ExportType, string sOutputName, Boolean bClipToGraphicsExtent) { /* EXPORT PARAMETER: (iOutputResolution) the resolution requested. * EXPORT PARAMETER: (lResampleRatio) Output Image Quality of the export. The value here will only be used if the export * object is a format that allows setting of Output Image Quality, i.e. a vector exporter. * The value assigned to ResampleRatio should be in the range 1 to 5. * 1 corresponds to "Best", 5 corresponds to "Fast" * EXPORT PARAMETER: (ExportType) a string which contains the export type to create. * EXPORT PARAMETER: (sOutputDir) a string which contains the directory to output to. * EXPORT PARAMETER: (bClipToGraphicsExtent) Assign True or False to determine if export image will be clipped to the graphic * extent of layout elements. This value is ignored for data view exports */ /* Exports the Active View of the document to selected output format. */ IActiveView docActiveView = null; if (m_hookHelper == null) { docActiveView = ExportActiveView; } else { docActiveView = m_hookHelper.ActiveView; } IExport docExport; long iPrevOutputImageQuality; IOutputRasterSettings docOutputRasterSettings; IEnvelope PixelBoundsEnv; tagRECT exportRECT; tagRECT DisplayBounds; IDisplayTransformation docDisplayTransformation; IPageLayout docPageLayout; IEnvelope docMapExtEnv; long hdc; long tmpDC; string sNameRoot; long iScreenResolution; bool bReenable = false; IEnvelope docGraphicsExtentEnv; IUnitConverter pUnitConvertor; if (GetFontSmoothing()) { /* font smoothing is on, disable it and set the flag to reenable it later. */ bReenable = true; DisableFontSmoothing(); if (GetFontSmoothing()) { //font smoothing is NOT successfully disabled, error out. return; } //else font smoothing was successfully disabled. } // The Export*Class() type initializes a new export class of the desired type. if (ExportType == "PDF") { docExport = new ExportPDFClass(); IExportPDF pExPDF = docExport as IExportPDF; pExPDF.EmbedFonts = true; } else if (ExportType == "EPS") { docExport = new ExportPSClass(); } else if (ExportType == "AI") { docExport = new ExportAIClass(); } else if (ExportType == "BMP") { docExport = new ExportBMPClass(); } else if (ExportType == "TIFF") { docExport = new ExportTIFFClass(); } else if (ExportType == "SVG") { docExport = new ExportSVGClass(); } else if (ExportType == "PNG") { docExport = new ExportPNGClass(); } else if (ExportType == "GIF") { docExport = new ExportGIFClass(); } else if (ExportType == "EMF") { docExport = new ExportEMFClass(); } else if (ExportType == "JPG") { docExport = new ExportJPEGClass(); } else { MessageBox.Show("Unsupported export type " + ExportType + ", defaulting to EMF."); ExportType = "EMF"; docExport = new ExportEMFClass(); } // save the previous output image quality, so that when the export is complete it will be set back. docOutputRasterSettings = docActiveView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings; iPrevOutputImageQuality = docOutputRasterSettings.ResampleRatio; if (docExport is IExportImage) { // always set the output quality of the DISPLAY to 1 for image export formats SetOutputQuality(docActiveView, 1); } else { // for vector formats, assign the desired ResampleRatio to control drawing of raster layers at export time SetOutputQuality(docActiveView, lResampleRatio); } //set the name root for the export sNameRoot = docActiveView.FocusMap.Name; //set the export filename (which is the nameroot + the appropriate file extension) docExport.ExportFileName = sOutputName; /* Get the device context of the screen */ tmpDC = GetDC(0); /* Get the screen resolution. */ iScreenResolution = GetDeviceCaps((int)tmpDC, 88); //88 is the win32 const for Logical pixels/inch in X) /* release the DC. */ ReleaseDC(0, (int)tmpDC); docExport.Resolution = iOutputResolution; if (docActiveView is IPageLayout) { //get the bounds of the "exportframe" of the active view. DisplayBounds = docActiveView.ExportFrame; //set up pGraphicsExtent, used if clipping to graphics extent. docGraphicsExtentEnv = GetGraphicsExtent(docActiveView); } else { //Get the bounds of the deviceframe for the screen. docDisplayTransformation = docActiveView.ScreenDisplay.DisplayTransformation; DisplayBounds = docDisplayTransformation.get_DeviceFrame(); } PixelBoundsEnv = new Envelope() as IEnvelope; if (bClipToGraphicsExtent && (docActiveView is IPageLayout)) { docGraphicsExtentEnv = GetGraphicsExtent(docActiveView); docPageLayout = docActiveView as PageLayout; pUnitConvertor = new UnitConverter(); //assign the x and y values representing the clipped area to the PixelBounds envelope PixelBoundsEnv.XMin = 0; PixelBoundsEnv.YMin = 0; PixelBoundsEnv.XMax = pUnitConvertor.ConvertUnits(docGraphicsExtentEnv.XMax, docPageLayout.Page.Units, esriUnits.esriInches) * docExport.Resolution - pUnitConvertor.ConvertUnits(docGraphicsExtentEnv.XMin, docPageLayout.Page.Units, esriUnits.esriInches) * docExport.Resolution; PixelBoundsEnv.YMax = pUnitConvertor.ConvertUnits(docGraphicsExtentEnv.YMax, docPageLayout.Page.Units, esriUnits.esriInches) * docExport.Resolution - pUnitConvertor.ConvertUnits(docGraphicsExtentEnv.YMin, docPageLayout.Page.Units, esriUnits.esriInches) * docExport.Resolution; //'assign the x and y values representing the clipped export extent to the exportRECT exportRECT.bottom = (int)(PixelBoundsEnv.YMax) + 1; exportRECT.left = (int)(PixelBoundsEnv.XMin); exportRECT.top = (int)(PixelBoundsEnv.YMin); exportRECT.right = (int)(PixelBoundsEnv.XMax) + 1; //since we're clipping to graphics extent, set the visible bounds. docMapExtEnv = docGraphicsExtentEnv; } else { double tempratio = iOutputResolution / iScreenResolution; double tempbottom = DisplayBounds.bottom * tempratio; double tempright = DisplayBounds.right * tempratio; //'The values in the exportRECT tagRECT correspond to the width //and height to export, measured in pixels with an origin in the top left corner. exportRECT.bottom = (int)Math.Truncate(tempbottom); exportRECT.left = 0; exportRECT.top = 0; exportRECT.right = (int)Math.Truncate(tempright); //populate the PixelBounds envelope with the values from exportRECT. // We need to do this because the exporter object requires an envelope object // instead of a tagRECT structure. PixelBoundsEnv.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom); //since it's a page layout or an unclipped page layout we don't need docMapExtEnv. docMapExtEnv = null; } // Assign the envelope object to the exporter object's PixelBounds property. The exporter object // will use these dimensions when allocating memory for the export file. docExport.PixelBounds = PixelBoundsEnv; // call the StartExporting method to tell docExport you're ready to start outputting. hdc = docExport.StartExporting(); // Redraw the active view, rendering it to the exporter object device context instead of the app display. // We pass the following values: // * hDC is the device context of the exporter object. // * exportRECT is the tagRECT structure that describes the dimensions of the view that will be rendered. // The values in exportRECT should match those held in the exporter object's PixelBounds property. // * docMapExtEnv is an envelope defining the section of the original image to draw into the export object. docActiveView.Output((int)hdc, (int)docExport.Resolution, ref exportRECT, docMapExtEnv, null); //finishexporting, then cleanup. docExport.FinishExporting(); docExport.Cleanup(); //MessageBox.Show("输出完成 " + sOutputName, "地图输出"); //set the output quality back to the previous value SetOutputQuality(docActiveView, iPrevOutputImageQuality); if (bReenable) { /* reenable font smoothing if we disabled it before */ EnableFontSmoothing(); bReenable = false; if (!GetFontSmoothing()) { //error: cannot reenable font smoothing. MessageBox.Show("Unable to reenable Font Smoothing", "Font Smoothing error"); } } docMapExtEnv = null; PixelBoundsEnv = null; }
/// <summary> /// 输出图片 /// </summary> /// <param name="activeView"></param> /// <param name="filename"></param> /// <param name="format"></param> /// <param name="resolution"></param> public static void ExportRasterFile(IActiveView activeView, string filename, string format, double resolution) { if (activeView == null || filename.Length == 0) { return; } double screenResolution = resolution; double outputResolution = resolution; IExport export = null; switch (format) { case "PDF": export = new ExportPDFClass(); IExportVectorOptions exportVectorOptions = export as IExportVectorOptions; exportVectorOptions.PolygonizeMarkers = true; IExportPDF exportPDF = export as IExportPDF; exportPDF.EmbedFonts = true; break; case "BMP": export = new ExportBMPClass(); break; case "JPG": export = new ExportJPEGClass(); if (filename.IndexOf("彩信") != -1) { IExportJPEG exportJPEG = export as IExportJPEG; exportJPEG.Quality = 70; outputResolution = 70.0; } break; case "PNG": export = new ExportPNGClass(); break; case "TIF": export = new ExportTIFFClass(); break; case "GIF": export = new ExportGIFClass(); break; case "EMF": export = new ExportEMFClass(); break; case "SVG": export = new ExportSVGClass(); break; case "AI": export = new ExportAIClass(); break; case "EPS": export = new ExportPSClass(); break; } IGraphicsContainer docGraphicsContainer; IElement docElement; IOutputRasterSettings docOutputRasterSettings; IMapFrame docMapFrame; IActiveView tmpActiveView; IOutputRasterSettings doOutputRasterSettings; if (activeView is IMap) { doOutputRasterSettings = activeView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings; doOutputRasterSettings.ResampleRatio = 3; } else if (activeView is IPageLayout) { doOutputRasterSettings = activeView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings; doOutputRasterSettings.ResampleRatio = 4; //and assign ResampleRatio to the maps in the PageLayout. docGraphicsContainer = activeView as IGraphicsContainer; docGraphicsContainer.Reset(); docElement = docGraphicsContainer.Next(); int c = 0; while (docElement != null) { c += 1; if (docElement is IMapFrame) { docMapFrame = docElement as IMapFrame; tmpActiveView = docMapFrame.Map as IActiveView; docOutputRasterSettings = tmpActiveView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings; docOutputRasterSettings.ResampleRatio = 1; } docElement = docGraphicsContainer.Next(); } docMapFrame = null; docGraphicsContainer = null; tmpActiveView = null; } else { docOutputRasterSettings = null; } //end export.ExportFileName = filename; export.Resolution = outputResolution; tagRECT exportRECT = activeView.ExportFrame; exportRECT.right = (int)(exportRECT.right * (outputResolution / screenResolution)); exportRECT.bottom = (int)(exportRECT.bottom * (outputResolution / screenResolution)); IEnvelope envelope = new EnvelopeClass(); envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom); export.PixelBounds = envelope; int hDC = export.StartExporting(); activeView.Output(hDC, Convert.ToInt32(export.Resolution), ref exportRECT, null, null); export.FinishExporting(); export.Cleanup(); }