CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2009
    Posts
    21

    [RESOLVED] Picturebox transparency and overlay

    I am using Visual C# 2008 Express edition. I've made a webcam application and the webcam stream is viewed in a picturebox. The problem is I want to make an overlay for example have a cross image made in photoshop and saved as a PNG, having alpha channels (transparency) and want to put it over the webcam picturebox. I tried using a paint event, for painting the cross image on the form, but when i activate the webcam, the painted cross is getting behind it. How can i solve this?

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Picturebox transparency and overlay

    You can simply paint everything yourself instead:

    Code:
    protected override void OnPaint( PaintEventArgs e )
    {
        // draw the webcam image first
        e.Graphics.DrawImage( ... );
        
        // now draw on top of it
        e.Graphics.DrawImage( ... );
    }

  3. #3
    Join Date
    Jun 2009
    Posts
    21

    Unhappy Re: Picturebox transparency and overlay

    Already tried that, and the images are flashing between them because there are 2 and need to process them by turn.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Picturebox transparency and overlay

    I don't know what you mean. That code works fine, so if there is more going on we will obviously need to see the code before we can help. You are drawing the images in serial, so I don't see how that would be a problem. Do you have drawing code somewhere besides the OnPaint method?

  5. #5
    Join Date
    Jun 2009
    Posts
    21

    Unhappy Re: Picturebox transparency and overlay

    Okay. here it goes ...
    The start button that starts the selected webcam from comboBox1:

    Code:
        private void start_Click(object sender, EventArgs e)
              {
                 
                  if (start.Text == "&Start")
                  {
                      if (DeviceExist)
                      {
                          videoSource = new  VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
                          videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
                          
                          CloseVideoSource();
                          videoSource.DesiredFrameSize = new Size(320, 240);
                          videoSource.DesiredFrameRate = 30;  
                          videoSource.Start();
                          label2.Text = "Device running...";
                          start.Text = "&Stop";
                          timer1.Enabled = true;
                      }
                      else
                      {
                          label2.Text = "Error: No Device selected.";
                      }
                  }
                  else
                  {
                      if (videoSource.IsRunning)
                      {
                          timer1.Enabled = false;
                          CloseVideoSource();
                          label2.Text = "Device stopped.";
                          start.Text = "&Start";
                      }
                  }  
              }
    next I have the received frame trigerred event:

    Code:
    private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)  
             {
                 Bitmap img = (Bitmap)eventArgs.Frame.Clone();  
                 pictureBox1.Image = img;
             }
    and of course the overlay that I'm trying to accomplish:
    Code:
    protected override void OnPaint(PaintEventArgs e)
    {
              Image bmp = Image.FromFile("cross.png");
              Graphics s = pictureBox1.CreateGraphics();
    
              //removes the red color for transparency, not quite necessary when using picturebox
              ImageAttributes attr = new ImageAttributes();
              attr.SetColorKey(Color.Red, Color.Red);
    
              Rectangle dstRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
              
              s.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height,
                  GraphicsUnit.Pixel, attr);
    }
    What i wanted to explain in my previous post is :
    1. a captured frame from the webcam is transmmited to the video_NewFrame subroutine
    2. it displays it pictureBox1.Image = img;
    3. the ovelayed image is flashing continously, maybe because the images are send in sequence, maybe they need to be merged together or something and after that they should be displayed in the picturebox.
    4. hope you understand.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Picturebox transparency and overlay

    I assume you are invalidating as well on every frame (can't tell if OnPaint is defined in your Form or picturebox class, but either way), so I would suggest first trying to double buffer the image. I don't know what your frame rate is, but the most simple way to do this is by setting the DoubleBuffered property to true.

  7. #7
    Join Date
    Jun 2009
    Posts
    21

    Resolved Re: Picturebox transparency and overlay

    Thank you for your help! I solved the problem

    I have created an eventhandler for the picturebox
    Code:
    pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
    and then paint it from within and it worked

    Code:
     private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
            {
              ...
            }

  8. #8
    Join Date
    Jun 2014
    Posts
    1

    Re: [RESOLVED] Picturebox transparency and overlay

    hi all...
    i'm new on the blog, and is very usefull!!!
    i'm sorry for the wrong moment, but i need the same code of revo09.
    i'm triyng to overlay a transparent image over the webcam stream...
    i tried to use DirectShowLib, but i don't understand the usage of library.

    Can someone help me?
    Please revo09, can you post or send me the code for the issue?

    thanks in advance.
    Best regard
    Massimo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured