if (el.VisualHit != null && (el.VisualHit as UIElement).IsHitTestVisible)
System.Windows.Media.DrawingVisual doesn't inherit from UIElement, so the as returns null and we get an exception.