# Exception around setting map extents

**URL:** <https://community.thinkgeo.com/t/exception-around-setting-map-extents/889>\
**Category:** WinForms\
**Created:** [December 1, 2009, 6:10pm UTC](https://community.thinkgeo.com/t/exception-around-setting-map-extents/889 "2009-12-01T18:10:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Greg](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@Greg](https://community.thinkgeo.com/u/Greg)\
**Post date:** [December 1, 2009, 6:10pm UTC](https://community.thinkgeo.com/t/exception-around-setting-map-extents/889/1 "2009-12-01T18:10:24Z")

</div>

Hi,

I&nbsp;get&nbsp;the&nbsp;following&nbsp;exception&nbsp;when&nbsp;I&nbsp;try&nbsp;to&nbsp;reset&nbsp;my&nbsp;map&nbsp;extents:

&nbsp;&nbsp;&nbsp;at&nbsp;System.Drawing.Bitmap..ctor(Int32&nbsp;width,&nbsp;Int32&nbsp;height,&nbsp;PixelFormat&nbsp;format)  
  
&nbsp;&nbsp;&nbsp;at&nbsp;ThinkGeo.MapSuite.DesktopEdition.WpfMap.xfbd15a0e9d15f4e2(Graphics&nbsp;x783a718028818269,&nbsp;RectangleShape&nbsp;xb35a33b423b17f65)  
  
&nbsp;&nbsp;&nbsp;at&nbsp;ThinkGeo.MapSuite.DesktopEdition.WpfMap.x7d52d0ad1d6779cd(Boolean&nbsp;x991dc8ddc7528b44,&nbsp;RectangleShape&nbsp;x178b193eec228e6e)

`  private void ResetExtents() {               layer.Open();          wpfMap1.CurrentExtent = layer.FeatureSource.GetBoundingBox();    wpfMap1.CenterAt(layer.GetBoundingBox().GetCenterPoint());          layer.Close();  } `

here&nbsp;are&nbsp;the&nbsp;values&nbsp;I'm&nbsp;supplying:

layer.GetBoundingBox().GetCenterPoint()&nbsp;=&nbsp;-93.9375495910645,32.2573108673095,0

layer.FeatureSource.GetBoundingBox()&nbsp;=&nbsp;-94.0427372387695,32.3991367749023,-93.8323619433594,32.1154849597168

Any&nbsp;ideas?

Thanks,

Greg

---

<div class="post-metadata">

**Author:** ![ThinkGeo](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/thinkgeo/32/25029_2.png) [@ThinkGeo](https://community.thinkgeo.com/u/ThinkGeo)\
**Post date:** [December 1, 2009, 6:37pm UTC](https://community.thinkgeo.com/t/exception-around-setting-map-extents/889/2 "2009-12-01T18:37:57Z")

</div>

Greg,

Thanks&nbsp;for&nbsp;your&nbsp;post.  
&nbsp;  
I&nbsp;tried&nbsp;the&nbsp;HowDoI&nbsp;sample\GettingStarted\HideOrShowAFeatureLayer&nbsp;by&nbsp;replacing&nbsp;the&nbsp;following&nbsp;code,&nbsp;it&nbsp;works&nbsp;fine.&nbsp;Could&nbsp;you&nbsp;try&nbsp;this&nbsp;in&nbsp;your&nbsp;environment?  
`	private void cbkShowLayer_CheckedChanged(object sender, RoutedEventArgs e)        {            //if (wpfMap1.Overlays.Count > 0)            //{            //    wpfMap1.FindFeatureLayer("WorldLayer").IsVisible = cbkShowLayer.IsChecked.Value;            //    wpfMap1.Refresh(wpfMap1.Overlays["WorldOverlay"]);            //}             if (wpfMap1.Overlays.Count > 0)            {                FeatureLayer featureLayer = wpfMap1.FindFeatureLayer("WorldLayer");                featureLayer.Open();                 //wpfMap1.CurrentExtent = featureLayer.GetBoundingBox();                wpfMap1.CurrentExtent = new RectangleShape(-94.0427372387695, 32.3991367749023, -93.8323619433594, 32.1154849597168);                 //wpfMap1.CenterAt(featureLayer.GetBoundingBox().GetCenterPoint());                wpfMap1.CenterAt(new PointShape(-93.9375495910645,32.2573108673095));                featureLayer.Close();                 wpfMap1.Refresh();            }        }`  
&nbsp;  
From&nbsp;the&nbsp;stack&nbsp;trace,&nbsp;it&nbsp;seems&nbsp;that&nbsp;the&nbsp;problem&nbsp;root&nbsp;is&nbsp;the&nbsp;background&nbsp;of&nbsp;the&nbsp;MapControl,&nbsp;I&nbsp;am&nbsp;wondering&nbsp;if&nbsp;there&nbsp;is&nbsp;any&nbsp;special&nbsp;code&nbsp;in&nbsp;it?  
&nbsp;  
If&nbsp;you&nbsp;still&nbsp;have&nbsp;problem,&nbsp;could&nbsp;you&nbsp;send&nbsp;us&nbsp;a&nbsp;small&nbsp;sample&nbsp;application&nbsp;to&nbsp;show&nbsp;us&nbsp;your&nbsp;problem?&nbsp;That&nbsp;would&nbsp;be&nbsp;very&nbsp;helpful&nbsp;to&nbsp;find&nbsp;out&nbsp;and&nbsp;fix&nbsp;the&nbsp;problem.  
&nbsp;  
Any&nbsp;more&nbsp;questions&nbsp;just&nbsp;feel&nbsp;free&nbsp;to&nbsp;let&nbsp;me&nbsp;know.  
&nbsp;  
Thanks.  
&nbsp;  
Yale

---

<div class="post-metadata">

**Author:** ![Greg](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@Greg](https://community.thinkgeo.com/u/Greg)\
**Post date:** [December 2, 2009, 12:10pm UTC](https://community.thinkgeo.com/t/exception-around-setting-map-extents/889/3 "2009-12-02T12:10:16Z")

</div>

Hardcoding&nbsp;the&nbsp;values&nbsp;does&nbsp;prevent&nbsp;the&nbsp;exception&nbsp;from&nbsp;happening.

The&nbsp;problem&nbsp;seems&nbsp;related&nbsp;to&nbsp;how&nbsp;I&nbsp;set&nbsp;the&nbsp;CurrentExtent&nbsp;upon&nbsp;the&nbsp;initial&nbsp;load/display&nbsp;of&nbsp;the&nbsp;map.&nbsp;&nbsp;When&nbsp;I&nbsp;initially&nbsp;display&nbsp;my&nbsp;map,&nbsp;I&nbsp;haven't&nbsp;yet&nbsp;loaded&nbsp;any&nbsp;features&nbsp;into&nbsp;it,&nbsp;but&nbsp;for&nbsp;some&nbsp;reason&nbsp;if&nbsp;I&nbsp;do&nbsp;not&nbsp;set&nbsp;my&nbsp;extents&nbsp;to&nbsp;the&nbsp;feature&nbsp;set&nbsp;bounding&nbsp;box&nbsp;(unknown&nbsp;at&nbsp;this&nbsp;time),&nbsp;then&nbsp;I&nbsp;get&nbsp;the&nbsp;exception.&nbsp;&nbsp;By&nbsp;hardcoding&nbsp;the&nbsp;value&nbsp;in,&nbsp;the&nbsp;value&nbsp;was&nbsp;set&nbsp;upon&nbsp;initial&nbsp;load.&nbsp;&nbsp;Then,&nbsp;when&nbsp;the&nbsp;user&nbsp;selects&nbsp;the&nbsp;dataset&nbsp;they&nbsp;wish&nbsp;to&nbsp;view,&nbsp;I&nbsp;reset&nbsp;the&nbsp;extents&nbsp;to&nbsp;match&nbsp;this&nbsp;new,&nbsp;user&nbsp;selected&nbsp;data&nbsp;set.

So...&nbsp;for&nbsp;example.&nbsp;The&nbsp;code&nbsp;below&nbsp;doesn't&nbsp;work.&nbsp;&nbsp;(initialSetup&nbsp;is&nbsp;true&nbsp;only&nbsp;during&nbsp;the&nbsp;initial&nbsp;setup&nbsp;of&nbsp;wpfMap&nbsp;(i.e.,&nbsp;true&nbsp;during&nbsp;construction)

&nbsp;

&nbsp;

&nbsp;

`   layer.open();   PointShape centerPoint = layer.GetBoundingBox().GetCenterPoint();   RectangleShape extent = layer.FeatureSource.GetBoundingBox();   if(initialSetup)   {                   wpfMap1.CenterAt(centerPoint);                   wpfMap1.CurrentExtent = extent;   }   else   {                   RectangleShape newExtent = new RectangleShape(-94.0427372387695, 32.3991367749023, -93.8323619433594, 32.1154849597168);          wpfMap1.CenterAt(centerPoint);                   wpfMap1.CurrentExtent = newExtent;   }   layer.close(); `

But,&nbsp;this&nbsp;code&nbsp;works.&nbsp;&nbsp;Obviously&nbsp;this&nbsp;won't&nbsp;fly&nbsp;for&nbsp;production.

&nbsp;

&nbsp;

&nbsp;

`  layer.open();  PointShape centerPoint = layer.GetBoundingBox().GetCenterPoint();      RectangleShape newExtent = new RectangleShape(-94.0427372387695, 32.3991367749023, -93.8323619433594, 32.1154849597168);      wpfMap1.CenterAt(centerPoint);      wpfMap1.CurrentExtent = newExtent;  layer.close(); `

So,&nbsp;Am&nbsp;I&nbsp;not&nbsp;able&nbsp;to&nbsp;change&nbsp;the&nbsp;initial&nbsp;extent?

Thanks,

Greg

---

<div class="post-metadata">

**Author:** ![Greg](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@Greg](https://community.thinkgeo.com/u/Greg)\
**Post date:** [December 2, 2009, 3:30pm UTC](https://community.thinkgeo.com/t/exception-around-setting-map-extents/889/4 "2009-12-02T15:30:49Z")

</div>

I&nbsp;found&nbsp;a&nbsp;workaround&nbsp;for&nbsp;my&nbsp;problem.&nbsp;&nbsp;I&nbsp;created&nbsp;a&nbsp;BeginUpdate&nbsp;/&nbsp;EndUpdate&nbsp;concept&nbsp;which&nbsp;prevented&nbsp;other&nbsp;code&nbsp;from&nbsp;successfully&nbsp;refreshing&nbsp;until&nbsp;I&nbsp;decide.&nbsp;&nbsp;I&nbsp;determined&nbsp;that&nbsp;a&nbsp;refresh&nbsp;was&nbsp;coming&nbsp;in&nbsp;around&nbsp;the&nbsp;time&nbsp;the&nbsp;code&nbsp;to&nbsp;reset&nbsp;the&nbsp;extents&nbsp;was&nbsp;happening.

-Greg

---

<div class="post-metadata">

**Author:** ![ThinkGeo](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/thinkgeo/32/25029_2.png) [@ThinkGeo](https://community.thinkgeo.com/u/ThinkGeo)\
**Post date:** [December 2, 2009, 6:25pm UTC](https://community.thinkgeo.com/t/exception-around-setting-map-extents/889/5 "2009-12-02T18:25:02Z")

</div>

Greg,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks&nbsp;for&nbsp;your&nbsp;sharing.&nbsp;  
&nbsp;&nbsp;  
&nbsp;I&nbsp;agree&nbsp;with&nbsp;you&nbsp;about&nbsp;the&nbsp;root&nbsp;problem&nbsp;reason,&nbsp;and&nbsp;I&nbsp;also&nbsp;found&nbsp;some&nbsp;similar&nbsp;problem&nbsp;when&nbsp;building&nbsp;our&nbsp;HowDoI&nbsp;samples.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Any&nbsp;more&nbsp;questions&nbsp;just&nbsp;feel&nbsp;free&nbsp;to&nbsp;let&nbsp;me&nbsp;know.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Yale&nbsp;
