How to draw primitives over a picture box on a windows form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • martinsmith160
    New Member
    • Dec 2009
    • 16

    How to draw primitives over a picture box on a windows form

    Hi Everyone

    I am creating a level bulider using windows forms and the background for the level is stored in a picture box control. All i want to do is draw a number of lines over the image to create a grid allowing better placement on the builder.

    here is my code so far. It draws a single line but if i place a control over it, it is drawn over.



    Code:
    namespace GraphicsTest
    {
        public partial class Form1 : Form
        {
            private Bitmap m_myLine;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                m_myLine = new Bitmap(this.ClientRectangle.Width,
                    this.ClientRectangle.Height,
                    System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                InitializeImage();
            }
    
            private void InitializeImage()
            {
                Graphics objGraphics;
                objGraphics = Graphics.FromImage(m_myLine);
    
                objGraphics.Clear(SystemColors.Control);
    
                objGraphics.DrawLine(Pens.Orange, 10, 0, 10, 50);
    
                objGraphics.Dispose();
            }
    
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                e.Graphics.DrawImage(m_myLine, 0, 0, m_myLine.Width,
    m_myLine.Height);
                e.Graphics.Dispose();
                this.Invalidate();
    
            }
    
            private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                m_myLine.Dispose();
            }
    
            
        }
    }
    any help would be greatly appreciated.
    Martin.
    Last edited by tlhintoq; Dec 6 '09, 06:08 AM. Reason: [CODE] ... your code here ... [/CODE] tags added
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    It looks like you're painting on the form itself, so the picturebox draws overtop. Can you perhaps do the drawing on the picture box's paint event, or even overload it's OnPaint method?

    I'll also throw this out there, perhaps check out XNA. It's Microsoft's new game programming framework built on .NET. It's fairly easy to use and there's tons of resources for you on the internet. Good luck!

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Does the grid have to be continually updated? That's what happens when you do your drawing in the "on paint" method.

      If you only need the grid drawn one time, then do you in a conventional m

      You could also do your grid drawing then invalidate the one control so it repaints *after* your grid is drawn.

      Comment

      • martinsmith160
        New Member
        • Dec 2009
        • 16

        #4
        Thanks alot for the response. I am using XNA to build and run my game but im using windows forms to build the level builder. I have decided because the background image is always the same size i have overload a grid layer ontop of the backgound image using photoshop and then just used the new image in the picture box instead of drawing on the picture box itself. Thanks everyone!

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          Oh ok cool :) Well the only other thing I'll say is that if you're using win forms for your level builder because you want some of the functionality of windows forms, you actually can embed an XNA game window into a form and put other in forms on top of it. I don't know if you know this already, but if not it might help you out. As I understand it, a lot of level builders work this way since you can basically recycle your level drawing engine in your editor.

          Still, it sounds like you've got your approach figured out, so you probably don't need to worry about it, I just wanted to mention it :)

          Comment

          Working...