I want to get a part of picture in a PictureBox by allowing the user to select
a rectangular region (similar to how it can be done in Photoshop with selection tool).
How I can do this?
You would have to draw a rectangle or lines based on where they clicked and dragged. Your starting point would be the mouse x, y and then you would get the last point from mouse release x, y.
Catch the mouse events on the Picture Box to define your selection rectangle and use Graphics.DrawRectangle to draw your selection rectangle in the Paint Event of the Picture Box.
Related
I have a couple of rectangles drawn on a visio sheet. I am able to get the rectangles coordinates. I would like to track the mouse coordinates and if the mouse click is in one of the rectangles i would like to execute some code.
Is there a basic example on how to check whether the mouse click was in one of the rectangles?
Thanks in advance!
I want to be able to drag around a 100% zoomed picture in a picturebox: http://spunit.tk/x/dragpic1.png.
I want it to work exactly like the Windows Photo Viewer: http://spunit.tk/x/dragpic2.png.
How is this possible?
I believe you need to maintain the coordinates of that picturebox, also set its view style to full-image, without any stretching.
Then, you will need three mouse events: mouse down, mouse up and mouse move, where you can get the mouse coordinates and capture or release mouse to translate the picture box according to mouse delta translation.
I'm working on an image editor, there are some shape controls like rectangle, eclipse etc with drag drop feature on canvas and they are re-sizable(one corner is fixed, other three are re-sizable), attached a screen shot below:
I'd like to only resize the seleted corner, other three will be fixed, any ideas?
DaveRook Edit
In Photoshop, this is achieved by wrapping (under transforms)! This means re-drawing that corner un-proportionally to the rest of the image.
You will need to do a few things to achieve resizing of your rectangle.
a drag handle (something for the user to click and move).
to calculate the new height and width of your rectangle based upon the mouse position when the drag handle is being moved.
to update the height and width of the rectangle.
There is no shortcut here, but this SO question should be enough to get you started
I have button named "Paint" which should allow drawing rectangle on my picturebox after a click, i.e. it acts like a switch to allow drawing(on/off).
I've drawn rectangle using mouse positions as explained here: How Can I Capture Mouse Coordinate on PictureBox? . But rectangle is drawn whenever I move over PictureBox.
How can I implement the functionality where drawing must be implemented only when "Paint" is 'on'
I've tried starting implementation from events of Picturebox: Paint, MouseDown, MoseMove, Mouse Up...
set a flag(bool) in your application telling you the mode you are in whether drawing or not(can be activated from the button you are telling about).
in mouse down take the start point(e.x, e.y) from the mouse event handler.
now you have the top left point of the rectangle.
3.while mouse move take e.x and e.y and which is the bottom right point and draw your rectangle. put the drawing code in mouse move so that the it draws like the "Paint" Program(do this if the draw flag is true).
in mouse up reset the drawing flag
5.in the paint event of the picturebox draw the all the shapes you have so that if you minimized your application windows and then maximized it you will find your shapes drawn this can be achieved by making the rectangle is a class and make some instances of it(for loop over your shapes and draw it).
Simplest solution is to add a boolean bDraw variable, which becomes TRUE only on button click. All other drawing methods do not do anything if this variable is FALSE.
Other solution could be simply to subscribe to
Mouse events inside button click event handler. So if button is NOT clicked, no event raise happen.
I'm trying to write a very simple photo editor using C# 2008 or QT4.
How to make a resizeable rectangle selection tool like the photoshop did?
If you are talking about the "classic" "rubber band" type of selection rectangle, check out How to draw a rubber band rectangle or a focus rectangle in Visual C#.
WPF Code Example: http://www.codeproject.com/KB/WPF/wpfmarchingants.aspx?display=Print
This one is a bit more complicated because it involves image cropping, but if you scroll down to the selection rectangle, you can see the basic formulas for calculating the rectangle: http://69.10.233.10/KB/WPF/ImageCropper.aspx
Lastly, another one using GDI: http://codelog.blogial.com/2008/10/31/rubber-band-selection-rectangle-in-c/
If you provide more specifics, I'm sure we can help you out more.
Qt has a class for that:
http://doc.trolltech.com/4.5-snapshot/qrubberband.html
I don't know the specific calls, but the idea is this:
You want to draw a transparent rectangle with an opaque or dotted border. The rectangle appears when the mouse button is clicked. While the button is held, the dimension of the rectangle will change as the mouse moves, with the top-left point at the position where the button was clicked and the bottom-right following the mouse as it moves. Releasing the button causes the rectangle to fix its position over the selection area.
You should be able to figure out the particulars from a resource on the C# Drawing namespace.