Connecting Tech Pros Worldwide Forums | Help | Site Map

Creating images and manipulating them

Newbie
 
Join Date: Jun 2009
Posts: 2
#1: Jun 18 '09
Hello, I'm new to Windows Forms development in C#, and I'm having some trouble with a college project.

Simply put, it is a paint-like application, that has a toolbox to draw shapes where the mouse points.
However, these shapes must be selectable and moveable. Also, there is a tricky tool that draw a line between 2 shapes and when one of the linked shapes moves, the line must still be linked to both shapes.
I tried to do this using Graphics.FillEllipse (and such) and managed to draw lines and shapes on a PictureBox, however, I don't know how could I make them selectable nor moveable... I'm trying to use Regions now to do so, but I'd like to know if there is another way to do these things...
Any ideas?

Thanks in advance

Expert
 
Join Date: Jan 2008
Location: York
Posts: 179
#2: Jun 18 '09

re: Creating images and manipulating them


Sounds like you are almost there. What you probably want to do is create classes of shapes that know their position and size.

Using these you can do some collision detection (is mouse within the given shape?) and each class can know how to draw itself given a Graphics context.

As for the lines, you'll need to maintain some form of collection of lines that are connected, and translate their end point whenever the shape moves.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,778
#3: Jun 18 '09

re: Creating images and manipulating them


Quote:

Originally Posted by rduarte View Post

Hello, I'm new to Windows Forms development in C#, and I'm having some trouble with a college project.

Simply put, it is a paint-like application, that has a toolbox to draw shapes where the mouse points.
However, these shapes must be selectable and moveable. Also, there is a tricky tool that draw a line between 2 shapes and when one of the linked shapes moves, the line must still be linked to both shapes.
I tried to do this using Graphics.FillEllipse (and such) and managed to draw lines and shapes on a PictureBox, however, I don't know how could I make them selectable nor moveable... I'm trying to use Regions now to do so, but I'd like to know if there is another way to do these things...
Any ideas?

Thanks in advance

Sounds like the difference between a 'paint' program where you have one layer and once you change the pixels you're done, and a 'draw' program that has objects on screen. Think of Photoshop versus Illustrator.

As Ian pointed out you will need to actually track each object individually so you can grab and move it again later. If you are only working by painting on a single layer that is considered "destructive". I.e. if you make a circle, half covering a square you actually replace those pixels. There would be no way to then re-create those replaced pixels to pick up the square again.
Newbie
 
Join Date: Jun 2009
Posts: 2
#4: Jun 18 '09

re: Creating images and manipulating them


Thanks for the quick response! Much appreciated =]

So, the way to go is by Region (to detect collision). I will keep on the same track then...

For every shape that is drawn, I also create a Region object with the same size and position of the shape. Then, I use the Regions to determine wheter a click happened in some region or in a blank space. I guess this will solve the moving problem.

What about the textbox problem? Every line has a label on it, and this label must be editable... Drawing pixels is one thing, but how do I create component in run-time?
Perhaps I should just draw the word and save a Region, and when the region is selected the new text is prompted.I don't know how to make a textbox appear there either...

To create a component in run-time I tried the following:
1- Declared a new component (a Label for example)
2- Set position, text, name, font, color, etc of the component
3- Added the component to PictureBox.Controls

But the component won't show... Do I have to refresh or update the display somehow?


In the following picture, the "AB" text above the line must be editable on onclick event (sorry about the quality of the picture, it's just a concept hehe)



Thanks again for the fast and useful answers.
Expert
 
Join Date: Jan 2008
Location: York
Posts: 179
#5: Jun 19 '09

re: Creating images and manipulating them


In the picture you've got. Don't attempt to use a control to store your text.

Again this wants to be in a shape class and behave in a similar way, then you can use Graphics.DrawString(). The region around it should basically be the bounding box of the text given it's font and size.

I'm not sure about the editing, you might be able to do this via a dialog? If not then you'll have to find some way to use a textbox control floating over the picture box at the correct location. (I'm not 100% sure how thats gonna work out).
Reply

Tags
drawing, images, moving, picturebox