Jump to content
  • Advertisement
Sign in to follow this  
errcw

[.net] Copy polygon region from one image to another (C#)

This topic is 3990 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would like to copy a polygon region out of a source image, scale it up by a factor of two, then draw that scaled polygon to a destination image. Some context: this function is designed to copy a region of a user-created drawing to a print template. Imagine, for example, decorating an origami crane on screen and from that creating a foldable printout. I am using C# 1.1 (bleh, it's painful but part of the specs). The best I came up with is Graphics.FillPolygon with a TextureBrush but that lacks scaling (i.e., the source would need to be pre-scaled) and, moreover, makes it very difficult to select the correct region from the source image. Another hack I devised is a straight pixel-by-pixel copy with a call to Polyon.IsInside(Point) to determine which pixels should be copied. This has two drawbacks in that it is painfully slow and requires me to write Polygon.IsInside (because I can't see where the standard library includes that functionality). Is there a simple way to do accomplish this copy using the standard library?

Share this post


Link to post
Share on other sites
Advertisement
A couple of ways to do this:

1. Create a Bitmap object large enough to hold the region to be copied, get a Graphics object from the Bitmap, set the Graphics object's clip region to the region you want to copy from the original, and draw the source image to the target Grahpics object. Now you have a copy of the the source region in the original size. Next, create a new Bitmap at the desired size, get a Graphics object from that Bitmap and draw the previously created Bitmap with whatever scaling you need.

2. Create a Bitmap object large enough to hold the region to be copied AND scaled to the desired target size. Then, use a Matrix to transform your region to the appropriate target scale, get a Graphics object from the new Bitmap, set the clip region using your newly scaled source region, and draw the source image to the new Graphics object with the desired scale.

Option 2 eliminates the need for an intermediate bitmap but might not be as accurate as option 1.

Share this post


Link to post
Share on other sites
Both excellent suggestions, thank you!

I implemented option 2 to replace my point-in-poly hack and it worked wonderfully plus was vastly more performant. It's a nice bonus that I also avoid creating an intermediate buffer, especially because this project is targeting very low spec devices.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!