版权声明:欢迎评论和转载,转载请注明来源。 https://blog.csdn.net/zy332719794/article/details/25982769
之前写了一些xmd文件导出图片的代码,但多多少少有一些bug,如范围、网格线、图例的显示bug问题。下面是比较全面的导出代码,亲测有效。
直接上代码:
using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Controls;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;
namespace DView.DotNet.Thematic
{
public class MapToPicture
{
#region 导出JPEG函数及常量
//导出JPEG函数及常量
[DllImport("GDI32.dll")]
public static extern int GetDeviceCaps(int hdc, int nIndex);
[DllImport("User32.dll")]
public static extern int GetDC(int hWnd);
[DllImport("User32.dll")]
public static extern int ReleaseDC(int hWnd, int hDC);
[DllImport("user32.dll", SetLastError = true)]
static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref int pvParam, uint fWinIni);
const uint SPI_GETFONTSMOOTHING = 74;
const uint SPI_SETFONTSMOOTHING = 75;
const uint SPIF_UPDATEINIFILE = 0x1;
#endregion
public MapToPicture()
{
}
#region 导出当前视图图片
/// <summary>
/// 导出当前视图图片
/// </summary>
/// <param name="iOutputResolution">分辨率</param>
/// <param name="lResampleRatio">输出图像质量</param>
/// <param name="ExportType">输出类型</param>
/// <param name="sOutputDir">输出目录</param>
/// <param name="bClipToGraphicsExtent">是否剪切为graphic</param>
public static void ExportActiveViewParameterized(IActiveView sActiveView, long iOutputResolution, long lResampleRatio, string ExportType, string outPathStr, Boolean bClipToGraphicsExtent)
{
IActiveView docActiveView = sActiveView;// 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 == "TIF")
{
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
{
//DevExpress.XtraEditors.XtraMessageBox.Show("Unsupported export type " + ExportType + ", defaulting to 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);
}
//string sNameRoot = strPicName;
if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(outPathStr)))
{
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(outPathStr));
}
docExport.ExportFileName = outPathStr;// sOutputDir +@"\"+ sNameRoot + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0];
tmpDC = GetDC(0);
iScreenResolution = GetDeviceCaps((int)tmpDC, 88);
ReleaseDC(0, (int)tmpDC);
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 = (Convert.ToDouble(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;
hdc = docExport.StartExporting();
docActiveView.Output((int)hdc, (int)docExport.Resolution, ref exportRECT, docMapExtEnv, null);
docExport.FinishExporting();
docExport.Cleanup();
//DevExpress.XtraEditors.XtraMessageBox.Show("成功导出地图到" + sOutputDir + sNameRoot + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0] + ".", "导出地图");
SetOutputQuality(docActiveView, iPrevOutputImageQuality);
if (bReenable)
{
EnableFontSmoothing();
bReenable = false;
if (!GetFontSmoothing())
{
//DevExpress.XtraEditors.XtraMessageBox.Show("Unable to reenable Font Smoothing", "Font Smoothing error");
}
}
docMapExtEnv = null;
PixelBoundsEnv = null;
}
private static Boolean GetFontSmoothing()
{
bool iResult;
int pv = 0;
iResult = SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, ref pv, 0);
if (pv > 0)
{
return true;
}
else
{
return false;
}
}
private static void SetOutputQuality(IActiveView docActiveView, long iResampleRatio)
{
IGraphicsContainer oiqGraphicsContainer;
IElement oiqElement;
IOutputRasterSettings docOutputRasterSettings;
IMapFrame docMapFrame;
IActiveView TmpActiveView;
if (docActiveView is IMap)
{
docOutputRasterSettings = docActiveView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings;
docOutputRasterSettings.ResampleRatio = (int)iResampleRatio;
}
else if (docActiveView is IPageLayout)
{
docOutputRasterSettings = docActiveView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings;
docOutputRasterSettings.ResampleRatio = (int)iResampleRatio;
oiqGraphicsContainer = docActiveView as IGraphicsContainer;
oiqGraphicsContainer.Reset();
oiqElement = oiqGraphicsContainer.Next();
while (oiqElement != null)
{
if (oiqElement is IMapFrame)
{
docMapFrame = oiqElement as IMapFrame;
TmpActiveView = docMapFrame.Map as IActiveView;
docOutputRasterSettings = TmpActiveView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings;
docOutputRasterSettings.ResampleRatio = (int)iResampleRatio;
}
oiqElement = oiqGraphicsContainer.Next();
}
docMapFrame = null;
oiqGraphicsContainer = null;
TmpActiveView = null;
}
docOutputRasterSettings = null;
}
private static void DisableFontSmoothing()
{
bool iResult;
int pv = 0;
iResult = SystemParametersInfo(SPI_SETFONTSMOOTHING, 0, ref pv, SPIF_UPDATEINIFILE);
}
private static IEnvelope GetGraphicsExtent(IActiveView docActiveView)
{
IEnvelope GraphicsBounds;
IEnvelope GraphicsEnvelope;
IGraphicsContainer oiqGraphicsContainer;
IPageLayout docPageLayout;
IDisplay GraphicsDisplay;
IElement oiqElement;
GraphicsBounds = new EnvelopeClass();
GraphicsEnvelope = new EnvelopeClass();
docPageLayout = docActiveView as IPageLayout;
GraphicsDisplay = docActiveView.ScreenDisplay;
oiqGraphicsContainer = docActiveView as IGraphicsContainer;
oiqGraphicsContainer.Reset();
oiqElement = oiqGraphicsContainer.Next();
while (oiqElement != null)
{
oiqElement.QueryBounds(GraphicsDisplay, GraphicsEnvelope);
GraphicsBounds.Union(GraphicsEnvelope);
oiqElement = oiqGraphicsContainer.Next();
}
return GraphicsBounds;
}
private static void EnableFontSmoothing()
{
bool iResult;
int pv = 0;
iResult = SystemParametersInfo(SPI_SETFONTSMOOTHING, 1, ref pv, SPIF_UPDATEINIFILE);
}
#endregion
}
}
这里再补上快速设置mxd数据源的代码
IWorkspace workspace = // 这里获取自己的空做空间,如sde、gdb等;
ThematicDataSourse dataSourse = new ThematicDataSourse();
dataSourse.DataSourceUpdateFunction(workspace, ref mapDocument);