/// <summary> /// 根据文件路径和路径中的扩展名,来决定生成导出不同类型图片的Export对象 /// </summary> /// <param name="picPath">导出的图片的路径</param> /// <param name="resolution"></param> /// <returns></returns> private static IExport CreateExport(string picPath, double resolution) { IExport export; string extension = System.IO.Path.GetExtension(picPath)?.ToLower(); switch (extension) { case ".jpg": export = new ExportJPEGClass(); break; case ".tiff": export = new ExportTIFFClass(); break; case ".bmp": export = new ExportBMPClass(); break; case ".emf": export = new ExportEMFClass(); break; case ".png": export = new ExportPNGClass(); break; case ".gif": export = new ExportGIFClass(); break; case ".pdf": export = new ExportPDFClass(); break; case ".eps": export = new ExportPSClass(); break; case ".ai": export = new ExportAIClass(); break; case ".svg": export = new ExportSVGClass(); break; default: throw new Exception("不支持的图片输出格式!(程序支持的图片格式包括:jpg,tiff,bmp,emf,png,gif,pdf,eps,ai,svg)"); } export.ExportFileName = picPath; export.Resolution = resolution; return(export); }
/// <summary> /// IExport point to certain type of instantiation /// </summary> /// <param name="pOutPath"></param> /// <returns></returns> public static IExport OutExport(string pOutPath) { IExport outExport = null; if (pOutPath.EndsWith(".jpg")) { outExport = new ExportJPEGClass(); //Information of Corordinates, BUT also NOT WELL worked as for .png IWorldFileSettings WFS = outExport as IWorldFileSettings; WFS.OutputWorldFile = true; } else if (pOutPath.EndsWith(".tif")) { outExport = new ExportTIFFClass(); //Information of Corordinates ((IExportTIFF)outExport).GeoTiff = true; } else if (pOutPath.EndsWith(".png")) { outExport = new ExportPNGClass(); } return(outExport); }
public static void ExportView(IActiveView activeView, IGeometry pGeo, int OutputResolution, int Width, int Height, string ExpPath, bool bRegion) { IExport pExport; tagRECT exportRect = new tagRECT(); IEnvelope pEnvelope = pGeo.Envelope; string sType = System.IO.Path.GetExtension(ExpPath); switch (sType) { case ".jpg": pExport = new ExportJPEGClass(); break; case ".bmp": pExport = new ExportBMPClass(); break; case ".gif": pExport = new ExportGIFClass(); break; case ".tif": pExport = new ExportTIFFClass(); break; case ".png": pExport = new ExportPNGClass(); break; case ".pdf": pExport = new ExportPDFClass(); break; default: MessageBox.Show("没有输出格式,默认到JPEG格式"); pExport = new ExportJPEGClass(); break; } pExport.ExportFileName = ExpPath; exportRect.left = 0; exportRect.top = 0; exportRect.right = Width; exportRect.bottom = Height; if (bRegion) { //删除区域导出拖出的多边形框 activeView.GraphicsContainer.DeleteAllElements(); activeView.Refresh(); } IEnvelope envelope = new EnvelopeClass(); envelope.PutCoords(exportRect.left, exportRect.top, exportRect.right, exportRect.bottom); pExport.PixelBounds = envelope; activeView.Output(pExport.StartExporting(), OutputResolution, ref exportRect, pEnvelope, null); pExport.FinishExporting(); pExport.Cleanup(); }
private void ShowOutputFileDialog(IActiveView docActiveView, int iOutputResolution = 320) { IExport docExport; IPrintAndExport docPrintExport; IWorldFileSettings worldFileSetting; //string localFilePath, fileNameExt, newFileName, FilePath; SaveFileDialog sfd = new SaveFileDialog(); //设置对话框标题 sfd.Title = "地图输出"; //设置文件类型 sfd.Filter = "EMF(*.emf)|*.emf|AI (*.ai) |*.ai|PDF (*.pdf)|*.pdf |SVG (*.svg)| *.svg|TIFF(*.tif)|*.tif|JPEG (*.jpg)| *.jpg|PNG (*.png)| *.png"; //设置默认文件类型显示顺序 sfd.FilterIndex = 5; //保存对话框是否记忆上次打开的目录 sfd.RestoreDirectory = true; //设置默认的文件名 int start = m_mapDocumentName.LastIndexOf("\\"); int end = m_mapDocumentName.LastIndexOf("."); sfd.FileName = m_mapDocumentName.Substring(start + 1, end - start - 1); //点了保存按钮进入 if (sfd.ShowDialog() == DialogResult.OK && sfd.FileName != "") { switch (sfd.FilterIndex) { case 1: { docExport = new ExportEMFClass(); break; } case 2: { docExport = new ExportAIClass(); break; } case 3: { docExport = new ExportPDFClass(); break; } case 4: { docExport = new ExportSVGClass(); break; } case 5: { docExport = new ExportTIFFClass(); IExportTIFF exportTiff = docExport as IExportTIFF; exportTiff.GeoTiff = true; exportTiff.CompressionType = esriTIFFCompression.esriTIFFCompressionLZW; break; } //对于TIFF格式导出带有地理配准的文件 case 6: { docExport = new ExportJPEGClass(); break; } case 7: { docExport = new ExportPNGClass(); break; } default: { docExport = new ExportTIFFClass(); break; } } docPrintExport = new PrintAndExportClass(); worldFileSetting = docExport as IWorldFileSettings; string localFilePath = sfd.FileName.ToString(); //获得文件路径 //string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1); //获取文件名,不带路径 docExport.ExportFileName = localFilePath; docPrintExport.Export(docActiveView, docExport, iOutputResolution, true, null); } }
/// <summary> /// Occurs when this command is clicked /// </summary> public override void OnClick() { // TODO: Add ControlsExportMapCommandClass.OnClick implementation if (m_hookHelper == null) { return; } try { SaveFileDialog saveFileDlg = new SaveFileDialog(); saveFileDlg.Filter = "(*jpeg)|*jpeg|(*.tif)|*tif|(*.pdf)|*.pdf|(*.bmp)|*.bmp|(*.gif)|*.gif|(*.png)|*.png"; if (saveFileDlg.ShowDialog() == DialogResult.OK) { IExport export = null; if (1 == saveFileDlg.FilterIndex) { export = new ExportJPEGClass(); export.ExportFileName = saveFileDlg.FileName + ".jpeg"; } else if (2 == saveFileDlg.FilterIndex) { export = new ExportTIFFClass(); export.ExportFileName = saveFileDlg.FileName + ".tif"; } else if (3 == saveFileDlg.FilterIndex) { export = new ExportPDFClass(); export.ExportFileName = saveFileDlg.FileName /* + ".pdf"*/; } else if (4 == saveFileDlg.FilterIndex) { export = new ExportBMPClass(); export.ExportFileName = saveFileDlg.FileName /* + ".bmp"*/; } else if (5 == saveFileDlg.FilterIndex) { export = new ExportGIFClass(); export.ExportFileName = saveFileDlg.FileName /* + ".gif"*/; } else if (6 == saveFileDlg.FilterIndex) { export = new ExportPNGClass(); export.ExportFileName = saveFileDlg.FileName /* + ".png"*/; } int res = 96; export.Resolution = res; tagRECT exportRECT = (m_hookHelper.Hook as IMapControl3).ActiveView.ExportFrame; IEnvelope pENV = new EnvelopeClass(); pENV.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom); export.PixelBounds = pENV; int Hdc = export.StartExporting(); IEnvelope pVisibleBounds = null; ITrackCancel pTrack = null; (m_hookHelper.Hook as IMapControl3).ActiveView.Output(Hdc, (int)export.Resolution, ref exportRECT, pVisibleBounds, pTrack); Application.DoEvents(); export.FinishExporting(); export.Cleanup(); } } catch { } }
//使用静态方法提高代码的复用性 public static void ExportView(IActiveView ActiveView, int _resolution, IGeometry pGeo, int Width, int Height, string pathtxt, bool bRegion) { IExport pExort = null; tagRECT pRect = new tagRECT(); IEnvelope pEnvelope = pGeo.Envelope; string outputType = System.IO.Path.GetExtension(pathtxt).ToLower(); switch (outputType) { case ".jpg": pExort = new ExportJPEGClass(); break; case ".bmp": pExort = new ExportBMPClass(); break; case ".gif": pExort = new ExportGIFClass(); break; case ".tif": pExort = new ExportTIFFClass(); break; case ".png": pExort = new ExportPNGClass(); break; case ".pdf": pExort = new ExportPDFClass(); break; default: MessageBox.Show("未设置输出格式,默认为jpg格式", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); pExort = new ExportJPEGClass(); break; } pExort.ExportFileName = pathtxt; pRect.left = 0; pRect.top = 0; pRect.right = Width; pRect.bottom = Height; // if (bRegion) { ActiveView.GraphicsContainer.DeleteAllElements(); ActiveView.Refresh(); } //获取输出影像范围的矩形大小 IEnvelope envelope = new EnvelopeClass(); envelope.PutCoords((double)pRect.left, (double)pRect.top, (double)pRect.right, (double)pRect.bottom); pExort.PixelBounds = envelope; ActiveView.Output(pExort.StartExporting(), _resolution, ref pRect, pEnvelope, null); pExort.FinishExporting(); pExort.Cleanup(); }
/// <summary> /// 导出地图 /// </summary> /// <param name="activeView">当前地图</param> /// <param name="geo">几何图形</param> /// <param name="OutputResolution">输出分辨率</param> /// <param name="width">宽</param> /// <param name="height">高</param> /// <param name="expPath">输出路径</param> /// <param name="isRegion">是否时范围输出</param> public static void ExportView(IActiveView activeView, IGeometry geo, int OutputResolution, int width, int height, string expPath, bool isRegion) { IExport export = null; IEnvelope envelope = geo.Envelope; string outputType = System.IO.Path.GetExtension(expPath); switch (outputType) { case ".jpg": export = new ExportJPEGClass(); break; case ".bmp": export = new ExportBMPClass(); break; case ".gif": export = new ExportGIFClass(); break; case ".tif": export = new ExportTIFFClass(); break; case ".png": export = new ExportPNGClass(); break; case ".pdf": export = new ExportPDFClass(); break; default: MessageBox.Show("没有输出格式,默认到JPEG格式"); export = new ExportJPEGClass(); break; } export.ExportFileName = expPath; tagRECT rect = new tagRECT(); rect.top = 0; rect.left = 0; rect.right = width; rect.bottom = height; if (isRegion) { activeView.GraphicsContainer.DeleteAllElements(); activeView.Refresh(); } IEnvelope env = new EnvelopeClass(); env.PutCoords((double)rect.left, (double)rect.top, (double)rect.right, (double)rect.bottom); export.PixelBounds = env; activeView.Output(export.StartExporting(), OutputResolution, ref rect, envelope, null); export.FinishExporting(); export.Cleanup(); }
/// <summary> /// 全域导出 /// </summary> /// <param name="OutputResolution">输出分辨率</param> /// <param name="ExpPath">输出路径</param> /// <param name="view">视图</param> public static void ExportActiveView(int OutputResolution, string ExpPath, IActiveView view) { IExport pExport = null; tagRECT exportRect; IEnvelope envelope2 = view.Extent; int num = (int)Math.Round(view.ScreenDisplay.DisplayTransformation.Resolution); string sType = System.IO.Path.GetExtension(ExpPath); switch (sType) { case ".jpg": pExport = new ExportJPEGClass(); break; case ".bmp": pExport = new ExportBMPClass(); break; case ".gif": pExport = new ExportGIFClass(); break; case ".tif": pExport = new ExportTIFFClass(); break; case ".png": pExport = new ExportPNGClass(); break; case ".pdf": pExport = new ExportPDFClass(); break; default: MessageBox.Show("没有输出格式,默认到JPEG格式"); pExport = new ExportJPEGClass(); break; } pExport.ExportFileName = ExpPath; exportRect.left = 0; exportRect.top = 0; exportRect.right = (int)Math.Round((double)(view.ExportFrame.right * (((double)OutputResolution) / ((double)num)))); exportRect.bottom = (int)Math.Round((double)(view.ExportFrame.bottom * (((double)OutputResolution) / ((double)num)))); IEnvelope envelope = new EnvelopeClass(); envelope.PutCoords((double)exportRect.left, (double)exportRect.top, (double)exportRect.right, (double)exportRect.bottom); pExport.PixelBounds = envelope; view.Output(pExport.StartExporting(), OutputResolution, ref exportRect, envelope2, null); pExport.FinishExporting(); pExport.Cleanup(); }
/// <summary> /// 设置导出路径,获取导出工具 /// </summary> /// <returns></returns> public static IExport GetExport() { string filter = "JPGE 文件(*.jpeg)|*.jpeg|BMP 文件(*.bmp)|*.bmp|GIF 文件(*.gif)|*.gif|TIF 文件(*.tif)|*.tif|PNG 文件(*.png)|*.png|PDF 文件(*.pdf)|*.pdf"; string outPath = FileAndFolderDialog.GetSaveFilePath(filter); if (string.IsNullOrEmpty(outPath)) { return(null); } IExport pExport; string sType = System.IO.Path.GetExtension(outPath); switch (sType) { case ".jpeg": pExport = new ExportJPEGClass(); break; case ".bmp": pExport = new ExportBMPClass(); break; case ".gif": pExport = new ExportGIFClass(); break; case ".tif": pExport = new ExportTIFFClass(); break; case ".png": pExport = new ExportPNGClass(); break; case ".pdf": pExport = new ExportPDFClass(); break; default: MessageBox.Show("没有输出格式,默认到JPEG格式"); pExport = new ExportJPEGClass(); break; } pExport.ExportFileName = outPath; return(pExport); }
/// <summary> /// 输出图片 /// </summary> /// <param name="MapCtrl">AxMapControl控件</param> public static void ExportFile(AxMapControl MapCtrl) { try { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "(*.tif)|*tif|(*jpg)|*jpg|(*.pdf)|*.pdf|(*.bmp)|*.bmp"; if (sfd.ShowDialog() == DialogResult.OK) { IExport pExport = null; if (1 == sfd.FilterIndex) { pExport = new ExportTIFFClass(); pExport.ExportFileName = sfd.FileName + ".tif"; } else if (2 == sfd.FilterIndex) { pExport = new ExportJPEGClass(); pExport.ExportFileName = sfd.FileName + ".jpeg"; } else if (3 == sfd.FilterIndex) { pExport = new ExportPDFClass(); pExport.ExportFileName = sfd.FileName + ".pdf"; } else if (4 == sfd.FilterIndex) { pExport = new ExportBMPClass(); pExport.ExportFileName = sfd.FileName + ".bmp"; } //pExport.ExportFileName = sfd.FileName; int res = 96; pExport.Resolution = res; tagRECT exportRECT = MapCtrl.ActiveView.ExportFrame; IEnvelope pENV = new EnvelopeClass(); pENV.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom); pExport.PixelBounds = pENV; int Hdc = pExport.StartExporting(); IEnvelope pVisibleBounds = null; ITrackCancel pTrack = null; MapCtrl.ActiveView.Output(Hdc, (int)pExport.Resolution, ref exportRECT, pVisibleBounds, pTrack); System.Windows.Forms.Application.DoEvents(); pExport.FinishExporting(); pExport.Cleanup(); } } catch { } }
/// <summary> /// Occurs when this command is clicked /// </summary> public override void OnClick() { // TODO: Add ControlsExportMapCommandClass.OnClick implementation if (m_hookHelper == null) return; try { SaveFileDialog saveFileDlg = new SaveFileDialog(); saveFileDlg.Filter = "(*jpeg)|*jpeg|(*.tif)|*tif|(*.pdf)|*.pdf|(*.bmp)|*.bmp|(*.gif)|*.gif|(*.png)|*.png"; if (saveFileDlg.ShowDialog() == DialogResult.OK) { IExport export = null; if (1 == saveFileDlg.FilterIndex) { export = new ExportJPEGClass(); export.ExportFileName = saveFileDlg.FileName + ".jpeg"; } else if (2 == saveFileDlg.FilterIndex) { export = new ExportTIFFClass(); export.ExportFileName = saveFileDlg.FileName + ".tif"; } else if (3 == saveFileDlg.FilterIndex) { export = new ExportPDFClass(); export.ExportFileName = saveFileDlg.FileName/* + ".pdf"*/; } else if (4 == saveFileDlg.FilterIndex) { export = new ExportBMPClass(); export.ExportFileName = saveFileDlg.FileName/* + ".bmp"*/; } else if (5 == saveFileDlg.FilterIndex) { export = new ExportGIFClass(); export.ExportFileName = saveFileDlg.FileName/* + ".gif"*/; } else if (6 == saveFileDlg.FilterIndex) { export = new ExportPNGClass(); export.ExportFileName = saveFileDlg.FileName/* + ".png"*/; } int res = 96; export.Resolution = res; tagRECT exportRECT = (m_hookHelper.Hook as IMapControl3).ActiveView.ExportFrame; IEnvelope pENV = new EnvelopeClass(); pENV.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom); export.PixelBounds = pENV; int Hdc = export.StartExporting(); IEnvelope pVisibleBounds = null; ITrackCancel pTrack = null; (m_hookHelper.Hook as IMapControl3).ActiveView.Output(Hdc, (int)export.Resolution, ref exportRECT, pVisibleBounds, pTrack); Application.DoEvents(); export.FinishExporting(); export.Cleanup(); } } catch { } }
private void ExportActiveViewParameterized(long iOutputResolution, long lResampleRatio, string ExportType, string sOutputDir, 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. */ // using predefined static member IActiveView docActiveView = ArcMap.Document.ActiveView; IExport docExport; IPrintAndExport docPrintExport; IOutputRasterSettings RasterSettings; string sNameRoot; bool bReenable = false; 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(); } 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 == "JPEG") { docExport = new ExportJPEGClass(); } else { MessageBox.Show("Unsupported export type " + ExportType + ", defaulting to EMF."); ExportType = "EMF"; docExport = new ExportEMFClass(); } docPrintExport = new PrintAndExportClass(); //set the name root for the export sNameRoot = "ExportActiveViewSampleOutput"; //set the export filename (which is the nameroot + the appropriate file extension) docExport.ExportFileName = sOutputDir + sNameRoot + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0]; //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" // check if export is vector or raster if (docExport is IOutputRasterSettings) { // for vector formats, assign the desired ResampleRatio to control drawing of raster layers at export time RasterSettings = (IOutputRasterSettings)docExport; RasterSettings.ResampleRatio = (int)lResampleRatio; // NOTE: for raster formats output quality of the DISPLAY is set to 1 for image export // formats by default which is what should be used } docPrintExport.Export(docActiveView, docExport, iOutputResolution, bClipToGraphicsExtent, null); MessageBox.Show("Finished exporting " + sOutputDir + sNameRoot + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0] + ".", "Export Active View Sample"); 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"); } } }
private void ExportActiveViewParameterized(long iOutputResolution, long lResampleRatio, string ExportType, string sOutputDir, string sOutputFileName, Boolean bClipToGraphicsExtent) { IActiveView 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; long iScreenResolution; bool bReenable = false; IEnvelope docGraphicsExtentEnv; IUnitConverter pUnitConvertor; if (GetFontSmoothing()) { bReenable = true; DisableFontSmoothing(); if (GetFontSmoothing()) { return; } } if (ExportType == "PDF") { docExport = new ExportPDFClass(); } 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 == "JPEG") { docExport = new ExportJPEGClass(); } else { MessageBox.Show("不支持的数据类型 " + ExportType + ", 默认导出为EMF格式的数据"); ExportType = "EMF"; docExport = new ExportEMFClass(); } docOutputRasterSettings = docActiveView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings; iPrevOutputImageQuality = docOutputRasterSettings.ResampleRatio; if (docExport is IExportImage) { SetOutputQuality(docActiveView, 1); } else { SetOutputQuality(docActiveView, lResampleRatio); } docExport.ExportFileName = sOutputFileName + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0]; tmpDC = GetDC(0); iScreenResolution = GetDeviceCaps((int)tmpDC, 88); //88 is the win32 const for Logical pixels/inch in X) ReleaseDC(0, (int)tmpDC); frmSetResolution mSetResolution = new frmSetResolution(iOutputResolution); mSetResolution.ShowDialog(); iOutputResolution = mSetResolution.m_Resolution; docExport.Resolution = iOutputResolution; if (docActiveView is IPageLayout) { DisplayBounds = docActiveView.ExportFrame; docGraphicsExtentEnv = GetGraphicsExtent(docActiveView); } else { 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(); 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; exportRECT.bottom = (int)(PixelBoundsEnv.YMax) + 1; exportRECT.left = (int)(PixelBoundsEnv.XMin); exportRECT.top = (int)(PixelBoundsEnv.YMin); exportRECT.right = (int)(PixelBoundsEnv.XMax) + 1; docMapExtEnv = docGraphicsExtentEnv; } else { double tempratio = iOutputResolution / iScreenResolution; double tempbottom = DisplayBounds.bottom * tempratio; double tempright = DisplayBounds.right * tempratio; exportRECT.bottom = (int)Math.Truncate(tempbottom); exportRECT.left = 0; exportRECT.top = 0; exportRECT.right = (int)Math.Truncate(tempright); PixelBoundsEnv.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom); docMapExtEnv = null; } docExport.PixelBounds = PixelBoundsEnv; try { hdc = docExport.StartExporting(); docActiveView.Output((int)hdc, (int)docExport.Resolution, ref exportRECT, docMapExtEnv, null); docExport.FinishExporting(); docExport.Cleanup(); MessageBox.Show("成功导出 " + sOutputDir + sOutputFileName + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0] + ".", "输出地图图片", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch { MessageBox.Show("输出地图图片过程中出现问题!", "输出地图图片", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } finally { SetOutputQuality(docActiveView, iPrevOutputImageQuality); if (bReenable) { EnableFontSmoothing(); bReenable = false; if (!GetFontSmoothing()) { MessageBox.Show("Unable to reenable Font Smoothing", "Font Smoothing error"); } } docMapExtEnv = null; PixelBoundsEnv = null; } }
private void button2_Click(object sender, EventArgs e) { if (System.IO.File.Exists(textBoxFileName.Text.ToString()) == true) { MessageBox.Show("该文件已经存在,请重新命名!"); textBoxFileName.SelectAll(); } else { IExport pExport = null; IWorldFileSettings pWorldFile = null; IExportImage pExportType; IEnvelope pDriverBounds = null; ESRI.ArcGIS.esriSystem.tagRECT userRECT = new ESRI.ArcGIS.esriSystem.tagRECT(); IEnvelope pEnv = new EnvelopeClass(); string FilePath = this.m_strFileName; string[] strFileName = FilePath.Split('.'); string strFileType = strFileName[1]; switch (strFileType) { case "jpg": pExport = new ExportJPEGClass(); break; case "bmp": pExport = new ExportBMPClass(); break; case "gif": pExport = new ExportGIFClass(); break; case "tif": pExport = new ExportTIFFClass(); break; case "png": pExport = new ExportPNGClass(); break; case "emf": pExport = new ExportEMFClass(); break; case "pdf": pExport = new ExportPDFClass(); break; case ".ai": pExport = new ExportAIClass(); break; case "svg": pExport = new ExportSVGClass(); break; default: pExport = new ExportJPEGClass(); break; } pExport.ExportFileName = this.m_strFileName; pExport.Resolution = Convert.ToInt32(numUDresolution.Value); pExportType = pExport as IExportImage; pExportType.ImageType = esriExportImageType.esriExportImageTypeTrueColor; pEnv = m_pageLayoutControl.ActiveView.Extent; pWorldFile = (IWorldFileSettings)pExport; pWorldFile.MapExtent = pEnv; pWorldFile.OutputWorldFile = false; userRECT.top = 0; userRECT.left = 0; userRECT.right = Convert.ToInt32(txtBoxWidth.Text); userRECT.bottom = Convert.ToInt32(txtBoxHeight.Text); pDriverBounds = new EnvelopeClass(); pDriverBounds.PutCoords(userRECT.top, userRECT.bottom, userRECT.right, userRECT.top); pExport.PixelBounds = pDriverBounds; ITrackCancel pTrackCancel = new TrackCancelClass(); m_pageLayoutControl.ActiveView.Output(pExport.StartExporting(), Convert.ToInt32(numUDresolution.Value), ref userRECT, m_pageLayoutControl.ActiveView.Extent, pTrackCancel); pExport.FinishExporting(); MessageBox.Show("打印图片保存成功!", "保存", MessageBoxButtons.OK); this.Close(); } }
private void btnPrint_Click(object sender, EventArgs e) { //创建保存文件对话框的实例 SaveFileDialog SaveFile = new SaveFileDialog(); //根据掩码文本框对对话框初始化 switch (comboBox1.SelectedIndex) { case 0: SaveFile.Filter = "bmp文件(*.bmp)|*.bmp"; SaveFile.Title = "保存bmb文件"; break; case 1: SaveFile.Filter = "emf文件(*.emf)|*.emf"; SaveFile.Title = "保存emp文件"; break; case 2: SaveFile.Filter = "gif文件(*.gif)|*.gif"; SaveFile.Title = "保存gif文件"; break; case 3: SaveFile.Filter = "jpg文件(*.jpeg)|*.jpeg"; SaveFile.Title = "保存jpg文件"; break; case 4: SaveFile.Filter = "PNG文件(*.png)|*.png"; SaveFile.Title = "保存png文件"; break; case 5: SaveFile.Filter = "TIFF文件(*.TIFF)|*.TIFF"; SaveFile.Title = "保存tiff文件"; break; case 6: SaveFile.Filter = "PDF文件(*.PDF)|*.pdf"; SaveFile.Title = "保存pdf文件"; break; } if (SaveFile.ShowDialog() == DialogResult.OK) { //打开保存文件对话框 并选择路径和文件名 SaveFile.AddExtension = true; //设置为自动补充文件扩展名 IExport pExport; switch (comboBox1.SelectedIndex) //根据掩码文本框的索引值穿件不同的pExport实例并调用Export方法 { case 0: pExport = new ExportBMPClass(); Export(pExport, SaveFile.FileName); break; case 1: pExport = new ExportEMFClass(); Export(pExport, SaveFile.FileName); break; case 2: pExport = new ExportGIFClass(); Export(pExport, SaveFile.FileName); break; case 3: pExport = new ExportJPEGClass(); Export(pExport, SaveFile.FileName); break; case 4: pExport = new ExportPNGClass(); Export(pExport, SaveFile.FileName); break; case 5: pExport = new ExportTIFFClass(); Export(pExport, SaveFile.FileName); break; case 6: pExport = new ExportPDFClass(); Export(pExport, SaveFile.FileName); break; } } }
public void ExportActiveViewParameterized(IActiveView docActiveView, 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. */ // using predefined static member IExport docExport; IPrintAndExport docPrintExport; IOutputRasterSettings RasterSettings; string sNameRoot; bool bReenable = false; 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(); } 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 == "JPEG") { docExport = new ExportJPEGClass(); } else { MessageBox.Show("Unsupported export type " + ExportType + ", defaulting to EMF."); ExportType = "EMF"; docExport = new ExportEMFClass(); } docPrintExport = new PrintAndExportClass(); //set the name root for the export sNameRoot = "ExportActiveViewSampleOutput"; //set the export filename (which is the nameroot + the appropriate file extension) //docExport.ExportFileName = sOutputDir + sNameRoot + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0]; docExport.ExportFileName = sOutputName; //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" // check if export is vector or raster if (docExport is IOutputRasterSettings) { // for vector formats, assign the desired ResampleRatio to control drawing of raster layers at export time RasterSettings = (IOutputRasterSettings)docExport; RasterSettings.ResampleRatio = (int)lResampleRatio; // NOTE: for raster formats output quality of the DISPLAY is set to 1 for image export // formats by default which is what should be used } docPrintExport.Export(docActiveView, docExport, iOutputResolution, bClipToGraphicsExtent, null); //MessageBox.Show("Finished exporting " + sOutputDir + sNameRoot + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0] + ".", "Export Active View Sample"); 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"); } } }
private void ExportActiveViewParameterized(long iOutputResolution, long lResampleRatio, string ExportType, string sOutputDir, string sNameRoot, Boolean bClipToGraphicsExtent, AxPageLayoutControl pageLayoutControl) { //解决文件名错误 sNameRoot = sNameRoot.Substring(1, sNameRoot.Length - 1); IActiveView docActiveView = pageLayoutControl.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()) { 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(); } 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 == "JPEG") { docExport = new ExportJPEGClass(); } else { MessageBox.Show("!!不支持的格式 " + ExportType + ", 默认导出 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 = "ExportActiveViewSampleOutput"; //set the export filename (which is the nameroot + the appropriate file extension) docExport.ExportFileName = sOutputDir + sNameRoot + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0]; tmpDC = GetDC(0); iScreenResolution = GetDeviceCaps((int)tmpDC, 88); //88 is the win32 const for Logical pixels/inch in X) 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( "成功导出地图: " + sOutputDir + sNameRoot + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0] + ".", "提示"); //set the output quality back to the previous value SetOutputQuality(docActiveView, iPrevOutputImageQuality); if (bReenable) { EnableFontSmoothing(); bReenable = false; if (!GetFontSmoothing()) { //error: cannot reenable font smoothing. MessageBox.Show("Unable to reenable Font Smoothing", "Font Smoothing error"); } } docMapExtEnv = null; PixelBoundsEnv = null; }
//Function: Export map //Date: 2019/4/3 public void ExportMap(int pOutputResolution, string pExportFileName, bool pWriteWorldFile, double pWidth, double pHeight) { IActiveView docActiveView; IExport docExport; IPrintAndExport docPrintExport; IWorldFileSettings pWorldFile = null; ESRI.ArcGIS.esriSystem.tagRECT userRECT = new ESRI.ArcGIS.esriSystem.tagRECT(); IEnvelope pEnv = new EnvelopeClass(); string pFileType; int pFileNameLength = pExportFileName.Length; if (pFileNameLength > 3) { pFileType = pExportFileName.Substring(pFileNameLength - 3, 3); } else { pFileType = pExportFileName; } switch (pFileType) { case "jpg": docExport = new ExportJPEGClass(); break; case "png": docExport = new ExportPNGClass(); break; case "tif": docExport = new ExportTIFFClass(); break; default: docExport = new ExportJPEGClass(); break; } if (miLayoutView.Checked) { docActiveView = axPageLayoutControl1.ActiveView; } else { docActiveView = axMapControl1.ActiveView; } pEnv = docActiveView.Extent; pWorldFile = (IWorldFileSettings)docExport; pWorldFile.MapExtent = pEnv; pWorldFile.OutputWorldFile = pWriteWorldFile; userRECT.left = 0; userRECT.top = 0; userRECT.right = Convert.ToInt32(pWidth); userRECT.bottom = Convert.ToInt32(pHeight); IEnvelope pDriverBounds = new EnvelopeClass(); pDriverBounds.PutCoords(userRECT.top, userRECT.bottom, userRECT.right, userRECT.top); docExport.PixelBounds = pDriverBounds; docPrintExport = new PrintAndExportClass(); docExport.ExportFileName = pExportFileName; docPrintExport.Export(docActiveView, docExport, pOutputResolution, true, null); }
/// <summary>Exports the page layout to a file.</summary> /// <param name="pageLayout">The page layout.</param> /// <param name="exportPath">The name of the output file.</param> /// <param name="exportFormat">The format of the output.</param> /// <param name="dpi">The resolution of the output files.</param> void ExportPageLayoutToFile(IPageLayout pageLayout, string exportPath, long dpi, ExportFormat exportFormat) { IExport export = null; // Get the color of the backgrounds for formats that support transparent backgrounds. IColor bgColor = null; switch (exportFormat) { case ExportFormat.GIF: case ExportFormat.PNG: IPage page = pageLayout.Page; bgColor = page.BackgroundColor; break; } // Set "export" to the proper type of Export*Class, and set parameters specific to that image type. switch (exportFormat) { case ExportFormat.AI: export = new ExportAIClass(); var ai = export as IExportAI2; //ai.EmbedFonts = true; break; case ExportFormat.EMF: export = new ExportEMFClass(); var xEmf = export as IExportEMF; //xEmf.Description = break; case ExportFormat.PS: export = new ExportPSClass(); var xPs = export as IExportPS; xPs.EmbedFonts = true; //xPs.Emulsion = //xPs.Image = //xPs.ImageCompression = esriExportImageCompression.esriExportImageCompressionNone //xPs.LanguageLevel = esriExportPSLanguageLevel.esriExportPSLevel3 break; case ExportFormat.SVG: export = new ExportSVGClass(); var xSvg = export as IExportSVG; //xSvg.Compressed = true; //xSvg.EmbedFonts = true; break; case ExportFormat.PDF: export = new ExportPDFClass(); var xPdf = export as IExportPDF; //xPdf.Compressed = true; //xPdf.EmbedFonts = true; //xPdf.ImageCompression = esriExportImageCompression.esriExportImageCompressionNone break; case ExportFormat.GIF: export = new ExportGIFClass(); IExportGIF xGif = export as IExportGIF; xGif.TransparentColor = bgColor; //xGif.BiLevelThreshold = //xGif.CompressionType = esriGIFCompression.esriGIFCompressionNone; //xGif.InterlaceMode = false; break; case ExportFormat.PNG: export = new ExportPNGClass(); IExportPNG xPng = export as IExportPNG; //xPng.BiLevelThreshold = //xPng.InterlaceMode = false; xPng.TransparentColor = bgColor; break; case ExportFormat.BMP: export = new ExportBMPClass(); IExportBMP xBmp = export as IExportBMP; //xBmp.BiLevelThreshold = //xBmp.RLECompression = true; break; case ExportFormat.JPEG: export = new ExportJPEGClass(); IExportJPEG xJpg = export as IExportJPEG; //xJpg.ProgressiveMode = false; //xJpg.Quality = 80; //The JPEG compression / image quality. Range (0..100). Default is 100 (no compression / best quality). break; case ExportFormat.TIFF: export = new ExportTIFFClass(); IExportTIFF xTif = export as IExportTIFF; //xTif.BiLevelThreshold = //xTif.CompressionType = //xTif.GeoTiff = true; //The JPEG or Deflate (depending on the Compression type) compression / image quality. Range (0..100). Default is 100 (no compression / best quality). //xTif.JPEGOrDeflateQuality = 100; break; default: const string MSG_FMT = "Support for \"{0}\" export is not yet implemented."; string message = string.Format(MSG_FMT, Enum.GetName(typeof(ExportFormat), exportFormat)); throw new NotImplementedException(message); } IEnvelope pixelEnv = new EnvelopeClass(); IEnvelope pageExt = GetPageExtent(pageLayout); IPoint upperRight = pageExt.UpperRight; pixelEnv.PutCoords(0, 0, dpi * upperRight.X, dpi * upperRight.Y); export.PixelBounds = pixelEnv; export.Resolution = dpi; export.ExportFileName = exportPath; // //(device coordinates origin is upper left, ypositive is down) tagRECT expRect; expRect.left = (int)export.PixelBounds.LowerLeft.X; expRect.bottom = (int)export.PixelBounds.UpperRight.Y; expRect.right = (int)export.PixelBounds.UpperRight.X; expRect.top = (int)export.PixelBounds.LowerLeft.Y; _app.StatusBar.set_Message(0, string.Format("exporting \"{0}\"", exportPath)); long hdc = export.StartExporting(); IActiveView av = (IActiveView)pageLayout; av.Output((int)hdc, (int)dpi, ref expRect, null, null); export.FinishExporting(); export.Cleanup(); }
/// <summary> /// 导出视图为图片 /// </summary> /// <param name="activeView">地图视图</param> /// <param name="pathFileName">导出文件名称</param> /// <param name="bUseHiDpi">是否使用高分辨率,默认为true</param> public static void ExportActiveView(IActiveView activeView, string pathFileName, bool bUseHiDpi = true) { if (activeView == null || string.IsNullOrEmpty(pathFileName)) { return; } IExport export = null; if (pathFileName.EndsWith(".jpg")) { export = new ExportJPEGClass(); } else if (pathFileName.EndsWith(".tiff")) { export = new ExportTIFFClass(); } else if (pathFileName.EndsWith(".bmp")) { export = new ExportBMPClass(); } else if (pathFileName.EndsWith(".emf")) { export = new ExportEMFClass(); } else if (pathFileName.EndsWith(".png")) { export = new ExportPNGClass(); } else if (pathFileName.EndsWith(".gif")) { export = new ExportGIFClass(); } else if (pathFileName.EndsWith(".pdf")) { export = new ExportPDFClass(); } //DateTime dt = DateTime.Now; //string strTime = string.Format("{0:yyyyMMddHHmmss}", dt); //string fileName = System.IO.Path.GetFileNameWithoutExtension(pathFileName); //string mergeStr = string.Format(System.IO.Path.GetDirectoryName(pathFileName) + "\\" + fileName + strTime+System.IO.Path.GetExtension(pathFileName)); 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 = bUseHiDpi ? 300 : 96; export.Resolution = outputResolution; 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 IEnvelope envelope = new EnvelopeClass(); envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom); export.PixelBounds = envelope; int hDC = export.StartExporting(); activeView.Output(hDC, (System.Int16)export.Resolution, ref exportRECT, null, null); // Explicit Cast and 'ref' keyword needed export.FinishExporting(); export.Cleanup(); }
//输出当前地图为图片 public static string ExportImage(IActiveView pActiveView) { if (pActiveView == null) { return(null); } try { SaveFileDialog pSaveFileDialog = new SaveFileDialog(); pSaveFileDialog.Filter = "JPEG(*.jpg)|*.jpg|AI(*.ai)|*.ai|BMP(*.BMP)|*.bmp|EMF(*.emf)|*.emf|GIF(*.gif)|*.gif|PDF(*.pdf)|*.pdf|PNG(*.png)|*.png|EPS(*.eps)|*.eps|SVG(*.svg)|*.svg|TIFF(*.tif)|*.tif"; pSaveFileDialog.Title = "输出地图"; pSaveFileDialog.RestoreDirectory = true; pSaveFileDialog.FilterIndex = 1; if (pSaveFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) { return(null); } string FileName = pSaveFileDialog.FileName; IExport pExporter = null; switch (pSaveFileDialog.FilterIndex) { case 1: pExporter = new ExportJPEGClass(); break; case 2: pExporter = new ExportBMPClass(); break; case 3: pExporter = new ExportEMFClass(); break; case 4: pExporter = new ExportGIFClass(); break; case 5: pExporter = new ExportAIClass(); break; case 6: pExporter = new ExportPDFClass(); break; case 7: pExporter = new ExportPNGClass(); break; case 8: pExporter = new ExportPSClass(); break; case 9: pExporter = new ExportSVGClass(); break; case 10: pExporter = new ExportTIFFClass(); break; default: MessageBox.Show("输出格式错误"); return(null); } IEnvelope pEnvelope = new EnvelopeClass(); ITrackCancel pTrackCancel = new CancelTrackerClass(); tagRECT ptagRECT; ptagRECT = pActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame(); int pResolution = (int)(pActiveView.ScreenDisplay.DisplayTransformation.Resolution); pEnvelope.PutCoords(ptagRECT.left, ptagRECT.bottom, ptagRECT.right, ptagRECT.top); pExporter.Resolution = pResolution; pExporter.ExportFileName = FileName; pExporter.PixelBounds = pEnvelope; pActiveView.Output(pExporter.StartExporting(), pResolution, ref ptagRECT, pActiveView.Extent, pTrackCancel); pExporter.FinishExporting(); //释放资源 pSaveFileDialog.Dispose(); System.Runtime.InteropServices.Marshal.ReleaseComObject(pExporter); return(FileName); } catch { return(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(); }
// Exports a given page layout or map frame to a variety of image formats, returns the image file path public static string exportImage(IMxDocument pMxDoc, string exportType, string dpi, string pathDocumentName, string mapFrameName) { // Define the activeView as either the page layout or the map frame // If the mapFrameName variable is null then the activeView is the page, otherwise it is set to the map frame name specified IMap pMap; IActiveView pActiveView = null; IMaps pMaps = pMxDoc.Maps; // Also construct output filename depending on the activeView / mapFrame input string pathFileName = string.Empty; if (mapFrameName == null) { pActiveView = pMxDoc.ActiveView; pathFileName = @pathDocumentName + "-" + dpi.ToString() + "dpi." + exportType; } else if (mapFrameName != null && PageLayoutProperties.detectMapFrame(pMxDoc, mapFrameName)) { for (int i = 0; i <= pMaps.Count - 1; i++) { pMap = pMaps.get_Item(i); if (pMap.Name == mapFrameName) { pActiveView = pMap as IActiveView; } } pathFileName = @pathDocumentName + "-mapframe-" + dpi.ToString() + "dpi." + exportType; } else { return(null); } //Declare the export variable and set the file and path from the parameters IExport docExport; //parameter check if (pActiveView == null) { return(null); } // The Export*Class() type initializes a new export class of the desired type. if (exportType == "pdf") { docExport = new ExportPDFClass(); } 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 == "jpeg") { IExportJPEG m_export; docExport = new ExportJPEGClass(); if (docExport is IExportJPEG) { m_export = (IExportJPEG)docExport; m_export.ProgressiveMode = false; //hardcoded progressive mode value here m_export.Quality = 80; //hardcoded quality value here docExport = (IExport)m_export; } } else { return(pathFileName); } docExport.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 = Convert.ToInt32(dpi); docExport.Resolution = outputResolution; // If input type is map frame calculate the export rectangle tagRECT exportRECT; // This is a structure exportRECT.left = 0; exportRECT.top = 0; exportRECT.right = pActiveView.ExportFrame.right * (outputResolution / screenResolution); exportRECT.bottom = pActiveView.ExportFrame.bottom * (outputResolution / screenResolution); // Set up the PixelBounds envelope to match the exportRECT IEnvelope envelope = new EnvelopeClass(); envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom); docExport.PixelBounds = envelope; try { System.Int32 hDC = docExport.StartExporting(); pActiveView.Output(hDC, (System.Int16)docExport.Resolution, ref exportRECT, null, null); // Explicit Cast and 'ref' keyword needed docExport.FinishExporting(); docExport.Cleanup(); } catch (Exception e) { Debug.WriteLine("Error writing to file, probably a file permissions error. Check the exception message below for details."); Debug.WriteLine(e.Message); } //Return the path of the file return(pathFileName); }
private IExport CreateExport(string PathName, int iResampleRatio) { IExport docExport; string sExtionsName = PathName.Substring(PathName.LastIndexOf(".") + 1); sExtionsName = sExtionsName.ToUpper(); switch (sExtionsName) { case "PDF": docExport = new ExportPDFClass(); break; case "EPS": docExport = new ExportPSClass(); break; case "AI": docExport = new ExportAIClass(); break; case "BMP": docExport = new ExportBMPClass(); break; case "TIFF": docExport = new ExportTIFFClass(); break; case "TIF": docExport = new ExportTIFFClass(); break; case "SVG": docExport = new ExportSVGClass(); break; case "PNG": docExport = new ExportPNGClass(); break; case "GIF": docExport = new ExportGIFClass(); break; case "EMF": docExport = new ExportEMFClass(); break; case "JPEG": docExport = new ExportJPEGClass(); break; case "JPG": docExport = new ExportJPEGClass(); break; default: docExport = new ExportJPEGClass(); break; } if (docExport is IOutputRasterSettings) { if (iResampleRatio < 1) iResampleRatio = 1; if (iResampleRatio > 5) iResampleRatio = 5; IOutputRasterSettings RasterSettings = (IOutputRasterSettings)docExport; RasterSettings.ResampleRatio = iResampleRatio; } docExport.ExportFileName = PathName; return docExport; }
private void ExportActiveViewParameterized(long iOutputResolution, long lResampleRatio, string ExportType, string sOutputDir,string sOutputFileName, Boolean bClipToGraphicsExtent) { IActiveView 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; long iScreenResolution; bool bReenable = false; IEnvelope docGraphicsExtentEnv; IUnitConverter pUnitConvertor; if (GetFontSmoothing()) { bReenable = true; DisableFontSmoothing(); if (GetFontSmoothing()) { return; } } if (ExportType == "PDF") { docExport = new ExportPDFClass(); } 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 == "JPEG") { docExport = new ExportJPEGClass(); } else { MessageBox.Show("��֧�ֵ��������� " + ExportType + ", Ĭ�ϵ���ΪEMF��ʽ������"); ExportType = "EMF"; docExport = new ExportEMFClass(); } docOutputRasterSettings = docActiveView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings; iPrevOutputImageQuality = docOutputRasterSettings.ResampleRatio; if (docExport is IExportImage) { SetOutputQuality(docActiveView, 1); } else { SetOutputQuality(docActiveView, lResampleRatio); } docExport.ExportFileName = sOutputFileName + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0]; tmpDC = GetDC(0); iScreenResolution = GetDeviceCaps((int)tmpDC, 88); //88 is the win32 const for Logical pixels/inch in X) ReleaseDC(0, (int)tmpDC); frmSetResolution mSetResolution = new frmSetResolution(iOutputResolution); mSetResolution.ShowDialog(); iOutputResolution = mSetResolution.m_Resolution ; docExport.Resolution = iOutputResolution; if (docActiveView is IPageLayout) { DisplayBounds = docActiveView.ExportFrame; docGraphicsExtentEnv = GetGraphicsExtent(docActiveView); } else { 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(); 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; exportRECT.bottom = (int)(PixelBoundsEnv.YMax) + 1; exportRECT.left = (int)(PixelBoundsEnv.XMin); exportRECT.top = (int)(PixelBoundsEnv.YMin); exportRECT.right = (int)(PixelBoundsEnv.XMax) + 1; docMapExtEnv = docGraphicsExtentEnv; } else { double tempratio = iOutputResolution / iScreenResolution; double tempbottom = DisplayBounds.bottom * tempratio; double tempright = DisplayBounds.right * tempratio; exportRECT.bottom = (int)Math.Truncate(tempbottom); exportRECT.left = 0; exportRECT.top = 0; exportRECT.right = (int)Math.Truncate(tempright); PixelBoundsEnv.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom); docMapExtEnv = null; } docExport.PixelBounds = PixelBoundsEnv; try { hdc = docExport.StartExporting(); docActiveView.Output((int)hdc, (int)docExport.Resolution, ref exportRECT, docMapExtEnv, null); docExport.FinishExporting(); docExport.Cleanup(); MessageBox.Show("�ɹ����� " + sOutputDir + sOutputFileName + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0] + ".", "�����ͼͼƬ", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch { MessageBox.Show("�����ͼͼƬ�����г������⣡", "�����ͼͼƬ", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } finally { SetOutputQuality(docActiveView, iPrevOutputImageQuality); if (bReenable) { EnableFontSmoothing(); bReenable = false; if (!GetFontSmoothing()) { MessageBox.Show("Unable to reenable Font Smoothing", "Font Smoothing error"); } } docMapExtEnv = null; PixelBoundsEnv = null; } }
public void exportMapa(string ExportType, long iOutputResolution, long lResampleRatio, Boolean bClipToGraphicsExtent, string nombreArchivo,string sOutputDir) { IActiveView docActiveView = ArcMap.Document.ActiveView; IExport docExport; IPrintAndExport docPrintExport; IOutputRasterSettings RasterSettings; bool bReenable = false; if (GetFontSmoothing()) { /* font smoothing is on, disable it and set the flag to reenable it later. */ bReenable = true; DisableFontSmoothing(); if (GetFontSmoothing()) return; } // The Export*Class() type initializes a new export class of the desired type. if (ExportType == "PDF") { docExport = new ExportPDFClass(); } 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 == "JPEG") { docExport = new ExportJPEGClass(); } else { MessageBox.Show("Unsupported export type " + ExportType + ", defaulting to EMF."); ExportType = "EMF"; docExport = new ExportEMFClass(); } docPrintExport = new PrintAndExportClass(); //set the export filename (which is the nameroot + the appropriate file extension) docExport.ExportFileName = sOutputDir + nombreArchivo + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0]; if (docExport is IOutputRasterSettings){ RasterSettings = (IOutputRasterSettings)docExport; RasterSettings.ResampleRatio = (int)lResampleRatio; } docPrintExport.Export(docActiveView, docExport, iOutputResolution, bClipToGraphicsExtent, null); if (bReenable) { /* reenable font smoothing if we disabled it before */ EnableFontSmoothing(); bReenable = false; if (!GetFontSmoothing()) MessageBox.Show("Unable to reenable Font Smoothing", "Font Smoothing error"); } }
//输出当前地图为图片 public static string ExportImage(IActiveView pActiveView) { if (pActiveView == null) { return null; } try { SaveFileDialog pSaveFileDialog = new SaveFileDialog(); pSaveFileDialog.Filter = "JPEG(*.jpg)|*.jpg|AI(*.ai)|*.ai|BMP(*.BMP)|*.bmp|EMF(*.emf)|*.emf|GIF(*.gif)|*.gif|PDF(*.pdf)|*.pdf|PNG(*.png)|*.png|EPS(*.eps)|*.eps|SVG(*.svg)|*.svg|TIFF(*.tif)|*.tif"; pSaveFileDialog.Title = "输出地图"; pSaveFileDialog.RestoreDirectory = true; pSaveFileDialog.FilterIndex = 1; if (pSaveFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) { return null; } string FileName = pSaveFileDialog.FileName; IExport pExporter = null; switch (pSaveFileDialog.FilterIndex) { case 1: pExporter = new ExportJPEGClass(); break; case 2: pExporter = new ExportBMPClass(); break; case 3: pExporter = new ExportEMFClass(); break; case 4: pExporter = new ExportGIFClass(); break; case 5: pExporter = new ExportAIClass(); break; case 6: pExporter = new ExportPDFClass(); break; case 7: pExporter = new ExportPNGClass(); break; case 8: pExporter = new ExportPSClass(); break; case 9: pExporter = new ExportSVGClass(); break; case 10: pExporter = new ExportTIFFClass(); break; default: MessageBox.Show("输出格式错误"); return null; } IEnvelope pEnvelope = new EnvelopeClass(); ITrackCancel pTrackCancel = new CancelTrackerClass(); tagRECT ptagRECT; ptagRECT = pActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame(); int pResolution = (int)(pActiveView.ScreenDisplay.DisplayTransformation.Resolution); pEnvelope.PutCoords(ptagRECT.left, ptagRECT.bottom, ptagRECT.right, ptagRECT.top); pExporter.Resolution = pResolution; pExporter.ExportFileName = FileName; pExporter.PixelBounds = pEnvelope; pActiveView.Output(pExporter.StartExporting(), pResolution, ref ptagRECT, pActiveView.Extent, pTrackCancel); pExporter.FinishExporting(); //释放资源 pSaveFileDialog.Dispose(); System.Runtime.InteropServices.Marshal.ReleaseComObject(pExporter); return FileName; } catch { return null; } }
/// <summary> /// 区域导出 /// </summary> /// <param name="pGeo">几何图形</param> /// <param name="OutputResolution">输出分辨率</param> /// <param name="ExpPath">输出路径</param> /// <param name="view">视图</param> public static void ExportRegion(IGeometry pGeo, int OutputResolution, string ExpPath, IActiveView view) { IExport export = null; IWorldFileSettings settings = null; IEnvelope envelope2 = pGeo.Envelope; string str = ExpPath.Substring(ExpPath.Length - 3, 3).ToUpper(); switch (str) { case "JPG": settings = new ExportJPEGClass(); export = new ExportJPEGClass(); settings = export as IWorldFileSettings;; settings.MapExtent = envelope2; settings.OutputWorldFile = false; break; case "BMP": settings = new ExportBMPClass(); export = new ExportBMPClass(); settings = export as IWorldFileSettings;; settings.MapExtent = envelope2; settings.OutputWorldFile = false; break; case "TIF": settings = new ExportTIFFClass(); export = new ExportTIFFClass(); settings = export as IWorldFileSettings;; settings.MapExtent = envelope2; settings.OutputWorldFile = false; break; case "PNG": settings = new ExportPNGClass(); export = new ExportPNGClass(); settings = export as IWorldFileSettings; settings.MapExtent = envelope2; settings.OutputWorldFile = false; break; default: break; } if (settings == null) { return; } export.ExportFileName = ExpPath; int num = (int)Math.Round(view.ScreenDisplay.DisplayTransformation.Resolution); tagRECT grect2 = new tagRECT(); IEnvelope envelope3 = new EnvelopeClass(); view.ScreenDisplay.DisplayTransformation.TransformRect(envelope2, ref grect2, 9); grect2.left = 0; grect2.top = 0; grect2.right = (int)Math.Round((double)((grect2.right - grect2.left) * (((double)OutputResolution) / ((double)num)))); grect2.bottom = (int)Math.Round((double)((grect2.bottom - grect2.top) * (((double)OutputResolution) / ((double)num)))); envelope3.PutCoords((double)grect2.left, (double)grect2.top, (double)grect2.right, (double)grect2.bottom); export.PixelBounds = envelope3; view.GraphicsContainer.DeleteAllElements(); view.Output(export.StartExporting(), OutputResolution, ref grect2, envelope2, null); export.FinishExporting(); export.Cleanup(); AddElement(pGeo, view); }
public static void ExportActiveView(IActiveView pView, long iOutputResolution, long lResampleRatio, string sExportType, string sOutputDir, string sOutputName, Boolean bClipToGraphicsExtent = true, IEnvelope pEnvelope = null) { //解决文件名错误 IActiveView docActiveView = pView; IExport docExport; long iPrevOutputImageQuality; IOutputRasterSettings docOutputRasterSettings; IEnvelope PixelBoundsEnv; tagRECT exportRECT; tagRECT DisplayBounds; IDisplayTransformation docDisplayTransformation; IPageLayout docPageLayout; IEnvelope docMapExtEnv; if (pEnvelope == null) pEnvelope = GetGraphicsExtent(pView); long hdc; long tmpDC; //string sNameRoot; long iScreenResolution; bool bReenable = false; IEnvelope docGraphicsExtentEnv; IUnitConverter pUnitConvertor; if (GetFontSmoothing()) { 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 (String.Compare(sExportType,"PDF",true) == 0) { docExport = new ExportPDFClass(); } else if (String.Compare(sExportType,"EPS",true) == 0) { docExport = new ExportPSClass(); } else if (String.Compare(sExportType,"AI",true) == 0) { docExport = new ExportAIClass(); } else if (String.Compare(sExportType,"BMP",true) == 0) { docExport = new ExportBMPClass(); } else if (String.Compare(sExportType,"TIFF",true) == 0) { docExport = new ExportTIFFClass(); } else if (String.Compare(sExportType,"SVG",true) == 0) { docExport = new ExportSVGClass(); } else if (String.Compare(sExportType,"PNG",true) == 0) { docExport = new ExportPNGClass(); } else if (String.Compare(sExportType,"GIF",true) == 0) { docExport = new ExportGIFClass(); } else if (String.Compare(sExportType,"EMF",true) == 0) { docExport = new ExportEMFClass(); } else if (String.Compare(sExportType,"JPEG",true) == 0 || String.Compare(sExportType,"JPG",true) == 0) { docExport = new ExportJPEGClass(); } else { MessageBox.Show("!!不支持的格式 " + sExportType + ", 默认导出 EMF."); sExportType = "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 = "ExportActiveViewSampleOutput"; //set the export filename (which is the nameroot + the appropriate file extension) docExport.ExportFileName = sOutputDir + "\\" + sOutputName + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0]; tmpDC = GetDC(0); iScreenResolution = GetDeviceCaps((int)tmpDC, 88); //88 is the win32 const for Logical pixels/inch in X) 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 = pEnvelope; } 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 = pEnvelope; 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("成功导出地图: " + docExport.ExportFileName, "提示"); //set the output quality back to the previous value SetOutputQuality(docActiveView, iPrevOutputImageQuality); if (bReenable) { EnableFontSmoothing(); bReenable = false; if (!GetFontSmoothing()) { //error: cannot reenable font smoothing. MessageBox.Show("Unable to reenable Font Smoothing", "Font Smoothing error"); } } docMapExtEnv = null; PixelBoundsEnv = null; }
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> /// Exports the map extent. /// </summary> /// <param name="activeView">The active view.</param> /// <param name="outRect">The out rect.</param> /// <param name="sOutputPath">The output file path.</param> /// <param name="dResolution">The d resolution.</param> /// <returns><c>true</c> if succeed, <c>false</c> otherwise.</returns> public bool ExportMapExtent(IActiveView activeView, Size outRect, string sOutputPath, double dResolution) { bool result; try { if (activeView == null) { result = false; } else { IExport export = null; if (sOutputPath.EndsWith(".jpg")) { export = new ExportJPEGClass(); } else if (sOutputPath.EndsWith(".tif")) { export = new ExportTIFFClass(); } else if (sOutputPath.EndsWith(".bmp")) { export = new ExportBMPClass(); } else if (sOutputPath.EndsWith(".emf")) { export = new ExportEMFClass(); } else if (sOutputPath.EndsWith(".png")) { export = new ExportPNGClass(); } else if (sOutputPath.EndsWith(".gif")) { export = new ExportGIFClass(); } export.ExportFileName = sOutputPath; IEnvelope extent = activeView.Extent; export.Resolution = dResolution; tagRECT tagRECT = default(tagRECT); tagRECT.left = (tagRECT.top = 0); tagRECT.right = outRect.Width; tagRECT.bottom = (int)((double)tagRECT.right * extent.Height / extent.Width); IEnvelope envelope = new EnvelopeClass(); envelope.PutCoords((double)tagRECT.left, (double)tagRECT.top, (double)tagRECT.right, (double)tagRECT.bottom); export.PixelBounds = envelope; ITrackCancel trackCancel = new CancelTrackerClass(); export.TrackCancel = trackCancel; trackCancel.Reset(); trackCancel.CancelOnKeyPress = true; trackCancel.CancelOnClick = false; trackCancel.ProcessMessages = true; int hDC = export.StartExporting(); activeView.Output(hDC, (int)((short)export.Resolution), ref tagRECT, extent, trackCancel); bool flag = trackCancel.Continue(); if (flag) { export.FinishExporting(); export.Cleanup(); } else { export.Cleanup(); } flag = trackCancel.Continue(); result = true; } } catch (Exception ex) { string message = ex.Message; result = false; } return(result); }
public static void ExportView(IActiveView view, IGeometry pGeo, int OutputResolution, int Width, int Height, string ExpPath, bool bRegion) { IExport pExport = null; tagRECT exportRect = new tagRECT(); IEnvelope pEnvelope = pGeo.Envelope; string sType = System.IO.Path.GetExtension(ExpPath); switch (sType) { case ".jpg": pExport = new ExportJPEGClass(); break; case ".bmp": pExport = new ExportBMPClass(); break; case ".gif": pExport = new ExportGIFClass(); break; case ".tif": pExport = new ExportTIFFClass(); break; case ".png": pExport = new ExportPNGClass(); break; case ".pdf": pExport = new ExportPDFClass(); break; default: MessageBox.Show("没有输出格式,默认到JPEG格式"); pExport = new ExportJPEGClass(); break; } pExport.ExportFileName = ExpPath; exportRect.left = 0; exportRect.top = 0; exportRect.right = Width; exportRect.bottom = Height;//bottom 写成 left 两天才排出错误 2016年4月17日20:24:13 if (bRegion) { view.GraphicsContainer.DeleteAllElements(); view.Refresh(); } IEnvelope envelope = new EnvelopeClass(); envelope.PutCoords((double)exportRect.left, (double)exportRect.top, (double)exportRect.right, (double)exportRect.bottom); pExport.PixelBounds = envelope; //MessageBox.Show("Go Here! OutputResolution " + OutputResolution, "yahooo"); view.Output(pExport.StartExporting(), OutputResolution, ref exportRect, pEnvelope, null);//无法运行2016年4月17日14:42:25 /* * 当时发现错误出现在上句,对分辨率参数做了排查,但是忽略了其它参数,而最终的问题就在于其中的pEnvelope参数 * 这次能够排出错误,是因为对照了原来的代码,而在自己写程序时没有参考代码,仅靠对照排错是不够的, * 因此,要逐步缩小出错的代码块,对每一个参数都要进行检验。(2016年4月17日20:31:12) */ pExport.FinishExporting(); pExport.Cleanup(); }
/// <summary> /// 输出当前地图至指定的文件 /// </summary> /// <param name="pView"></param> /// <param name="outPath"></param> public void ExportMapExtent(IActiveView pView, string outPath) { try { //参数检查 if (pView == null) { throw new Exception("输入参数错误,无法生成图片文件!"); } //根据给定的文件扩展名,来决定生成不同类型的对象 IExport export = null; if (outPath.EndsWith(".jpg")) { export = new ExportJPEGClass(); } else if (outPath.EndsWith(".tiff")) { export = new ExportTIFFClass(); } else if (outPath.EndsWith(".bmp")) { export = new ExportBMPClass(); } else if (outPath.EndsWith(".emf")) { export = new ExportEMFClass(); } else if (outPath.EndsWith(".png")) { export = new ExportPNGClass(); } else if (outPath.EndsWith(".gif")) { export = new ExportGIFClass(); } export.ExportFileName = outPath; IEnvelope pEnvelope = pView.Extent; //导出参数 export.Resolution = 300; tagRECT exportRect = new tagRECT(); exportRect.left = 0; exportRect.top = 0; //这里暂时固定大小 exportRect.right = 700; exportRect.bottom = 1000; IEnvelope envelope = new EnvelopeClass(); //输出范围 envelope.PutCoords(exportRect.left, exportRect.bottom, exportRect.right, exportRect.top); export.PixelBounds = envelope; //可用于取消操作 ITrackCancel pCancel = new CancelTrackerClass(); export.TrackCancel = pCancel; pCancel.Reset(); //点击ESC键时,中止转出 pCancel.CancelOnKeyPress = true; pCancel.CancelOnClick = false; pCancel.ProcessMessages = true; //获取handle int hDC = export.StartExporting(); //开始转出 pView.Output(hDC, (int)export.Resolution, ref exportRect, pEnvelope, pCancel); bool bContinue = pCancel.Continue(); //捕获是否继续 if (bContinue) { export.FinishExporting(); export.Cleanup(); MessageBox.Show("导出至" + outPath); } else { export.Cleanup(); } bContinue = pCancel.Continue(); } catch (Exception excep) { MessageBox.Show("导出失败" + excep.Message);//错误信息提示 } }