I add an paint event handler to a panel. So I paint two lines (like an X) on top of a PictureBox. But the "X" is underneath the PictureBox. Is there a way to make them be on top?
How does paint work? What is painted last is on top?
I add an paint event handler to a panel. So I paint two lines (like an X) on top of a PictureBox. But the "X" is underneath the PictureBox. Is there a way to make them be on top?
How does paint work? What is painted last is on top?
when u say the X is under the picture - is it still on the panel or are you having to move the panel to see the X ?
if the picturebox is on the panel then are you sure you are drawing it to the picturebox and not the panel paint routine ?
just a few thoughts - as you know im no expert but we all overlook things sometimes
The X is printed on the panel paint routine (panel.Pain). The layers are:
panel
pictureBox
X
So I see like the edges only of X, the rest is the PictureBox. Also I have to mention that the PictureBox has an image loaded on it, if that has to do with anything.
My understanding is that panel.Paint has already a paint event handler. That draws the controls. So when i do panel.Paint += new appropriateHandler(appropriateFun); Then one paint is called and then the other. But I would expect the first one to be called first. Is the above correct?
okay from my limited knowledge ( its limited trust me)...
i take it you want the X to appear over the image in the picturebox
1. is it not easier to create a graphic for the bitmap and then put your picture on it - then draw your cross as an overlay - then move it to the picturebox ?
2. when i was messing about with panels - a lot of stuff i read mentioned that you have to create a new panel derived from panel then try your override paint routine ? public void mypanel : panel
anychance of seeing the code ?
this kind of does what u want - the lines look odd because of the imagestretch
will need modding for your paint routine i guessCode:namespace paneltest1 { public partial class Form1 : Form { Bitmap mybitmap; Image myimage = Image.FromFile(@"c:\batman.jpg"); public Form1() { InitializeComponent(); Graphics mygraphic; mygraphic = Graphics.FromImage(myimage); mygraphic.DrawLine(new Pen(Color.Red), new Point(0, 0), new Point(myimage.Width, myimage.Height)); mygraphic.DrawLine(new Pen(Color.Red), new Point(myimage.Width, 0), new Point(0, myimage.Height)); pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox1.Image = myimage; } } }
just create new proj -with form, panel and picture box - and sub the picture for one of your own
Last edited by deviousdexter; 12-10-2008 at 05:22 PM.
This is the code:
Well, thats my code. So I have the picture boxes and I want to draw a cross on top of them. How would I do this?Code:... board.Paint += new PaintEventHandler(paintBoard); //board is the panel ... board.Invalidate(); ... .... private void paintBoard(object sender, PaintEventArgs e) { ... foreach (Xmark x in Util.Xmarks) { e.Graphics.DrawLine(x.pen, x.upLeft, x.downRight); e.Graphics.DrawLine(x.pen, x.upRight, x.downLeft); } ... }
The Graphics.FromImage() didn't change much for me
from what i can see here, you are painting to the panel not the picturebox. your e.Graphics is pointing to the panel paint not the picturebox paint, this is why your X is appearing behind your picturebox instead on on top of it.
You need to modify your paint routine so that it updates the picturebox. Ill take a quick look at this and get back hopefully if someone doesnt get there first.
Are you using more than one picturebox ?? if so is this an array or individually named ?
try modifying it something like this.Code:pictureBox1.Paint += new PaintEventHandler(paintBoard); . . . private void paintBoard(object sender, PaintEventArgs e) { mygraphic = e.Graphics; mygraphic.DrawLine(new Pen(Color.Black), 0, 0, pictureBox1.Width, pictureBox1.Height); mygraphic.DrawLine(new Pen(Color.Black), pictureBox1.Width, 0, 0,pictureBox1.Height); }
Thanx, that worked. Didn't know that there was a Paint for PictureBox
I did this:
There are multiple picture boxes. I choose which one by doingCode:private void dyingPawn(object sender, PaintEventArgs e) { PictureBox pic = (PictureBox)sender; e.Graphics.DrawLine(new Pen(Color.Black, 3f), 0, 0, pic.Width, pic.Height); e.Graphics.DrawLine(new Pen(Color.Black, 3f), pic.Width, 0, 0, pic.Height); }
which seems to be the most reliable/fast methodCode:pictureboxX.Paint += dyingPawn //turn on pictureboxX.Paint -= dyingPawn //turn off
glad i could help maybe more to do with bling's patience tbh lol
I was thinking how the whole Paint system works because I have another problem. The problem is that I want to make an object move. I choose a picturebox and make it move with a for loop, like:
I wan though for it to appear and re appear. So I do this:Code:for(...) { p.X = ... p.Y = ... picturebox.Location = p; Thread.Sleep(300); }
But it doesn't work. The last Hide() works, but the Show() doesn't. If I put the Show() in another place in the code it works, giving me the feeling that Show() needs more "time", if that makes sense (it doesn't to me).Code:picturebox.Show(); for(...) { p.X = ... p.Y = ... picturebox.Location = p; Thread.Sleep(300); } picturebox.Hide();
I guess an event is trigggered with Show() and it is not finished before another on is triggered with changing Location? Or something like that?
Show (and Hide) requires a repaint for the effects to be shown. This repaint is handled in the message loop, which you won't reach until your entire loop is done, in which case it will be immediately hidden again.
You could try calling
after the call to Show, and possibly in the loop too. Note though that it's probably better with a solution that does not block the main thread.Code:System.Windows.Forms.Application.DoEvents();
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.