Jump to content

gdi+ questions - how to get a path from a region


Recommended Posts

Two things:

    [*:88f34]If you want to learn how to use GDI+ in a Paint.NET effect, I suggest you read this source code:
    http://paintdotnet.12.forumer.com/viewtopic.php?t=2580
    Ignore the source code posted in the thread. Instead, look for the VS2005 source code zip file near the bottom of my post.
    [*:88f34]You do not have access to the selection path (marching ants).
    I had this same problem when working on the Feather plugin. I solved it by using transparency as an indication instead of the selection path.

Link to comment
Share on other sites

BoltBait, thanks four your answer.

Im not trying to do a plugin, I just looking to get a GraphicsPath object from a region (System.Drawing.Region). Purely Gdi+.

Basically I need:

Build a path ( I need point and dataTypes).

Build a region from that path.

Apply some boolean operation with other region.

Get the path of the resulting operation. ( with the pointsF[] and dataType[])

Of course, this could be solved if boolean operation were applicable on GraphicsPath objects.

Link to comment
Share on other sites

GDI+ does not provide a way to do this. In Paint.NET, we wrote a very brute-force algorithm to do this, and it's very slow. You can see it in PdnGraphicsPath.FromRegion().

The Paint.NET Blog: https://blog.getpaint.net/

Donations are always appreciated! https://www.getpaint.net/donate.html

forumSig_bmwE60.jpg

Link to comment
Share on other sites

  • 4 weeks later...

I've seen the way you are getting a path from the region, and indeed is a 'brute force' way to do it.

I just cant use this way since I need to handle floating point operations, but I can give you some hints on how to get this in a more elegant way. (Im working on this right now).

First, java.awt.geom.Area has an inmplementation of how to do logical operation to paths (It's using PathIterator to build segments and then operate them with algebraic functions). This can be implemented to .NET since it's not a very complicated algorithm.

RegionData contains information about the paths that forms a region, the problem it's not documented. I was working on this solution until now, since when a operation is applied to the region, the format seems to change.

What I have so far:

Region.GetRegionData().Data returns an array of bytes that represents that shape, this is what I have so far. (Before doing an operation to a region).

bitarray[28] has the number of paths of this region, I dont remember if I was converting it using int32, int16 o just ToString().

bitarray[36]:

First point of the path.

From now on, it's dificult to continue, since if the point is an Int it can be transformed using ToInt16 (so 2 bytes are taken) but if it's a float point 4 bytes are needed (using ToSingle).

bitarray[length - pathCount] first pathPointType.

These are easy to get, since they just need one byte per pathPointType.

Well, that's all I have, I hope to have something more this week. If someone knows something about regionData please share it.

Link to comment
Share on other sites

Well, I came up with some info (if someone is interested), but there are some bad news also.

I was able to get the path from GetRegionData, the big problem is paths are just being stored without the operations applied on them, I working to know how to get this anyway.

Link to comment
Share on other sites

sapphiron, I've actually done a lot of research into this area in order to do exactly what you are trying to figure out. The algorithms I've found have been ingenius and I've figured them out on paper, but I've yet to be successful at implementing them because the code ends up being very complex. I think it would take about 1-2 weeks of full time work to pull it off, and I just haven't been able to find that time unfortunately. I have PDF's of the best two lying around somewhere at home, I can send it to you if you want.

What you want is a function that takes a polygon set, a polygon, an operation, and then returns a new polygon set that is the result of combining the polygon set with the polygon based on the requested operation. By "polygon set" I basically mean an array of closed polygons. Each subsequent polygon in the set is implicitely combined with the previous one using XOR (that is to say, if it were rasterized, each output pixel would be XOR'd ... right now I don't remember the exact polygon-specific terminology).

Point[][] CombinePaths(Point[][] lhsPath, CombineMode combineMode, Point[] rhsPath)

('lhs' for left-hand side, rhs for right-hand side ... as in 'result = lhs OPERATOR rhs')

The Paint.NET Blog: https://blog.getpaint.net/

Donations are always appreciated! https://www.getpaint.net/donate.html

forumSig_bmwE60.jpg

Link to comment
Share on other sites

Rick, I really appreciate you answering my posts. I decided to ask for help in this forum, since if I'm a paint.net user and I know if someone helps me in this is helping this project too.

Anyway, as many developers, I'm working for a client that wont be very happy to know that I need 2 works for this and of course I wont ask anyone to do this for me.

So, I will try to work this out as fast as I can, and if I came up with something I'll let you know.

Link to comment
Share on other sites

If you implement this yourself it is definitely not something that can be done "super fast." Unless of course your IQ is 200 and you have high proficiency and lots of experience with both mathematics and computer programming. (Or maybe I'm just a complete retard :))

There's also a library out there that already does this. It's called ... Generic Polygon Clipper library? I know that Anti Grain Geometry, or whatever that library is called, uses it. The problem for me w.r.t. using that library is that the licensing is not compatible with what *I* need it for.

http://www.antigrain.com/

http://www.cs.man.ac.uk/~toby/alan/software/

Actually, I might just shoot the GPC crew an e-mail and ask about it. It's free for "non-commercial use" but I'm not sure how they'd classify Paint.NET (which is freeware, but clearly generates money from donations / advertising ... so it's commercial?).

The Paint.NET Blog: https://blog.getpaint.net/

Donations are always appreciated! https://www.getpaint.net/donate.html

forumSig_bmwE60.jpg

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...