954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help, Implementation of a selection tool

Hi,

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 :)

fkhaeer
Newbie Poster
4 posts since May 2005
Reputation Points: 11
Solved Threads: 0
 

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

Iron_Cross
Junior Poster
117 posts since Jul 2003
Reputation Points: 46
Solved Threads: 2
 

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.......

fkhaeer
Newbie Poster
4 posts since May 2005
Reputation Points: 11
Solved Threads: 0
 

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

Regards..

fkhaeer
Newbie Poster
4 posts since May 2005
Reputation Points: 11
Solved Threads: 0
 

Hi..

any HELPING IDEA!!!

fkhaeer
Newbie Poster
4 posts since May 2005
Reputation Points: 11
Solved Threads: 0
 

hi
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
g.DrawRectangle(Pens.Red,startPoint.X,startPoint.Y,
e.X-startPoint.X,e.Y-startPoint.Y);

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 ;

BlackSun
Light Poster
46 posts since Feb 2008
Reputation Points: 28
Solved Threads: 5
 

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).

JerryShaw
Posting Pro in Training
465 posts since Nov 2006
Reputation Points: 69
Solved Threads: 75
 

Hi,
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?
Thanx

Seffix
Newbie Poster
3 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

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.

JerryShaw
Posting Pro in Training
465 posts since Nov 2006
Reputation Points: 69
Solved Threads: 75
 

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
        this.Invalidate();
    }
}

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.

Seffix
Newbie Poster
3 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

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()
        {
            InitializeComponent();
        }

        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;
            }
            else
                _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;
                pictureBox1.Invalidate();    
            }
        }

        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

JerryShaw
Posting Pro in Training
465 posts since Nov 2006
Reputation Points: 69
Solved Threads: 75
 

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
Newbie Poster
3 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

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

JerryShaw
Posting Pro in Training
465 posts since Nov 2006
Reputation Points: 69
Solved Threads: 75
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You