public static void AddScaleBar() { try { stdole.StdFont pFont = new stdole.StdFont(); pFont.Name = "Arial"; ITextSymbol pTextSymbol = new TextSymbolClass(); pTextSymbol.Font = pFont as stdole.IFontDisp; pTextSymbol.Size = 5; IGraphicsContainer pGraphicsContainer = m_pPageLayout as IGraphicsContainer; IFrameElement pFrameElement = pGraphicsContainer.FindFrame(m_pMap); IMapFrame mapFrame = pFrameElement as IMapFrame; IUID uid = new UIDClass(); uid.Value = "esriCarto.AlternatingScaleBar"; IMapSurroundFrame mapSurroundFrame = mapFrame.CreateSurroundFrame(uid as UID, null); IElement element = mapSurroundFrame as IElement; IEnvelope envelope = new EnvelopeClass(); envelope.PutCoords(1.24, 0.4, 3.58, 0.55); element.Geometry = envelope; pGraphicsContainer.AddElement(element, 0); IMapSurround mapSurround = mapSurroundFrame.MapSurround; IScaleBar pScaleBar = mapSurround as IScaleBar; pScaleBar.Divisions = 1; pScaleBar.Subdivisions = 2; pScaleBar.DivisionsBeforeZero = 0; pScaleBar.LabelSymbol = pTextSymbol; pScaleBar.UnitLabelPosition = esriScaleBarPos.esriScaleBarBelow; pScaleBar.UnitLabelSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHACenter; pScaleBar.UnitLabelSymbol = pTextSymbol; pScaleBar.UnitLabelSymbol.Font = pFont as stdole.IFontDisp; pScaleBar.UnitLabelSymbol.Size = pTextSymbol.Size; pScaleBar.Name = "scalebar"; //if map scale is greater than 25000, switch the scale units to miles if (m_pMap.MapScale >= 25000) pScaleBar.Units = esriUnits.esriMiles; else pScaleBar.Units = esriUnits.esriFeet; IGraphicsContainerSelect pGraphicsContainerSelect = pGraphicsContainer as IGraphicsContainerSelect; pGraphicsContainerSelect.UnselectAllElements(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
Carlos Piccirillo Did you eventually get around this issue?
i have the same issue. Only when the Process runs in an background job, even if IMap is initialized. The IMap Object is not null, but the access to IMap.Mapscale aborts.
This is how I solved it.
/// <summary>
/// Purpose: Determine map scale once you are zoomed into the desired extent.
/// Cannot use m_pMap.MapScale because it cannot be used outside of ArcMap.
/// pMapFrame.MapBounds.Height is height of the DataFrame in feet and 7.8 is
/// the height of the DataFrame in inches. Before we calculate map scale, ground
/// distance needs to be in like units (inches), hence, pMapFrame.MapBounds.Height * 12.
/// </summary>
/// <returns>Int32</returns>
private static Int32 GetMapScale()
{
IMapFrame pMapFrame = new MapFrameClass();
IGraphicsContainer pGraphicsContainer = null;
try
{
pGraphicsContainer = m_pPageLayout as IGraphicsContainer;
pMapFrame = pGraphicsContainer.FindFrame(m_pMap) as IMapFrame;
double groundDistanceInInches = pMapFrame.MapBounds.Height * 12;
Int32 mapScale = Convert.ToInt32(groundDistanceInInches / 7.8);
return mapScale;
}
catch (Exception ex)
{
Queries.LogError(ex.StackTrace, ex.Message, "GetMapScale", "ensRegulatoryApplicationTools");
return 0;
}
finally
{
if (pMapFrame != null) { Marshal.ReleaseComObject(pMapFrame); pMapFrame = null; }
}
}