Thread: Paint Layers

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    Paint Layers

    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?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    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

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    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

    Code:
    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;
               
            }
        }
    }
    will need modding for your paint routine i guess

    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.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    This is the code:
    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);
     } 
    ...
    }
    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?
    The Graphics.FromImage() didn't change much for me

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    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.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    Are you using more than one picturebox ?? if so is this an array or individually named ?

    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);
                  
            }
    try modifying it something like this.

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Thanx, that worked. Didn't know that there was a Paint for PictureBox

    I did this:
    Code:
            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);
            }
    There are multiple picture boxes. I choose which one by doing
    Code:
     pictureboxX.Paint += dyingPawn  //turn on
     pictureboxX.Paint -= dyingPawn   //turn off
    which seems to be the most reliable/fast method

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    glad i could help maybe more to do with bling's patience tbh lol

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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:
    Code:
    for(...)
    {
       p.X = ...
       p.Y = ...
       picturebox.Location = p;
       Thread.Sleep(300);
    }
    I wan though for it to appear and re appear. So I do this:

    Code:
    picturebox.Show();
    for(...)
    {
       p.X = ...
       p.Y = ...
       picturebox.Location = p;
       Thread.Sleep(300);
    }
    picturebox.Hide();
    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).
    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?

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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
    Code:
    System.Windows.Forms.Application.DoEvents();
    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.
    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.

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Thanx! That is what I wanted

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory layers in c
    By sreeramu in forum C Programming
    Replies: 3
    Last Post: 10-19-2007, 09:44 AM
  2. Paint BMP from another function
    By Sober in forum Windows Programming
    Replies: 6
    Last Post: 05-01-2007, 08:10 AM
  3. How to paint YUV bitmaps ?
    By Hermisky in forum Windows Programming
    Replies: 5
    Last Post: 03-27-2007, 08:35 AM
  4. composing layers is openGL
    By hannibar in forum Game Programming
    Replies: 1
    Last Post: 03-18-2006, 09:31 AM
  5. HELP - Thread can't use any paint functions
    By vulcan_146 in forum Windows Programming
    Replies: 1
    Last Post: 04-17-2004, 10:58 PM