Creating images and manipulating them

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rduarte
    New Member
    • Jun 2009
    • 2

    Creating images and manipulating them

    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.FillEl lipse (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
  • IanWright
    New Member
    • Jan 2008
    • 179

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

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Originally posted by rduarte
      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.FillEl lipse (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 "destructiv e". 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.

      Comment

      • rduarte
        New Member
        • Jun 2009
        • 2

        #4
        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.Cont rols

        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.

        Comment

        • IanWright
          New Member
          • Jan 2008
          • 179

          #5
          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.DrawSt ring(). 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).

          Comment

          Working...