I would like to implement a selection tool that is used for images. It selects a part of an image to enable the user for any further usage.

So, what is the simplest way to do such a thing?!

Any help will be welcome :)

Recommended Answers

All 12 Replies

You want to be able to select part of an image so the user can have further usage? Please rephrase ;)

My application deals with images. I want the user to select a part of an image, a text in the image for example. That selection part will be saved, copied, moved or any thing :evil:

I hope you could understand now :)

I am waiting.......

By the way, I found in somewhare, what i want is called: rubberband.




u can use GDI+ in c# and implement selection tool with my own i
i used MouseDownevent , MouseMoveevent , MouseUpevent and PaintEvent

declare refernce of Point as start point for ex:
Point startPoint = Point.empty;
Write in Mouse Down event:
startPoint = new (e.X,e.Y);
Write in Mouse Move event
Graphics g = this.CreateGraphics()l

that`s it
sorry i did not send the code because am work here and unfortunlly i don`t have c# editor :( . and sorry i forget daniweb convention for writting code ;

As BlackSun explained, you will need to manage it yourself using the image Graphics. It is a bit more complicated than the example above, but should move you into the right direction. The image onPaint event will have to remember that you have an active selection (the rectangle, and its deminsions) so that if you refresh the form, the selection will be persistent. You may want to use a popup menu, or something to grab that selection of the image and copy it to another bitmap object for processing (send to file, drag drop, whatever).

I've tried BlackSun solution, but I can't see the rectangle.
I've brought the picturebox to background, and still the rectangle is invisible.

do someone have an idea?

Are you drawing the rectangle inside of the Paint event of the picture box ?
Need to, and sending the picturebox to the background has no effect because you are drawing on top of the object no matter where it is in the Z order.

This is what I wrote:

private void pictureBox_MouseDown(object sender, MouseEventArgs e)
    start = true;
    startPoint = e.Location;

private void pictureBox_MouseMove(object sender, MouseEventArgs e)
    if (start) //if the mouse is pressed
        //set rectangle size&location
        rect.X = startPoint.X;
        rect.Y = startPoint.Y;
        rect.Width = e.X - startPoint.X;
        rect.Height = e.Y - startPoint.Y;
        //force the form to redrawn

private void pictureBox_Paint(object sender, PaintEventArgs e)
    graph = e.Graphics;
    graph.DrawRectangle(Pens.Red, rect.Top, rect.Left, rect.Width, rect.Height);

And still I can't see the rectangle.

There are a number of issues with your snippet, but its just a snipet, so I won't go into that....
Here is a snippet I wrote for you, starting with your own code.

using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DW_Rect1
    public partial class Form1 : Form
        private bool _start = false;
        private Rectangle _rect = new Rectangle(10, 10, 0, 0);

        public Form1()

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            if (e.Button == MouseButtons.Left)
                _start = true;
                _rect.X = e.Location.X;
                _rect.Y = e.Location.Y;
                _rect.Width = 0;
                _rect.Height = 0;
                _start = false;

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
            if (_start && e.Button == MouseButtons.Left)                    
                _rect.Width = e.X - _rect.X;
                _rect.Height = e.Y - _rect.Y;

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
            e.Graphics.DrawRectangle(Pens.Red, _rect);


Review the changes, and ask questions about why I did this or that.
// Jerry

Thanks a lot!
Now I am seeing all the mess in what I wrote...
All what you wrote makes sense, but I'll glad to know why are you put a '_' before each variable? I assume this is the proffesional syntax but as a beginner I haven't get any explenation about this.

Seffix, Good question. Local class variables starting with an underscore and all lower case is a coding standard. You will see many Microsoft code examples using this standard.

This standard makes it easy for others to see code snippets and understand that "these" variables are class scoped variables.

Most of the people I work with used to work at Microsoft, so it was natural for our company has adopted their standards.

// Jerry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.