C# WinForms 4.7.8录屏APP:如何在RecorderOptions中添加全屏忽略SourceRect的条件
解决WinForms录屏应用中RecorderOptions的SourceRect条件逻辑问题
针对你的需求,这里提供两种符合C# WinForms 4.7.8的实现方式,核心逻辑是全屏录制时跳过SourceRect赋值,非全屏时使用指定区域:
方式一:对象初始化器内用三元运算符(适用于SourceRect支持可空类型的场景)
直接在对象初始化时通过三元表达式完成条件赋值,代码更紧凑:
RecorderOptions options = new RecorderOptions { AudioOptions = new AudioOptions { IsAudioEnabled = true, IsOutputDeviceEnabled = SpeakersOn, IsInputDeviceEnabled = MicrophoneOn, AudioOutputDevice = selectedOutputDevice.DeviceName, AudioInputDevice = selectedInputDevice.DeviceName, InputVolume = 1, OutputVolume = 1, }, OutputOptions = new OutputOptions { RecorderMode = RecorderMode.Video, OutputFrameSize = new ScreenSize(1920, 1080), Stretch = StretchMode.Uniform, // 核心逻辑:非全屏时赋值指定区域,全屏时设为null(框架会自动忽略并使用全屏) SourceRect = RecordFullscreen ? null : new ScreenRect(this.Left + 15, this.Top + 35, this.Width - 30, this.Height - 50) }, };
方式二:分步初始化(适用于SourceRect不支持可空的场景)
如果你的录屏框架要求SourceRect必须为非空值,可先初始化基础配置,再根据条件赋值:
// 先初始化OutputOptions的基础参数 var outputOptions = new OutputOptions { RecorderMode = RecorderMode.Video, OutputFrameSize = new ScreenSize(1920, 1080), Stretch = StretchMode.Uniform }; // 仅在非全屏模式下设置SourceRect if (!RecordFullscreen) { outputOptions.SourceRect = new ScreenRect(this.Left + 15, this.Top + 35, this.Width - 30, this.Height - 50); } // 组装最终的RecorderOptions RecorderOptions options = new RecorderOptions { AudioOptions = new AudioOptions { IsAudioEnabled = true, IsOutputDeviceEnabled = SpeakersOn, IsInputDeviceEnabled = MicrophoneOn, AudioOutputDevice = selectedOutputDevice.DeviceName, AudioInputDevice = selectedInputDevice.DeviceName, InputVolume = 1, OutputVolume = 1, }, OutputOptions = outputOptions };
逻辑说明
- 当
RecordFullscreen为true时,不设置/清空SourceRect,录屏框架会自动触发全屏录制逻辑 - 当
RecordFullscreen为false时,使用你定义的窗口内区域(偏移15/35像素,去除边框后的区域)
内容的提问来源于stack exchange,提问作者user2180706
相关产品推荐
相关产品推荐

