Unable to remove and Image layer

Jan 27, 2011 at 4:45 PM


I am working on an application using the dotspatial dll's to retreive and display an image from a web service. This image is saved to the drive along with the prj information and then added as a layer to the map and legend. When the user pans or zooms the previously added image layer is removed and a new layer is added.

Everything was working fine in the previous build, but in the latest build - 12/15/2010, Map.Layers.Remove(IMapLayer) is throwing an error. The stack trace or the error is below:

   at DotSpatial.Data.InRamImageData.OnBoundsChanged(IRasterBounds bounds) in c:\Dev\DotSpatial\DotSpatial.Data\DotSpatial.Data\InRamImageData.cs:line 457
   at DotSpatial.Data.RasterBoundDataSet.set_Bounds(IRasterBounds value) in c:\Dev\DotSpatial\DotSpatial.Data\DotSpatial.Data\RasterBoundDataSet.cs:line 57
   at DotSpatial.Data.RasterBoundDataSet.set_Bounds(IRasterBounds value) in c:\Dev\DotSpatial\DotSpatial.Data\DotSpatial.Data\RasterBoundDataSet.cs:line 51
   at DotSpatial.Data.ImageData.Dispose(Boolean disposeManagedResources) in c:\Dev\DotSpatial\DotSpatial.Data\DotSpatial.Data\ImageData.cs:line 538
   at DotSpatial.Data.DisposeBase.Dispose() in c:\Dev\DotSpatial\DotSpatial.Data\DotSpatial.Data\DisposeBase.cs:line 55
   at DotSpatial.Symbology.Layer.Dispose(Boolean disposeManagedResources) in c:\Dev\DotSpatial\DotSpatial.Symbology\DotSpatial.Symbology\Layer.cs:line 865
   at DotSpatial.Symbology.ImageLayer.Dispose(Boolean disposeManagedResources) in c:\Dev\DotSpatial\DotSpatial.Symbology\DotSpatial.Symbology\ImageLayer.cs:line 173
   at DotSpatial.Symbology.Layer.Dispose() in c:\Dev\DotSpatial\DotSpatial.Symbology\DotSpatial.Symbology\Layer.cs:line 784
   at DotSpatial.Symbology.LayerEventList`1.OnExclude(T item) in c:\Dev\DotSpatial\DotSpatial.Symbology\DotSpatial.Symbology\LayerEventList.cs:line 220
   at DotSpatial.Symbology.LayerCollection.OnExclude(ILayer item) in c:\Dev\DotSpatial\DotSpatial.Symbology\DotSpatial.Symbology\LayerCollection.cs:line 100
   at DotSpatial.Serialization.BaseList`1.RemoveAt(Int32 index) in c:\Dev\DotSpatial\DotSpatial.Serialization\DotSpatial.Serialization\BaseList.cs:line 160


This is my code for adding the layer, which I am trying to remove:

 IImageData imageData = new ImageData(string.Format("{0}\\{1}.jpg", Properties.Settings.Default.ApplicationFilesLocation, fileName));

IMapImageLayer layer = new MapImageLayer(imageData);


layer.LegendText = layerName;

Please let me know how I can fix this.



Jan 27, 2011 at 5:42 PM

This looks like a bug related to extents and is probably a quick fix.  I would report this as an issue instead of a discussion because this looks like a an invalid casting exception or null exception that probably came about by the changes from Envelope to Extent.  In the mean time, you can probably skate around the issue by just keeping a single image layer loaded, for instance, and changing the ImageData that it uses as the data object and updating the extents as you swap out images.  But yes, this looks like something that needs to be fixed on our end at line 457 in InRamImageData.


Jan 27, 2011 at 6:05 PM

Thanks Ted,

I have added this as an issue. I will try your approach to delete a layer for now.



Jan 27, 2011 at 8:42 PM


I am getting the same error on following your approach.

Here's what I tried:

IMapImageLayer layer = GetLayer(removeLayerName) as IMapImageLayer; 
IImageData imageData = new ImageData(string.Format("{0}\\{1}.jpg", Properties.Settings.Default.ApplicationFilesLocation, fileName)); 
layer.DataSet = imageData; 
Please advise.



Jan 28, 2011 at 4:16 PM

You said you were working with the December release.  Maybe if you got the latest release from source and built it this bug would be addressed.  I had planned on creating a release last weekend but never got around to it.  I will do so this weekend though.  But you can build any time from the latest source, which has had some bug fixes and changes, so maybe this is addressed.



May 12, 2011 at 12:16 AM


I am getting this error with the latest buid - DotSpatial 4-22-2011.

It was working fine with earlier builds. Please advise.

Thank you,


Jun 22, 2011 at 2:14 PM

Hi Ted,

I'm still getting this error with the latest build 5-23-2011

Is there any planned fix? Or a work around?



Aug 29, 2011 at 11:39 PM


Is there any fix for this?



Aug 30, 2011 at 9:44 PM

This method in InRamImageData.cs seems to be causing the exception

  protected override void OnBoundsChanged(IRasterBounds bounds)
            WorldFile.Affine = bounds.AffineCoefficients;
 I Commented it out and everything I need seems to be working. I did this on the source code associated with the 05/23 release.