15,399,462 members
1.00/5 (1 vote)
See more:
Hey Codeproject,

If I have a polygon that resembles a square with the cooardinates:
0: 0, 0
1: 0, 32
2: 32, 32
3: 32, 0

How would I fill those cooardinates(The square) with an existing Texture2D, and convert the polygon with the texture to another Texture2D?

Here's what I mean:

Texture2D: ---
Polygon: [ ]
New Texture2D: [-]
Posted
Updated 15-Mar-14 1:14am
v2

Solution 1

I've been Googling for a while, and I came across this. I'll be testing this out.

C#
```private Bitmap ClippedBitmap(Bitmap texture, Point[] pointsArray, out Point position)
{
int minX = pointsArray.Min(x => x.X);
int maxX = pointsArray.Max(x => x.X);
int minY = pointsArray.Min(x => x.Y);
int maxY = pointsArray.Max(x => x.Y);
position = new Point(minX, minY);
Bitmap bit = new Bitmap(maxX - minX, maxY - minY);
Point[] offset = new Point[pointsArray.Length];
pointsArray.CopyTo(offset, 0);
offset = Array.ConvertAll(offset, x=> x = new Point(x.X - minX,x.Y - minY));
Graphics g = Graphics.FromImage(bit);
TextureBrush tb = new TextureBrush(texture);
g.FillPolygon(tb, offset);
return bit;
}```
v2
Deviant Sapphire 15-Mar-14 7:40am

Yes, this worked perfectly!