473,322 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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.



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
4 4601
GaryTexmo
1,501 Expert 1GB
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
3,525 Expert 2GB
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
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
1,501 Expert 1GB
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

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

Similar topics

1
by: Dennis | last post by:
Hello, Ive to draw a line on a picture i used this code: Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) x1 = z.X y1 = z.Y lbl3 = "x1: " & x1 lbl4...
2
by: Bill N. | last post by:
Using VB .Net 2003 Standard Edition. This simple form has a "File in Use" problem while the executable is running. Created this simple program to make sure that I hadn't done something in my...
21
by: DraguVaso | last post by:
Hi, I have an inherited DataGrid, that does lots of extra stuff. For exemple drawing a backgroundimage in every cell. The problem is that it's taking too much time (using gdi+), so I want to do...
3
by: Colin McGuire | last post by:
Hi there. I have written a small procedure to draw various shapes on things. A bit of it is shown below. Private Sub drawShape(ByVal shapeType As Integer, ByRef g As Graphics) Select Case...
8
by: Oberfuhrer | last post by:
I am an old mathematician, who in my sparetime work on a topic oriented math application. I have converted from old, dry Fortran to the more intuitive and juicy VB.net and Windows. In...
1
by: Jeff Waskiewicz | last post by:
Hello All, I'm trying to solve a nagging problem. The goal is to draw a rectangle over the top of all the other controls on a form. Specifically, over a ChartFX control. The user would draw...
4
by: annsh | last post by:
Hi On a windows form - I want to be able to allow a user to draw an image (on a graphics tablet). Its for an application for selling carpets - customer comes in and describes room where carpet...
1
by: martinsmith160 | last post by:
Hi all I am trying to create a level builder tool for a final year project and im having some problems drawing. I have placed a picture box within a panel so i can scroll around the image which is...
8
by: martinsmith160 | last post by:
Hi all I have a picture box placed within a panel so i can scroll across the picture due to it being very wide. My problem is I want to draw an image over the picture box when a user clicks at a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.