This is going to be a long answer. I post the code, which I think is rather straightforward, but if you feel you need further explanations, I will post comments.
I guess you can accomplish this more easily (and faster) with advanced math, but I bet you'll get the main idea from the simple code below. I warn you that this code is very slow; try it with small images. Due to lack of time this code is not optimized at all, you can easily improve speed by changing a few lines, but I leave it to you :)
protected override void OnPaint(PaintEventArgs e)
{
if (Texture == null)
{
return;
}
Bitmap bmp = (Bitmap)Texture;
int width = Texture.Width;
int height = Texture.Height;
using (Graphics g = this.CreateGraphics())
{
g.Clear(Color.Black);
Point[] Dwu = this.DivideEdge(DrawPoints[0], DrawPoints[1], width, false);
Point[] Dwd = this.DivideEdge(DrawPoints[2], DrawPoints[3], width, true);
Point[] Dhl = this.DivideEdge(DrawPoints[3], DrawPoints[0], height, false);
Point[] Dhr = this.DivideEdge(DrawPoints[1], DrawPoints[2], height, true);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Point a = GetIntersection(Dwu[x], Dwd[x], Dhl[y], Dhr[y]);
g.DrawRectangle(new Pen(bmp.GetPixel(x, y)), a.X, a.Y, 1, 1);
}
}
}
}
Point[] DivideEdge(Point a, Point b, int points, bool reverse)
{
Point[] d = new Point[points + 1];
for (int i = 0; i <= points; i++)
{
d[i] = new Point(a.X + (b.X - a.X) * i / points, a.Y + (b.Y - a.Y) * i / points);
}
if (reverse)
{
Point[] d2 = new Point[points + 1];
for (int j = 0; j <= points; j++)
{
d2[points - j] = d[j];
}
return d2;
}
else
{
return d;
}
}
public Point GetIntersection(Point p1, Point p2, Point p3, Point p4)
{
int x1 = p1.X;int x2 = p2.X;int x3 = p3.X;int x4 = p4.X;
int y1 = p1.Y;int y2 = p2.Y;int y3 = p3.Y;int y4 = p4.Y;
float d = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
if (d == 0f)
{
return new Point(0, 0);
}
float na = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
float nb = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3);
float ua = na / d;
float ub = nb / d;
return new Point((int)(x1 + ua * (x2 - x1)), (int)(y1 + ua * (y2 - y1)));
}
Hope this helps, good luck!