Image Transparency in overlapping PictureBox objects.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?RG9jdG9y?=

    Image Transparency in overlapping PictureBox objects.

    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
    OnPaintBackgrou nd 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::Compone ntModel;
    using namespace System::Collect ions;
    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::Create Params;
    cp->ExStyle |= 0x20; // WS_EX_TRANSPARE NT
    return cp;
    }
    }
    protected: virtual void OnPaintBackgrou nd(PaintEventAr gs^ pevent)
    override {
    // Don't paint the background.
    }
    protected: virtual void OnPaint(PaintEv entArgs^ pe) override {
    // Paint background image
    if (this->BackgroundImag e != nullptr) {
    Bitmap^ bmp = gcnew Bitmap(this->BackgroundImag e);
    bmp->MakeTransparen t(Color::White) ;
    pe->Graphics->DrawImage(bm p, 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
    -----------------------------------------------

Working...