I am using C# .NET to do some vision work. I have a viewer object that is displaying an image and allows a user to use the toolbar to mark regions on the image. In the background, I pull the region from the image (if any regions exist) and want to use the marked region to perform a different function. My purposes require that the region be a rectangle so I want to make sure that the region I am pulling from the viewer is, indeed, a rectangle.
So what I would like to do is something along the lines of
if ( region_from_viewer is an instance of CWIMAQRectangle ) do_things;
I have tried using the IsInstanceOfType method but the problem is that the method "goes both ways" so to speak. In other words, if I invoke the method in this fashion:
RegionObject.IsInstanceOfType(RectangleObject), it will always be true because the InstanceOf method checks to see if one of two things is true (from the MSDN docs):
if the current Type is in the inheritance hierarchy of the object represented by [...] parameter, or if the current Type is an interface that [the parameter] supports
that is to say, if the function as implemented above returns true it just is simply indicating that the Rectangle Object supports the CWIMAQRegion interface, but what I really need to know is if that RegionObject can legally be cast down to a CWIMAQRectangle object (ie. is the RegionObject *really* an instance of CWIMAQRectangle).
the only way I currently know to do this is to simply attempt the cast, catch any exceptions and take that as an appropriate indication of what I need to know. That's pretty dirty though...so if anyone knows of another way, that would be great. Below is some code I have written to further clarify what I am trying to do. Note, I am still using the IsInstanceOf() method since using it "sounds" right when, in fact, it doesnt seem to be the appropriate function.
=====================================================================
CWIMAQRegionsClass searchRegion = new CWIMAQRegionsClass();
CWIMAQRectangle searchRectangle = new CWIMAQRectangle();
int LeftBoundry = currentImage.Width/4;
int TopBoundry = currentImage.Height/4;
searchRectangle.Initialize(LeftBoundry, TopBoundry, LeftBoundry * 2, TopBoundry * 2);
CWIMAQRegion viewerRegion;
if(imageViewer.Regions.Count > 0)
{
viewerRegion = imageViewer.Regions.Item(1);
if(viewerRegion.GetType().IsInstanceOfType(searchRectangle))
searchRegion.AddRectangle((CWIMAQRectangle)viewerRegion);
else
searchRegion.AddRectangle(searchRectangle);
}
else
searchRegion.AddRectangle(searchRectangle);
=====================================================================
thanks in advance for help!