1

I have a picture box which playing video from DirectShow. I want to draw a rectangle overlay on picture box to grab a image in that rectangle only. I found some samples that draw rectangle on picture box, but not for picture box which is playing video. If someone has experience on this, please show me how to do that. I'm very appreciated. Thank you a lot.

************Update*****

As you can see the picture below, i created a usercontrol and add it to controls of form, i place this usercontrol on a picturebox(which is playing video)

mOverlay = new MyUserControl();
mOverlay.Location = new Point(50,50);
Form1.Controls.Add(mOverlay);           
mOverlay.BringToFront();

I draw a red rectangle on this usercontrol, although i set transparent for this usercontrol like this, but it did not work:

SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent; //this here is MyUsercontrol

enter image description here

How can i make this usercontrol is transparent background so that i can see the video beneath?Thanks a lot.

2
  • I think you'd have to "overlay" a separate control over the PictureBox (it'd be contained by the form, not the PictureBox). Use GraphicsPath() and the Region() property of that control to "punch" a hole in it so that it appears to be a rectangle, and then you could see the video underneath/through it. Haven't tested it...just thinking out loud.
    – Idle_Mind
    Dec 2, 2013 at 16:17
  • Can you please see the update of my question!
    – MichaelP
    Dec 3, 2013 at 3:21

2 Answers 2

1

I meant something more like this:

See Through Rectangle via GraphicsPath and Region

public partial class MyUserControl : UserControl
{
    public MyUserControl()
    {
        InitializeComponent();
        this.BackColor = Color.Red;
        this.SizeChanged += MyUserControl_SizeChanged;
    }

    void MyUserControl_SizeChanged(object sender, EventArgs e)
    {
        System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
        Rectangle rc = this.ClientRectangle;
        gp.AddRectangle(rc);
        rc.Inflate(-4, -4);
        gp.AddRectangle(rc);
        this.Region = new Region(gp);
    }
}
3
  • in this way, can you move or resize the rectangle with mouse?
    – MichaelP
    Dec 3, 2013 at 7:07
  • You'd have to write a bunch of code to allow it to be moved/resized. I've got a version in VB.Net. I'll post a C# version later if you want to see it.
    – Idle_Mind
    Dec 3, 2013 at 12:50
  • Can you please post your code to allow the rectangle to be moved/resized? Thank you very much.
    – MichaelP
    Jan 13, 2014 at 2:21
1

I have done that few weeks ago with other camera, If you want yo draw rectangle on Picture box just handle the paint event of your picture box and draw your rectangle like;

    private void pictuerBox_Paint(object sender, PaintEventArgs e)
    {
         if(IsDrawRect) // Flag Variable to check if need to draw rect
              {

                Rectangle RectMark = new Rectangle(startX,StartY,Hieght,Widht); // your location to draw
                e.Graphics.DrawRectangle(new Pen(Color.Red, 1), RectMark);
               }
    }

then you have to clip the image from picturebox ;

      //  Create Temp Bitmap Image 
       Bitmap    bmpImgDisp =
                new Bitmap(250, 250);  // your size of rect.
      using (Graphics g = Graphics.FromImage(bmpImgDisp))
            {
                   g.DrawImage(YourMainpictuerBox.Image, new RectangleF(0, 0, 250, 250), rect size you want to clip {x,y,height,widht}, GraphicsUnit.Pixel);
            }
1
  • I pass the handle of picturebox into DirectShow, and i don't see the paint event of picturebox is callback when displaying video.
    – MichaelP
    Dec 3, 2013 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.