/// <summary> /// 预览桌面 /// </summary> private void PreviewDesktop() { if (PreviewPlayer.IsRunning) { PreviewPlayer.SignalToStop(); PreviewPlayer.WaitForStop(); } Rectangle screenArea = Rectangle.Empty; screenArea = Rectangle.Union(screenArea, System.Windows.Forms.Screen.PrimaryScreen.Bounds); var streamVideo = new ScreenCaptureStream(screenArea); //当前屏幕的宽高比 var index1 = Math.Round((screenArea.Width * 1.0 / screenArea.Height * 1.0), 2); var width = Convert.ToInt32(Math.Round(CameraStackpanel.Width)); var height = Convert.ToInt32(Math.Round(CameraStackpanel.Height)); //预览播放器的宽高比 var index2 = Math.Round((width * 1.0 / height * 1.0), 2); if (index1 > index2) { PreviewPlayer.Width = width; PreviewPlayer.Height = CommonHelper.GetHeight(screenArea.Width, screenArea.Height, width); } else { PreviewPlayer.Width = CommonHelper.GetWidth(screenArea.Width, screenArea.Height, height); PreviewPlayer.Height = height; } PreviewPlayer.VideoSource = streamVideo; PreviewPlayer.Start(); }
/// <summary> /// 预览摄像头 /// </summary> private void PreviewCamera() { if (PreviewPlayer.IsRunning) { PreviewPlayer.SignalToStop(); PreviewPlayer.WaitForStop(); } var camera = VideoHelper.GetCameraInfo(cameraName); if (camera != null) { cameraResolutionIndex = VideoHelper.GetCameraResolutionIndex(); camera.VideoResolution = camera.VideoCapabilities[cameraResolutionIndex]; //当前摄像头分辨率的宽高比 var index1 = Math.Round((camera.VideoResolution.FrameSize.Width * 1.0 / camera.VideoResolution.FrameSize.Height * 1.0), 2); var width = Convert.ToInt32(Math.Round(CameraStackpanel.Width)); var height = Convert.ToInt32(Math.Round(CameraStackpanel.Height)); //预览播放器的宽高比 var index2 = Math.Round((width * 1.0 / height * 1.0), 2); if (index1 > index2) { PreviewPlayer.Width = width; PreviewPlayer.Height = CommonHelper.GetHeight(camera.VideoResolution.FrameSize.Width, camera.VideoResolution.FrameSize.Height, width); } else { PreviewPlayer.Width = CommonHelper.GetWidth(camera.VideoResolution.FrameSize.Width, camera.VideoResolution.FrameSize.Height, height); PreviewPlayer.Height = height; } PreviewPlayer.VideoSource = camera; PreviewPlayer.Start(); } else { MessageBox.Show("错误的摄像头设置", "提示"); } }