How to do Free hand Image Cropping in WPF? - wpf

all
I m developing on Medical Image Processing software, I wont crop image on mouse event, means when we drag mouse cursor and move over the image in any shape, the selected portion of image should be cropped. and that will paste in same image. is it Possible in WPF or Silverlight?

Check out the Clip property. You can specify a path in this property that is used to clip the parent object. Set Clip on your Image control to clip the image.
For design time clipping, your best bet is to use Blend (download a trial here if you don't have it) to draw a free hand path. Right click on the path and select Path->Make Clipping Path. In the next dialog, select the image that you want to clip.
EDIT: It sounds like you want to use runtime clipping/cropping, so you will want to generate the clip path using the points gathered from the mouse events. Create a collection of these points and then generate a path from them (linear lines probably... I don't know how you would generate appropriate curves unless you wrote a complex algorithm). Set this path to the Clip property after the fact.
/EDIT

You can add a transparent layer on top of the control and add an InkCanvas.
Then allow the user to draw a stroke (freehand) and turn the stroke into a Geometry by using GetGeometry use the geometry in the Clip property of the control to be clipped.

Related

WPF InkCanvas Z-Index for strokes

In WPF InkCanvas, ink is rendered on top and strokes don't have z-index property. I have other UIElements like shape, images on InkCanvas and can't bring UIElements to top. Is there any work around for this problem. I have tried
below things.
1) use two different InkCanvas (one for strokes & one for UIElements)
- Can't achieve sandwich effect in this (i.e. UIElement between strokes
Something like this in this circle is UIElement
2) Masking strokes using geometry path and add as UIElement
- If I add strokes as UIElements (using path) then I will loose erase functionality.
The map editor which is part of our game suite uses ink to draw terrain so I have recent experience of this sort of thing.
The way my app works is you select a thing you're drawing ( woods, road, contour etc ) and draw a stroke using ink.
I then process that stroke and obtain a geometry.
I pass that to a viewmodel > itemssource > itemscontrol.
This is then templated into a piece of terrain.
This itemscontrol is below the inkcanvas.
The user can manipulate the z-index of those by moving them up and down in the observablecollection.
I rely on just the order to define z-index.
I also retain the data from each stroke by serialising it.
You're thinking "how about erase?".
To do that I'd have an explicit erase-this-thing mode.
Let them pick a shape and get it's stroke data, add the stroke back to the inkcanvas. Once they finish erasing then use a "save changes" button or something to re-process the stroke(s) and switch back to edit mode.
Since this is a game map editor I'm working on, drawing produces area and line types of terrain but their area/line is defined by a geometry translated from a stroke. ZIndex corresponds to the order of objects in the left panel. The buttons you can see with the chevrons pointing up and down move the order of a

WPF Get Image inside drawing

I'm using an Image control inside a simple canvas and I allow final user to draw some line (free hand) through Line wpf object.
When user ends his draw I would be able to get the image inside the design (such as a crop image).
Any idea?

Drawing shapes, text and images with moving and scaling in WPF

I'm trying to draw simple map in WPF. I need to draw shapes, text and images. It also should be possible to use mouse to move around and zoom in and out.
Right now, I have combination of Canvas + Geometry + Transforms to draw shapes, but I don't know how to add text and images.
I already tried various combination of Canvas/Grid and Layout/Render transform. Biggest problem is adding text and images, because transformations are in geometries.
If i understand you right, you're currently putting Path objects (with transformed Geometries) into a Canvas. For adding text and images you could easily add TextBlock and Image objects to the same Canvas and apply your transformations to their RenderTransform property.
A completely different approach would be to use WPF low-level rendering, provided by the DrawingVisual class. You may start at WPF Graphics Rendering Overview.

How to draw onto a PictureBox image when control resizes?

I am using the pictureBox_Paint event to try and draw an overlay onto the image in a PictureBox.
This is working fine until I resize the PictureBox (set to use SizeMode.Zoom), when I do this the overlay graphic is drawn off position by the margin between the image and the edge of the PictureBox. I guess I need to use the ImageRectangle somehow but this is not public.
I would create a custom usercontrol instead, you would have much more control, and would not be difficult to build.

Morphing from an image to a shape in Silverlight 3

I have a requirement to morph from an image (png) to a shape (polygon) in Silverlight 3 as an effect, but of course there is no built in transition or method to do this.
At the moment the best I have is fade one out and the other in, but can anyone suggest a decent alternative that may work or look better?
Regards
Moo
In Blend:
Create a rectangle. Set stroke to No Brush and Fill to Tile Brush.
For the ImageBrush of the Tile Brush, select your image.
In the object browser, select the rectangle, right click > Path > Convert to Path.
Use the pen tool to add some points to the path.
Add a storyboard.
Add a keyframe at 1 second. Blend will go into record mode
Use the direct selection tool to move the points into the polygon shape you want. Test out your animation.
At this point, the image morphs into a shape, but the image is still there. If you need to remove the image as well as morph it:
In your storyboard, at the keyframe at 1 second, change the opacity to 0.
Create a copy of the rectangle, but make sure fill is set to No Brush and Stroke is set to a color and width. Set the opacity to 0.
Add points, and mimic the animation you just set up for the image's rectangle.
Add a keyframe at 1 second for this element. In record mode, change the opacity to 100%.
The end result will be both paths morphing, the one with the image fading out while the one with no fill fading in.
I'm not a silverlight programmer, and don't know the details of what you want to do, so this is just a shot in the dark, but... if the shape you want the image to morph to is always going to have the same initial visual appearance (or some limited set of appearances) you might try morphing from the original image to an image of that shape, and then swapping the image that's the morph target for the geometry once you're done the morph. Course whether that would work or not is very dependent on the details of what you're doing. Sorry if you've already considered that and ruled it out.
You could possibly morph the image brush to the path of the shape by using an appropriate projection matrix. Or render a shape using the image brush and then morph that shape to the target shape, i.e. go from a rectangle to the target shape but using the image brush as the shape background. You may need to still warp the image brush somehow though.
An example of rendering a warped image is here in Charles Petzold's blog.

Resources