I have the following code:
using (var gp = new GraphicsPath())
{
var outer = new PointF[outerpoly.Count];
for (int i = 0; i < outerpoly.Count; i++)
{
outer[i] = new PointF(((int)(scale * outerpoly[i].X - xmin)), (int)(scale * (outerpoly[i].Y + -ymin)));
}
gp.AddPolygon(outer);
foreach (var hole in insideholes)
{
if (hole.Count < 3) continue;
var inner = new PointF[hole.Count];
for (int i = 0; i < hole.Count; i++)
{
inner[i] = new PointF(((int)(scale * hole[i].X - xmin)), (int)(scale * (hole[i].Y + -ymin)));
}
gp.AddPolygon(inner);
}
Graphics.FromImage(e).FillPath(color, gp);
}
where outerpoly is a list of intpoints (pairs of x
and y
) representing the outer border of the polygon and inside holes are a list of list of intpoints representing the holes in side the polygon.
Now this code should draw a polygon with a number of holes in it. A example of what the inner and outer might be given as values:
outer
{System.Drawing.PointF[4]}
[0]: {X=-289, Y=971}
[1]: {X=-289, Y=0}
[2]: {X=734, Y=971}
[3]: {X=-289, Y=971}
inner
{System.Drawing.PointF[4]}
[0]: {X=-158, Y=797}
[1]: {X=189, Y=568}
[2]: {X=-158, Y=568}
[3]: {X=-158, Y=797}
Now the result of this code is that only the outer is drawn and the holes are ignored. Any idea why?
The code is based on the question.
When trying to use the exclude method instead like in the following:
var outer = new PointF[outerpoly.Count];
for (int i = 0; i < outerpoly.Count; i++)
{
outer[i] = new PointF(((int)(scale * outerpoly[i].X - xmin)), (int)(scale * (outerpoly[i].Y + -ymin)));
}
var gp = new GraphicsPath();
gp.AddPolygon(outer);
Region rr = new Region(gp);
foreach (var hole in insideholes)
{
if (hole.Count < 3) continue;
var inner = new PointF[hole.Count];
for (int i = 0; i < hole.Count; i++;)
{
inner[i] = new PointF(((int)(scale * hole[i].X - xmin)), (int)(scale * (hole[i].Y + -ymin)));
}
var gpe = new GraphicsPath();
gpe.AddPolygon(inner);
Region.Exclude(gpe);
gpe.Dispose();
}
gp.Dispose();
Graphics.FromImage(e).FillRegion(color, rr);
rr.Dispose();
This crashed on the line Region.Exclude(gpe);
instead, no exception, just a sudden crash to desktop.