I am using wpf and want to capture the desktop and then save mp4 to file.
You can use native Win32 interfaces, like
IMFCaptureEngine with P/Invoke
(tested on Windows 10 22H2 from MS SDK sample https://github.com/microsoft/Windows-classic-samples/tree/main/Samples/CaptureEngineVideoCapture )