I know this is rather old question but in case someone comes around and looks for something similar ...
If you just want to get the mask, there are much better approaches than referencing System.Drawing and actually drawing onto image in memory ...
struct Point
{
public readonly int X;
public readonly int Y;
public Point(int x, int y)
{
this.X = x;
this.Y = y;
}
}
bool PointInPolygon(Point[] polygon, int x, int y)
{
if(polygon == null || polygon.Length < 3) return false;
int counter = 0;
double intersections;
Point p1 = polygon[0];
Point p2;
for (int i = 1; i <= polygon.Length; i++)
{
p2 = polygon[i % polygon.Length];
if ((y > (p1.Y < p2.Y ? p1.Y : p2.Y)) && (y <= (p1.Y > p2.Y ? p1.Y : p2.Y)) && (x <= (p1.X > p2.X ? p1.X : p2.X)) && (p1.Y != p2.Y))
{
intersections = (y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X;
if (p1.X == p2.X || x <= intersections) counter++;
}
p1 = p2;
}
return counter % 2 != 0;
}
void Main()
{
Point[] polygon = new Point[] { new Point(1,1), new Point(20,10), new Point(10,30) };
bool[,] mask = new bool[30,30];
for(int i=0;i<30;i++)
{
for(int j=0;j<30;j++)
{
mask[i,j] = PointInPolygon(polygon, i, j);
Console.Write(mask[i,j]?"*":".");
}
Console.WriteLine();
}
}
Which will output like this:
..............................
..............................
..............................
..***.........................
...*****......................
...********...................
....**********................
....**************............
.....****************.........
.....*******************......
......*********************...
......************************
.......*********************..
.......*******************....
........****************......
........**************........
.........***********..........
.........*********............
..........******..............
..........****................
..........**..................
..............................
..............................
..............................
..............................
..............................
..............................
..............................
..............................
..............................