By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,636 Members | 1,693 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,636 IT Pros & Developers. It's quick & easy.

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

P: 16
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.



Expand|Select|Wrap|Line Numbers
  1. namespace GraphicsTest
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         private Bitmap m_myLine;
  6.  
  7.         public Form1()
  8.         {
  9.             InitializeComponent();
  10.         }
  11.  
  12.         private void Form1_Load(object sender, EventArgs e)
  13.         {
  14.             m_myLine = new Bitmap(this.ClientRectangle.Width,
  15.                 this.ClientRectangle.Height,
  16.                 System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  17.             InitializeImage();
  18.         }
  19.  
  20.         private void InitializeImage()
  21.         {
  22.             Graphics objGraphics;
  23.             objGraphics = Graphics.FromImage(m_myLine);
  24.  
  25.             objGraphics.Clear(SystemColors.Control);
  26.  
  27.             objGraphics.DrawLine(Pens.Orange, 10, 0, 10, 50);
  28.  
  29.             objGraphics.Dispose();
  30.         }
  31.  
  32.         private void Form1_Paint(object sender, PaintEventArgs e)
  33.         {
  34.             e.Graphics.DrawImage(m_myLine, 0, 0, m_myLine.Width,
  35. m_myLine.Height);
  36.             e.Graphics.Dispose();
  37.             this.Invalidate();
  38.  
  39.         }
  40.  
  41.         private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  42.         {
  43.             m_myLine.Dispose();
  44.         }
  45.  
  46.  
  47.     }
  48. }
any help would be greatly appreciated.
Martin.
Dec 5 '09 #1
Share this Question
Share on Google+
4 Replies


GaryTexmo
Expert 100+
P: 1,501
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!
Dec 6 '09 #2

tlhintoq
Expert 2.5K+
P: 3,525
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.
Dec 6 '09 #3

P: 16
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!
Dec 7 '09 #4

GaryTexmo
Expert 100+
P: 1,501
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 :)
Dec 7 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.