techyian / MMALSharp

C# wrapper to Broadcom's MMAL with an API to the Raspberry Pi camera.
MIT License
195 stars 33 forks source link

Howto change PreviewPort after cam.Camera.PreviewPort.ConnectTo(video) #205

Open clahil-linum opened 1 year ago

clahil-linum commented 1 year ago

I want to use something like the code below (in a WinForm) the get the screen location of a picture box:

Point previewPos = PointToScreen(QrCodePreview.Location);
            Rectangle screenPosition = new Rectangle(previewPos.X, previewPos.Y, QrCodePreview.Width, QrCodePreview.Height);

This works fine a give me the current screen location of a previewbox inside the WinForm dialog. After this I want to use code like the one below to attach the camera preview port to that screen location:

PreviewConfiguration previewConfig = new PreviewOverlayConfiguration
            {
                FullScreen = false,
                PreviewWindow = screenPosition,
                Layer = 1,
                Opacity = 255
            };
var video = new MMALVideoRenderer(previewConfig);
cam.Camera.PreviewPort.ConnectTo(video);

This works fine to. I get a very fast preview of the camera that is overlayed onto the picturebox item inside the dialog. But obviously as soon as I move and/or resize the dialog the screen coordinates of the preview port must be updated. I tried code like to one above inside a "changed size/position" handler but new settings had no effect. It seems that I miss something to attach a new previewport configuration.