Search code examples
c#.netgraphicsgdi+gdi

Equality of GDI+ Regions


Why does the assertion fail in the following code? Why aren't regions a and b equal?

  Region a = new Region(new RectangleF(0.0f, 0.0f, 10.0f, 10.0f));
  Region b = new Region();
  b.MakeEmpty();
  b.Union(new RectangleF(0.0f, 0.0f, 10.0f, 10.0f));
  Debug.Assert(a == b, "Regions not equal");

Solution

  • From what I can see, System.Drawing.Region does not override Object's implementation of Equals(). Therefore your == call is using ReferenceEquals and simply telling you a and b are not the same object.

    Try using the System.Drawing.Region.Equals(Region, Graphics) overload instead, passing in a Graphics object in the context you wish to compare the two regions.