473,386 Members | 1,803 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,386 software developers and data experts.

How to make lines appear in front of picturebox?

82
Expand|Select|Wrap|Line Numbers
  1. namespace TestControl
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.           private void Form1_Load(object sender, EventArgs e)
  11.         {
  12.             this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
  13.         }
  14.  
  15.           protected override void OnPaint(PaintEventArgs e)
  16.           {
  17.               drawRobot();
  18.           }
  19.  
  20.         private void drawRobot()
  21.         {
  22.          Pen MaplinePen = new Pen(System.Drawing.Color.LightGray);
  23.             Pen RobotEllipsePen = new Pen(System.Drawing.Color.Red);
  24.  
  25.             int RobotOriginX = (this.ClientSize.Width / 2); // -(RobotellipseWidth / 2);
  26.             int RobotOriginY = (this.ClientSize.Height / 2); // -(RobotellipseHeight / 2);
  27.  
  28.             int RobotellipseWidth = 20;
  29.             int RobotellipseHeight = 20;
  30.  
  31.             Graphics grphx = this.CreateGraphics();
  32.  
  33.             //Draw RobotElipse
  34.             for (int n = 0; n < 5; n++)
  35.             {
  36.                 grphx.DrawEllipse(RobotEllipsePen,
  37.                 RobotOriginX - (RobotellipseWidth / 2),
  38.                 RobotOriginY - (RobotellipseHeight / 2),
  39.                 (RobotellipseWidth + n),
  40.                 (RobotellipseHeight + n));
  41.                 RobotellipseWidth = RobotellipseWidth + 20;
  42.                 RobotellipseHeight = RobotellipseHeight + 20;
  43.             }
  44.        }
  45.  
  46.         private void openToolStripMenuItem_Click(object sender, EventArgs e)
  47.         {
  48.             OpenFileDialog openFileDialog1 = new OpenFileDialog();
  49.  
  50.             openFileDialog1.Title = "Open Image File";
  51.             openFileDialog1.Filter = "Bitmap Files|*.bmp" +
  52.                 "|Enhanced Windows MetaFile|*.emf" +
  53.                 "|Exchangeable Image File|*.exif" +
  54.                 "|Gif Files|*.gif|Icons|*.ico|JPEG Files|*.jpg" +
  55.                 "|PNG Files|*.png|TIFF Files|*.tif|Windows MetaFile|*.wmf";
  56.  
  57.             openFileDialog1.FilterIndex = 6;
  58.             openFileDialog1.FileName = "";
  59.             openFileDialog1.ShowDialog();
  60.  
  61.             if (openFileDialog1.FileName == "")
  62.                 return;
  63.  
  64.             // set the extended picturebox control's
  65.             // image to the open file dialog's filename
  66.             this.xtendPicBox1.PictureFile = openFileDialog1.FileName;
  67.         }
  68.  
  69.  
  70.         private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  71.         {
  72.             this.Dispose();
  73.         }  
  74.  
  75.     }
  76. }
  77.  
Nov 30 '10 #1
20 8009
GaryTexmo
1,501 Expert 1GB
Instead of creating a new graphics object, use the one that comes with the parameter of the OnPaint method you over rode. It's in the PaintEventArgs parameter as a property named Graphics.

Then you can update your drawRobot method to take a Graphics parameter.

Expand|Select|Wrap|Line Numbers
  1.           protected override void OnPaint(PaintEventArgs e)
  2.           {
  3.               drawRobot(e.Graphics);
  4.           }
Expand|Select|Wrap|Line Numbers
  1.         private void drawRobot(Graphics g)
  2.         {
  3.             Graphics grfx = g;
  4.             ...
  5.         }
It's also worth noting here that you are drawing on the form, not the PictureBox. If you want to draw on the PictureBox, you will need to use the Paint event of that control.
Nov 30 '10 #2
mrcw
82
Hi, I put those commands in, but the robot is still drawn behind the .jpg. I don't know why this is
thanks
Dec 3 '10 #3
GaryTexmo
1,501 Expert 1GB
As I mentioned, you're currently drawing on the form. So it's quite likely that your PictureBox will draw over top. Instead of using the Form's paint event, use the one on the PictureBox itself. There should be an OnPaint event you can use.
Dec 3 '10 #4
mrcw
82
I think I've tried that (look at the final two methods. ps I redesigned the form to make it easier for me to understand.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace xpic3
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void Form1_Load(object sender, EventArgs e)
  20.         {
  21.             this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
  22.             pictureBox1.Image = Image.FromFile("c:/Users/m/Desktop/map-blank.jpg");
  23.             hScrollBar.Minimum = 0;
  24.             hScrollBar.Maximum = pictureBox1.Width;
  25.             vScrollBar.Minimum = 0;
  26.             vScrollBar.Maximum = pictureBox1.Height;
  27.          }
  28.  
  29.         private void vScrollBar1_ValueChanged(object sender, EventArgs e)
  30.         {
  31.             pictureBox1.Top = vScrollBar.Value;
  32.         }
  33.  
  34.         private void hScrollBar_ValueChanged(object sender, EventArgs e)
  35.         {
  36.             pictureBox1.Left = hScrollBar.Value;
  37.         }
  38.  
  39.         private void drawRobot(Graphics g)
  40.         {
  41.             Graphics grfx = g;
  42.             Pen RobotEllipsePen = new Pen(System.Drawing.Color.Red);
  43.  
  44.             int RobotOriginX = (this.ClientSize.Width / 2); // -(RobotellipseWidth / 2);
  45.             int RobotOriginY = (this.ClientSize.Height / 2); // -(RobotellipseHeight / 2);
  46.  
  47.             int RobotellipseWidth = 20;
  48.             int RobotellipseHeight = 20;
  49.  
  50.             Graphics grphx = this.CreateGraphics();
  51.  
  52.             //Draw RobotElipse
  53.             for (int n = 0; n < 5; n++)
  54.             {
  55.                 grphx.DrawEllipse(RobotEllipsePen,
  56.                 RobotOriginX - (RobotellipseWidth / 2),
  57.                 RobotOriginY - (RobotellipseHeight / 2),
  58.                 (RobotellipseWidth + n),
  59.                 (RobotellipseHeight + n));
  60.                 RobotellipseWidth = RobotellipseWidth + 20;
  61.                 RobotellipseHeight = RobotellipseHeight + 20;
  62.             }
  63.         }
  64.  
  65.         protected override void OnPaint(PaintEventArgs e)
  66.         {
  67.             // drawRobot(e.Graphics);
  68.         }
  69.         private void pictureBox1_OnPaint(object sender, PaintEventArgs e)
  70.         {
  71.             drawRobot(e.Graphics);
  72.         }  
  73.     }
  74. }
Dec 3 '10 #5
GaryTexmo
1,501 Expert 1GB
I can't see where you've attached to the event... it's actually the Paint event on a PictureBox. Have it set to your pictureBox1_OnPaint method, it should work. I've got similar going on my end and experiencing no troubles.
Dec 3 '10 #6
mrcw
82
haven't I done that with the last method

drawRobot(e.Graphics);?
Dec 3 '10 #7
GaryTexmo
1,501 Expert 1GB
No it still needs to be called. You've got a method there that does nothing. You need to have it attach to the paint method of the picturebox. You can either do it via the designer, or...

Expand|Select|Wrap|Line Numbers
  1. pictureBox1.Paint += new PaintEventHandler(YOUR METHOD NAME HERE);
... though it's probably easier to do via designer.
Dec 3 '10 #8
mrcw
82
I seem to be going backwards. I have re-done the form again, but the drawing is still in the form not in the paintbox. I am using the picturebox paint event. The picturebox paint event appears to be attached to the right place in the designer.

The drawrobot method has gone
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace picbox_test1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void pictureBox1_Paint(object sender, PaintEventArgs e)
  20.         {
  21.            // Graphics grfx = g;
  22.             Pen RobotEllipsePen = new Pen(System.Drawing.Color.Red);
  23.  
  24.             int RobotOriginX = (this.ClientSize.Width / 2); 
  25.             int RobotOriginY = (this.ClientSize.Height / 2); 
  26.  
  27.             int RobotellipseWidth = 20;
  28.             int RobotellipseHeight = 20;
  29.  
  30.             Graphics grphx = this.CreateGraphics();
  31.  
  32.             //Draw RobotElipse
  33.             for (int n = 0; n < 5; n++)
  34.             {
  35.                 grphx.DrawEllipse(RobotEllipsePen,
  36.                 RobotOriginX - (RobotellipseWidth / 2),
  37.                 RobotOriginY - (RobotellipseHeight / 2),
  38.                 (RobotellipseWidth + n),
  39.                 (RobotellipseHeight + n));
  40.                 RobotellipseWidth = RobotellipseWidth + 20;
  41.                 RobotellipseHeight = RobotellipseHeight + 20;
  42.             }
  43.         }
  44.     }
  45. }
  46.  
the designer code is
Expand|Select|Wrap|Line Numbers
  1. namespace picbox_test1
  2. {
  3.     partial class Form1
  4.     {
  5.         /// <summary>
  6.         /// Required designer variable.
  7.         /// </summary>
  8.         private System.ComponentModel.IContainer components = null;
  9.  
  10.         /// <summary>
  11.         /// Clean up any resources being used.
  12.         /// </summary>
  13.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14.         protected override void Dispose(bool disposing)
  15.         {
  16.             if (disposing && (components != null))
  17.             {
  18.                 components.Dispose();
  19.             }
  20.             base.Dispose(disposing);
  21.         }
  22.  
  23.         #region Windows Form Designer generated code
  24.  
  25.         private void InitializeComponent()
  26.         {
  27.             this.pictureBox1 = new System.Windows.Forms.PictureBox();
  28.             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
  29.             this.SuspendLayout();
  30.             // 
  31.             // pictureBox1
  32.             // 
  33.             this.pictureBox1.Location = new System.Drawing.Point(38, 158);
  34.             this.pictureBox1.Name = "pictureBox1";
  35.             this.pictureBox1.Size = new System.Drawing.Size(100, 50);
  36.             this.pictureBox1.TabIndex = 0;
  37.             this.pictureBox1.TabStop = false;
  38.             this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
  39.             // 
  40.             // Form1
  41.             // 
  42.             this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
  43.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  44.             this.ClientSize = new System.Drawing.Size(282, 255);
  45.             this.Controls.Add(this.pictureBox1);
  46.             this.Name = "Form1";
  47.             this.Text = "Form1";
  48.             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
  49.             this.ResumeLayout(false);
  50.  
  51.         }
  52.         #endregion
  53.  
  54.         private System.Windows.Forms.PictureBox pictureBox1;
  55.     }
  56. }
  57.  
I'm totally lost now I can't see what I'm doing wrong.
Dec 5 '10 #9
GaryTexmo
1,501 Expert 1GB
I tried your code, it didn't work at all and it's because you have made a mistake on line 30 of your paint method.

Expand|Select|Wrap|Line Numbers
  1. Graphics grphx = this.CreateGraphics();
... should be ...

Expand|Select|Wrap|Line Numbers
  1. Graphics grphx = e.Graphics;
Other than that, it works fine. If it looks like things are drawing on the form, it's only because you don't have an image there. It's actually drawing on the picture box.
Dec 6 '10 #10
mrcw
82
I put a .jpg in the pictureBox using
Expand|Select|Wrap|Line Numbers
  1. pictureBox1.Image = Image.FromFile("c:/Users/m/Desktop/map-blank.jpg");
in the Form1_load event and the stuff I've drawn still disappears behind the picturebox
Dec 6 '10 #11
GaryTexmo
1,501 Expert 1GB
I just tested again, it's working for me. The drawing comes up over top of the image. This is with the code you posted, other than the correction I pointed out in my last post. Have you corrected that line in yours?
Dec 6 '10 #12
mrcw
82
yes I corrected my code. I have the pictureBpx over half the form1 and the robot graphics seem to be on the form1 background, The picturebox makes half of the robot graphic disappear. Also if I move the picturebox the robot stays where it is even it's not anywhere near the picturebox. Surely it should move when I move the pictureBox?
Dec 7 '10 #13
GaryTexmo
1,501 Expert 1GB
The reason for that is because of where you're drawing it. Look at lines 24 and 25 in your code. You're defining the origin as the center of the form, but when you're drawing on a control the origin is the top-left of that control, which is (0, 0). If your form size is (300, 300), your origin gets defined as (150, 150). If your picturebox is (50, 50) in size, then you're not going to see the whole picture because you're drawing in the bottom right of the picture box, outside its bounds.

As for it moving when you move the picturebox... I've copied and pasted your code into a project and it is doing exactly that. If yours isn't, you need to look over your code again and make sure you're drawing in the right place.

Remember, when you're drawing on a control the coordinate (0, 0) will always be in the top-left of the control you're drawing on.
Dec 7 '10 #14
mrcw
82
lines 24 and 25 should ensure the robot is drawn at the centre of the pictureBox. But it is drawn in the centre of the form. I have got a 700 x 700 form with the paintbox 200 x 200 at location 350, 420. The pictureBox is nowhere near the pictureBox, yet it still shows, yet according to you it should be out of the pictureBox bounds.
Dec 7 '10 #15
mrcw
82
paintbox should say pictureBox (sorry I can't shpell...Hic)
Dec 7 '10 #16
GaryTexmo
1,501 Expert 1GB
No, the "this" reference is for the form, so you're accessing properties on the form, not the pictureBox... this is why it draws in the center of the form, but yes this should be an offset to the origin of the picturebox. When I ran your code it was always drawing the concentric rings in the bottom-left of my picturebox, out of the bounds. You couldn't see the center point, but you could see part of the rings.

This was with a copy-paste of the code you posted, making the appropriate change to the graphics object.

Try this, make your origin variables both zero and see where your rings draw. Move your picturebox around and see the result.
Dec 7 '10 #17
mrcw
82
I understand how "this" works now - thanks, I removed all references to "this" and made the origin variables 0. Now the bottom right quadrant of the rings is visible in the top left of the form and moving the picturebox makes no difference. This doesn't make sense but I can't see why. This is what I'm reduced to

Expand|Select|Wrap|Line Numbers
  1. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  2.         {
  3.             Pen RobotEllipsePen = new Pen(System.Drawing.Color.Red);
  4.  
  5.             int RobotOriginX = 0;  
  6.             int RobotOriginY = 0; 
  7.  
  8.             int RobotellipseWidth = 20;
  9.             int RobotellipseHeight = 20;
  10.  
  11.             Graphics grphx = CreateGraphics();
  12.  
  13.             for (int n = 0; n < 5; n++)
  14.             {
  15.                 grphx.DrawEllipse(RobotEllipsePen, 
  16.                 RobotOriginX - (RobotellipseWidth / 2),
  17.                 RobotOriginY - (RobotellipseHeight / 2),
  18.                 (RobotellipseWidth + n),
  19.                 (RobotellipseHeight + n));
  20.                 RobotellipseWidth = RobotellipseWidth + 20;
  21.                 RobotellipseHeight = RobotellipseHeight + 20;
  22.             }
  23.         }
Dec 8 '10 #18
GaryTexmo
1,501 Expert 1GB
Line 11, you're still using CreateGraphics instead of e.Graphics. I pointed it out in post 2 and again in post 10. You said in post 13 that you fixed this, yet clearly you did not. As it was at the very start, this is the reason you're not getting things drawing where you want them... you're drawing on the wrong control.

CreateGraphics is a form method... just because you remove the this reference doesn't mean it won't find the method. Creating a new graphics object from the form means you're still going to draw on the form. The event arguments for the paint event on a control give you the graphics object for that control. If you want to draw on that control, you need to use the graphics object associated with that control.

Please make sure you're using the correct graphics object. Give it another try, it should work for you.
Dec 8 '10 #19
mrcw
82
It works!!

Thank you
Dec 8 '10 #20
GaryTexmo
1,501 Expert 1GB
Glad you got it working. Remember, each control has its own graphics object and will always draw relative to itself. Good luck with the rest of your project! :)
Dec 8 '10 #21

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

Similar topics

2
by: chanmmn | last post by:
How to make dll appear in .Net tag when Add References? Please help chanmm
4
by: giddy | last post by:
hi when i run this class i made here , this is what it looks like without text - http://gidsfiles.googlepages.com/LinedTextBox_1.jpg WITH TEXT (heres the issue) -...
2
by: alizabeth33 | last post by:
I want to make an image pop up when my text is clicked and I can not figure out how to do that. Instead of having a new page come up, I want just the image to pop up and I know you can do it through...
5
by: ryanmhuc | last post by:
Is it possible to make the scrollbar appear on a page which is NOT A POPUP? On IE the scollbar space is always reserved or appears even if not used. In Firefox it does not exist unless the...
10
by: moondaddy | last post by:
Hi, I have 3 tiny content controls and each has a Path in it to draw some lines. These need to be very small and I'm having trouble making the lines clear. the lines have a stroke thickness of "1",...
1
by: azura | last post by:
i had a problem... how can i auto detect that bold n italic field..when i enter the matric no, then the name will auto appear... i want to make this field using less button.. thanks <table...
1
by: amirayat | last post by:
Hi dear, help me to make lines with matplotlib by 2 points. for example I'm going to plot a line . What should I import? matplotlib? pylab? Best Amirayat
8
Jerry Maiapu
by: Jerry Maiapu | last post by:
Hi everyone, I have just created a form and made it to stay on top of non-Microsoft Access forms and windows using examples modules here: http://support.microsoft.com/kb/210500. Which works fine. ...
4
by: Will The Gray | last post by:
Hi all I'm trying to let images appear after a fixed amount of seconds. So 3 second wait, image 1 appears, 3 second wait, image 2 appears, 3 second wait and image 3 appears. I've tried...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.