Click to See Complete Forum and Search --> : [RESOLVED] Picturebox transparency and overlay
revo09
July 19th, 2009, 02:19 PM
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?
BigEd781
July 19th, 2009, 02:38 PM
You can simply paint everything yourself instead:
protected override void OnPaint( PaintEventArgs e )
{
// draw the webcam image first
e.Graphics.DrawImage( ... );
// now draw on top of it
e.Graphics.DrawImage( ... );
}
revo09
July 19th, 2009, 03:54 PM
Already tried that, and the images are flashing between them because there are 2 and need to process them by turn.
BigEd781
July 19th, 2009, 04:01 PM
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?
revo09
July 19th, 2009, 04:33 PM
Okay. here it goes ...
The start button that starts the selected webcam from comboBox1:
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:
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:
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.
BigEd781
July 19th, 2009, 04:51 PM
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.
revo09
July 20th, 2009, 04:06 AM
Thank you for your help! I solved the problem :)
I have created an eventhandler for the picturebox
pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
and then paint it from within and it worked
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
...
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.