Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is related to the question I asked earlier about the syntax for this method under Python and comtypes. Now that I know the syntax and am trying to use it, the returned out params are equal to 0. Why is this? Here is my code:

#make TextElement
pTextElement = NewObj(esriCarto.TextElement, esriCarto.ITextElement)
pTextFont = NewObj(stdole.StdFont, stdole.IFontDisp)
pTextFont.Name = 'Arial'
pTextFont.Size = 34.0
pTextSymbol = NewObj(esriDisplay.TextSymbol, esriDisplay.IFormattedTextSymbol)
pTextSymbol.Font = pTextFont
pTextSymbol.HorizontalAlignment = esriDisplay.esriTHACenter
pTextSymbol.VerticalAlignment = esriDisplay.esriTVACenter
txt = 'T>'   #((( hard coded for testing )))
pScrDisp = mxDoc.ActiveView.ScreenDisplay
pDisplay = CType(pScrDisp, esriDisplay.IDisplay)
#Get dimensions of text string
pDisplay.StartDrawing(0, -1)
dblX, dblY = pTextSymbol.GetTextSize(pDisplay.hDC, pDisplay.DisplayTransformation, txt)

The out parameters dblX and dblY both equal 0. The hDC and DisplayTransformation inputs are valid. I checked. So, there is something I'm missing. Any insights?

share|improve this question
up vote 3 down vote accepted

If your script is not running inside the ArcMap's process, I think this is what's happening: The text symbol is created in the python's process, while the display object (map's or layout's screen display) inside the ArcMap's process. Processes in Windows cannot (by design) access GDI resources (like device contexts - the hDC in your code) which do not belong to them. Thus in the context of GetTextSize() call, the passed device context handle is invalid and measurement cannot be performed.

In order words, using IObjectFactory to create the symbols/elements in ArcMap's process (as discussed in other questions) will probably do the trick.

share|improve this answer
Exactly right, anything involving layout elements needs a drawing context to correctly get/set element metrics. arcpy.mapping does a lot of this behind the scenes, perhaps a hybridized approach here would work best. – Jason Scheirer Jan 12 '11 at 21:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.