|
-
Mar 25th, 2015, 03:29 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Fill GDI+ Path with an image?
Hi everyone!
I should be able to do this but.....
I need to fill a GDI+ graphics path with the contents of a bitmap image, the ultimate goal being to warp the image.
To clarify, I don't mean by using a texture brush, I mean to render the entire image along the path. I'm thinking that I need to get the image bits and somehow use that to fill the path?
I can't seem to find anything anywhere online about doing this. I can fill with shapes, text, textured shapes/text, etc. but can't seem to figure out how to fill the path with an image, say from a standard picture object.
As always, thoughts very much appreciated .
-
Mar 25th, 2015, 05:05 PM
#2
Re: Fill GDI+ Path with an image?
 Originally Posted by SomeYguy
...the ultimate goal being to warp the image.
To clarify, I don't mean by using a texture brush, I mean to render the entire image along the path.?
What kind of Path do you have in mind, can you show the outline of the Path-Shape,
the Image will finally have to fit in?
A concrete approach will depend on the complexity of that path...
Olaf
Last edited by Schmidt; Mar 25th, 2015 at 09:00 PM.
-
Mar 25th, 2015, 07:27 PM
#3
Thread Starter
Hyperactive Member
Re: Fill GDI+ Path with an image?
Hi Olaf, thanks for reply. The path will be arbitrary and will be changing as each of the four corners of the bounding rectangle are moved. So the path isn't very complex. I just need to fill the path with an image of the user's choosing.
-
Mar 25th, 2015, 07:51 PM
#4
Re: Fill GDI+ Path with an image?
Just 4 points? Then maybe something like this in our codebank? Here are a couple of ideas posted at VB Forums
http://www.vbforums.com/showthread.p...on-of-an-image
http://www.vbforums.com/showthread.p...in-vb6-like-3D
-
Mar 25th, 2015, 10:32 PM
#5
Hyperactive Member
Re: Fill GDI+ Path with an image?
The 2nd link given above does not seem to be a satisfactory one. I have now made the following comment there:
----------------------------------------------------------------------
...... To do a quick test of perspective, one may do the following: Add a couple of horizontal and vertical lines, more importantly also add the diagonal lines to your test picture.
Not only the horizontal and vertical lines should remain straight, the diagonal ones as well.
----------------------------------------------------------------------
-
Mar 26th, 2015, 01:28 AM
#6
Thread Starter
Hyperactive Member
Re: Fill GDI+ Path with an image?
Thanks guys, I had checked those two threads before and almost got it but I'm missing something . @LaVolpe, I'm using some of your vintage (and most excellent!) path warping code, modified for my purposes. I've learned many things from you and others regarding GDI+ operations & uses, thank you .
Anyway, no problems rendering shapes & lines & polygons & text etc. along the path, but I can't seem to figure out how to render a given image along the path so that I can use grab handles to interactively warp the image. The image would be stretched to the confines of the path.
I know how to load the image with GDI+ etc., so I guess that I just need to figure out how to add or render the image along the path after I have the image already loaded and have an associated bitmap object.
Again, thank you guys. You are most appreciated.
-
Mar 26th, 2015, 12:54 PM
#7
Re: Fill GDI+ Path with an image?
 Originally Posted by SomeYguy
... no problems rendering shapes & lines & polygons & text etc. along the path, but I can't seem to figure out how to render a given image along the path so that I can use grab handles to interactively warp the image. The image would be stretched to the confines of the path.
Since you mentioned "Handles" (aka "ControlPoints") - how many of them do you plan to use
in your "Image-Warping"?
If its really only four of them - and the Path which meets the Control-Points is rendered
as "linear PolygonPoint-Connections" (simple Lines), then the already mentioned "four Corner"-
Image-Distortion-Algo(s) might be enough...
In the RC5-cairo-wrapper there's also a HighLevel-method for this, to render an Image-Surface this way:
cCairoContext.RenderSurfaceWithinCorners(...)
But if your Path is more complex (described by more ControlPoints than only 4 -
or being curved between the ControlPoints), then please describe (or upload an image)
of the outline of this more complex Path, because the Algoritms for that kind of image-
Distortion are not as "straight" as in the "simple 4-corner-case" (or also not as simple as
e.g. a circular projection or something like that...).
Olaf
-
Mar 26th, 2015, 02:41 PM
#8
Re: Fill GDI+ Path with an image?
 Originally Posted by Brenker
The 2nd link given above does not seem to be a satisfactory one....
Not only the horizontal and vertical lines should remain straight, the diagonal ones as well.
Good point, those examples are kinda like a fast-easy method that does not reproduce true perspective. If someone were to want to build their own logic for a better example, this link I think explains the logic pretty well.
-
Mar 29th, 2015, 08:55 AM
#9
Re: Fill GDI+ Path with an image?
Since this thread hasn't been marked as "resolved" yet, here's another relevant link:
http://www.vbforums.com/showthread.p...rm-on-an-image
That thread includes a link to another VB implementation of projective transforms. There are also links to additional 3rd-party discussions that may be helpful.
-
Mar 29th, 2015, 09:53 AM
#10
Re: Fill GDI+ Path with an image?
Tanner, I found that thread Friday & forgot to come back here and reference it. I've been meaning to take a look at your project for enhancement ideas for one or more of my projects. Just haven found the motivation yet
-
Mar 29th, 2015, 12:27 PM
#11
Thread Starter
Hyperactive Member
Re: Fill GDI+ Path with an image?
Thanks guys I'm still working on it and I'll mark resolved when I have a solution. I'm testing GDI+ ideas currently. Thanks for help & great info!
-
Mar 29th, 2015, 12:42 PM
#12
Thread Starter
Hyperactive Member
Re: Fill GDI+ Path with an image?
@Tanner thanks, that's an awesome program! I'm trying to stay with GDI+ for my warping needs because (like Photodemon) it is a small part of a bigger project and all graphics operations throughout the program are done 100% with GDI+.
I'm experimenting with GdipDrawImagePointsRectI and a parallelogram which should do well for this. I'm currently trying to work out a way to allow for irregular shapes i.e. I have 4 corner points each of which I can drag anywhere on the drawing surface and the image will stretch to within the resulting shape
-
Mar 29th, 2015, 02:47 PM
#13
Re: Fill GDI+ Path with an image?
 Originally Posted by LaVolpe
Tanner, I found that thread Friday & forgot to come back here and reference it. I've been meaning to take a look at your project for enhancement ideas for one or more of my projects. Just haven found the motivation yet 
Ugh, I know what you mean about motivation. This topic reminded me of my intent to finish a StretchBlt-like function that takes eight arbitrary points (four source, four destination) and does the mapping for you. I have all the math, so it's just a matter of sitting down and doing it.
"Oh well, maybe tomorrow." 
 Originally Posted by SomeYguy
@Tanner thanks, that's an awesome program! I'm trying to stay with GDI+ for my warping needs because (like Photodemon) it is a small part of a bigger project and all graphics operations throughout the program are done 100% with GDI+.
Thanks for the kind words. If I may, here's why GDI+ alone can't handle this task.
GDI+ supports matrix manipulations for images, but they are limited to affine transforms only. Projective transforms also use a 3x3 matrix, but they require special terms in the 3rd column. Per that MSDN link, GDI+ only stores 6 out of the 9 terms in its matrix operations (because affine transforms don't use the 3rd column). This link shows some nice comparison images of affine and projective transforms; affine transforms include operations like translation, rotation, and skew, which GDI+ handles very well. Unfortunately, arbitrary four-point projective transforms cannot be decomposed into affine transformations, so they fall outside GDI+'s capabilities.
So there's no choice but to map the pixels yourself. This isn't actually that hard, assuming you have code in place for per-pixel access of GDI+ images (e.g. GdipBitmapLockBits). Projective transforms are nice because once you go through all the messy business of setting up the transformation matrix, the actual transform is easy, just two lines of code in the inner loop.
In my opinion, the hardest parts of this task are the quality issues involved in complex pixel mapping. Edge handling can be tricky, subsampling is generally advisable for regions where the destination is larger than the source, while supersampling is advisable in regions where the destination is smaller. Depending on your needs, these issues may/may not be relevant, but perhaps something to think about. (I wouldn't be surprised if these issues are why GDI+ doesn't provide projective transforms for image data.)
Anyway, this topic is a deep rabbit hole. But the tl;dr message is this: GDI+ cannot perform a projective transformation of pixel data.* You need to map the pixels yourself, unfortunately.
* Interestingly, GDI+ can apply projective transforms to paths, but there are caveats (the path must be flattened), and unfortunately the ability can't be extended to full images.
-
Mar 29th, 2015, 07:27 PM
#14
Thread Starter
Hyperactive Member
Re: Fill GDI+ Path with an image?
 Originally Posted by Tanner_H
Yup, I see that now.....I've seen projective transforming done with stretchblt, plgblt and some trickery for the 3rd column (I think) but the output is of pretty poor quality.
I can live with just dynamic resizing, rotating and simple skewing if I have to.....the involved images are going to be quite small and the program will use them in proprietary documents in a fashion similar to SVG graphics.
Back to play with it some more then rest cuz back to work tmw
-
Aug 1st, 2015, 10:53 AM
#15
Thread Starter
Hyperactive Member
Re: [RESOLVED] Fill GDI+ Path with an image?
I've marked this resolved because I finally ended up modifying and using code from here:
http://www.vbforums.com/showthread.p...on-of-an-image
Not the fastest but certainly usable for my purpose. Thank you guys!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|