Draw on top of picturebox?

Is it possible to draw on top of a picture box Here's my code so far:

Pen blackPen = new Pen(Color.Black, 3);

// Create rectangle for ellipse.

Rectangle rect = new Rectangle(0, 0, 200, 100);

// Draw ellipse to screen.

e.Graphics.DrawEllipse(blackPen, rect);

but it prints behind the controls on my form. Is there some way to 'send to back' everything except my ellipse

Please help

Peter



Answer this question

Draw on top of picturebox?

  • Mark Hollman

    I'd like to now make the ellipse semi transparent, but the this doesn't seem to work:

    e.Graphics.FillEllipse(new SolidBrush(Color.FromArgb(50,255,255,255)),rect);

    any thoughts why

    Thanks,

    Pete


  • JDFox

    The Paint event handler is called when a WM_PAINT message is processed by the control. You can cause a WM_PAINT message to be sent in a few different ways but the usual way is to call the Invalidate method on the control. So, for example, in the Tick event handler for the Timer you can update the location at which the ellipse should be drawn (posX, posY) and then call pictureBox1.Invalidate() to cause the control to repaint.

  • Kody Clemens

    Can you give me an example I'm not exactly sure what you mean.

    Thank you sir,

    Pete M


  • amanzoor

    Cool. Thank you.
  • Phil_Ramos

    Assuming that you're using CF 2.0, the Paint event on the PictureBox has apparently been marked as not supported. This is surprising as, from memory, painting on a PictureBox was something that came up frequently in the newsgroups with CF 1.0. Anyways, you can hook the Paint event manually, even though it is not shown in the Properties window at design-time or Intellisense in the editor. You will get a warning that this is not supported; however, I just tested this on Windows Mobile 5.0 PPC and it worked as expected.

    ...

    public Form1()
    {
    InitializeComponent();
    this.pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
    }

    void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
    using (Pen p = new Pen(Color.Black, 3))
    {
    e.Graphics.DrawEllipse(p, 0, 0, this.pictureBox1.Width, this.pictureBox1.Height);
    }
    }

    ...

    The other option is to create a new control, that inherits from the PictureBox control or some other control such as Control, override the OnPaint method, and perform any custom painting in this method.



  • Bahman; Fakhr

  • Yeowser

    you also can code the class yourself inherits with control, inside the class use the OnMouseDown, OnMouseMove and OnMouseUp methods.


  • shiggsy

    Assuming that you're using a PictureBox on a Form, and the Forms AutoScroll property is set to "true", then try setting the Forms AutoScrollPosition property.

  • rokana3

    You can paint on a PictureBox by hooking into the Paint event of the specific PictureBox and drawing any shapes, text, etc.

  • djMP2006

    My image is larger than the viewable screen on the ppc, so vertical and horizontal scrollbars automatically appear. Do you know of a scroll function in C# for these types of scrollbars Is there a way to scroll to a certain point

    Thanks in advance.

    peter


  • Steven Raybell

    I am trying to update the location of the ellipse on a timer tick.

    Here's my Paint method:

    void pictureBox1_Paint (object sender, PaintEventArgs e)
    {
    using (Pen p = new Pen(Color.Gray,2))
    {
    Rectangle rect - new Rectangle(posX, posY, 10,10);
    e.Graphics.DrawEllipse(p, rect);
    e.Graphics.FillEllipse(new SolidBrush(Color.Yellow), rect);
    }

    }

    Would it be possible remove the ellipse i just drew and then, then draw it to a new location

    Oh, and what is the syntax for calling this routine from a timer_tick method

    Thanks for all of your help by the way!

    Pete M


  • Draw on top of picturebox?