Image Transparency in overlapping PictureBox objects.

D

Doctor

I am making an application that streams video from a web camera onto the
client area of a PictureBox on a Windows CLR Form in MS Visual Studio 2005
(using C++).
The streaming video works fine, and now I am trying to create an image
overlay.
I am assuming that the best way for me to do this is to create a second
PictureBox that lays over the streaming video. I would then need to draw
directly into the bitmap shown in the overlaying PictureBox and have most of
the pixels in that image be transparent (i.e. showing the streaming video
from the underlying picturebox). I have attempted to create a class called
myPictBox that inherits from PictureBox and overrides both the OnPaint and
OnPaintBackground methods, but I really don't know what I am doing. I think
what I want to do is essentially equivalent to the problem of creating a
nonrectangular custom control, though I will not need to respond to mouse
click events as a form control would.

Can anyone offer me any suggestions about this problem?
Am I on the right track, at least?

A stripped down version of how I think this should be solved is shown below
(as is suggested by an article in the MSDN forums). This code results in a
black rectangle overlaying my video stream.

------ File: MyPictBox.h ----------------------
pragma once
#pragma managed

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

namespace v1 {
public ref class MyPictBox: public PictureBox {
protected: virtual property CreateParams^ CreateParams {
CreateParams^ get () override {
CreateParams^ cp = __super::CreateParams;
cp->ExStyle |= 0x20; // WS_EX_TRANSPARENT
return cp;
}
}
protected: virtual void OnPaintBackground(PaintEventArgs^ pevent)
override {
// Don't paint the background.
}
protected: virtual void OnPaint(PaintEventArgs^ pe) override {
// Paint background image
if (this->BackgroundImage != nullptr) {
Bitmap^ bmp = gcnew Bitmap(this->BackgroundImage);
bmp->MakeTransparent(Color::White);
pe->Graphics->DrawImage(bmp, 0, 0);
}
// Draw opaque portion of control
Pen^ myPen = gcnew Pen(Color::Red,3.0f);
Point point1 = Point(0,50);
Point point2 = Point(50,50);
pe->Graphics->DrawLine(myPen, point1, point2);
delete myPen;
}
}; // class MyPictBox
} // namespace v1
-----------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top