Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
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

1 solution

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;
    }
 
Share this answer
 
v2
Comments
Deviant Sapphire 15-Mar-14 7:40am    
Yes, this worked perfectly!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900