Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
how to develop a crop selection control like photoshop's in c# 4.0 in widows form application.

I have a windows form application in c# 4.0 that can crop images. At first you have to draw a rectangle using mouse to select the cropped region.

C#
 private Point _pt;
 private Point _pt2;
private void picBoxImageProcessing_MouseDown(object sender, MouseEventArgs e)
{
    //// Starting point of the selection:
    //if (e.Button == MouseButtons.Left)
    //{
    //    _selecting = true;
    //    _selection = new Rectangle(new Point(e.X, e.Y), new Size());
    //}

    if (e.Button == MouseButtons.Left)
    {
        int ix = (int)(e.X / _zoom);
        int iy = (int)(e.Y / _zoom);

        //reset _pt2
        _pt2 = new Point(0, 0);
        _pt = new Point(ix, iy);

        // pictureBox1.Invalidate();
        picBoxImageProcessing.Invalidate();
    }
}

private void picBoxImageProcessing_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left && _selecting)
    {
        _selecting = false;
    }
}

private void picBoxImageProcessing_Paint(object sender, PaintEventArgs e)
{
    //if (_selecting)
    //{
    //    // Draw a rectangle displaying the current selection
    //    Pen pen = new Pen(Color.Red, 4);
    //    e.Graphics.DrawRectangle(pen, _selection);
    //}

    if (_selecting &&_pt.X >= 0 && _pt.Y >= 0 && _pt2.X >= 0 && _pt2.Y >= 0)
    {

        e.Graphics.DrawRectangle(pen, _pt.X * _zoom, _pt.Y * _zoom, (_pt2.X - _pt.X) * _zoom, (_pt2.Y - _pt.Y) * _zoom);
    }
}

private void picBoxImageProcessing_MouseMove(object sender, MouseEventArgs e)
{
    // Update the actual size of the selection:
    //if (_selecting)
    //{
    //    _selection.Width = e.X - _selection.X;
    //    _selection.Height = e.Y - _selection.Y;

    //    // Redraw the picturebox:
    //    picBoxImageProcessing.Refresh();
    //}

    if (e.Button == MouseButtons.Left)
    {
        _selecting = true;
        int ix = (int)(e.X / _zoom);
        int iy = (int)(e.Y / _zoom);

        _pt2 = new Point(ix, iy);

        //  pictureBox1.Invalidate();
        picBoxImageProcessing.Invalidate();
    }
}


there is no problem to draw the rectangle by mouse dragging. But if i want to change the height or width of the rectangle then I have to draw a new rectangle, that i don't want. I want to change the height and width of the rectangle by modifying the drawn rectangle instead of drawing a new rectangle.
I don’t want to know how to crop. I need to draw a resizable rectangle on the image as we can do in photoshop.

So I need a crop selection control like photoshop's crop control.
Posted
Comments
Sergey Alexandrovich Kryukov 6-Mar-12 14:48pm    
Are you still using PictureBox? All of lines with PictureBox are commented out...
Quick answer would be: don't use PictureBox, using it does not really help.
--SA
akul123 7-Mar-12 8:18am    
my picturebox control name is picBoxImageProcessing.
Sergey Alexandrovich Kryukov 7-Mar-12 19:29pm    
Thank you for clarification. Please see my answer: I explain why you don't need any and explain what to do instead.
Don't make your life harder than it has to be.
--SA

What you need to think about doing here is create yourself a UserControl that encapsulates a rectangle. This control will be responsible for the actual drawing of the rectangle, but more importantly, will know how to resize itself. I know that this is just repeating what you have already said, but bear with me for a moment.

This control should have mini rectangles drawn at the corners, and in the centre of each line; these will represent your grab handles. When the mouse is pressed down inside any of these control rectangles, then the rectangle can be resized depending on the direction of the mouse drag movement. Now, there are certain optimisations you can do - don't redraw the rectangle at every move - instead, you could apply a small offset so that the rectangle is only repainted once it passes the offset. To determine if you're in a grab box, you could use Rectangle.Contains to see if the mouse location is in that grab box (funnily enough, this is the second time I've recommended using this method today - there must be something about today).

I hope this gives you something to think about for the design.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Mar-12 19:28pm    
My 5.

I have something to add: there is no need to use PictureBox and no real use: it only makes implementation harder and does not offer any help. Please see my answer.
--SA
In addition to the idea of the design Pete offered in his answer, I would like to emphasize that you should not use PictureBox. All you need is a custom control derived from System.Windows.Forms.Control and do all the rendering in the handler of the event Paint or, better yet, in the overridden protected virtual method OnPaint.

The PictureBox is good only for simple presentation of a static picture, maybe replaced from time to time. Using this class for anything interactive, dynamic of animated is the abuse of it. In such cases, PictureBox does not provide any benefits, only extra hassles in development; it simply eats up some extra resources, performance and your development time offering nothing in return.

I explain it in detail in my past answers:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^].

See also my other past answers on related topics:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^].

—SA
 
Share this answer
 
v3
Comments
Pete O'Hanlon 8-Mar-12 2:05am    
A very valid addition to my answer. My 5.
Sergey Alexandrovich Kryukov 8-Mar-12 10:41am    
Thank you, Pete.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900