2020using Windows . Graphics . Imaging ;
2121using Windows . UI . Xaml . Media . Imaging ;
2222using Windows . UI . Xaml . Media ;
23+ using Windows . Media . MediaProperties ;
24+ using Windows . Storage . Pickers ;
2325
2426namespace StyleTransfer
2527{
@@ -35,6 +37,7 @@ public AppViewModel()
3537
3638 _inputSoftwareBitmapSource = new SoftwareBitmapSource ( ) ;
3739 _outputSoftwareBitmapSource = new SoftwareBitmapSource ( ) ;
40+ _saveEnabled = true ;
3841 NotifyUser ( true ) ;
3942
4043 }
@@ -88,6 +91,12 @@ public SoftwareBitmapSource OutputSoftwareBitmapSource
8891 get { return _outputSoftwareBitmapSource ; }
8992 set { _outputSoftwareBitmapSource = value ; OnPropertyChanged ( ) ; }
9093 }
94+ private bool _saveEnabled ;
95+ public bool SaveEnabled
96+ {
97+ get { return _saveEnabled ; }
98+ set { _saveEnabled = value ; OnPropertyChanged ( ) ; }
99+ }
91100
92101 public SolidColorBrush StatusBarColor
93102 {
@@ -104,7 +113,23 @@ public string StatusMessage
104113 public async void SaveOutput ( )
105114 {
106115 Debug . WriteLine ( "UIButtonSaveImage_Click" ) ;
107- await ImageHelper . SaveVideoFrameToFilePickedAsync ( _appModel . OutputFrame ) ;
116+ if ( _appModel . InputMedia == "LiveStream" )
117+ {
118+
119+ var previewProperties = _mediaCapture . VideoDeviceController . GetMediaStreamProperties ( MediaStreamType . VideoPreview ) as VideoEncodingProperties ;
120+
121+ // Create the video frame to request a SoftwareBitmap preview frame
122+ var videoFrame = new VideoFrame ( BitmapPixelFormat . Bgra8 , ( int ) previewProperties . Width , ( int ) previewProperties . Height ) ;
123+
124+ // Capture the preview frame
125+ using ( var currentFrame = await _mediaCapture . GetPreviewFrameAsync ( videoFrame ) )
126+ {
127+ SoftwareBitmap previewFrame = currentFrame . SoftwareBitmap ;
128+ await ImageHelper . SaveVideoFrameToFilePickedAsync ( videoFrame ) ;
129+ }
130+ }
131+ else await ImageHelper . SaveVideoFrameToFilePickedAsync ( _appModel . OutputFrame ) ;
132+
108133 return ;
109134 }
110135
@@ -116,12 +141,12 @@ public async Task SetMediaSource(string obj)
116141 CleanupCameraAsync ( ) ;
117142 CleanupInputImage ( ) ;
118143 NotifyUser ( true ) ;
144+ SaveEnabled = true ;
119145
120146 // Changes media source while keeping all other controls
121147 switch ( _appModel . InputMedia )
122148 {
123149 case "LiveStream" :
124-
125150 await StartLiveStream ( ) ;
126151 // TODO: Also spin up a Capture for preview on left side
127152 break ;
@@ -281,6 +306,8 @@ public async Task StartLiveStream()
281306 public async Task ChangeLiveStream ( )
282307 {
283308 Debug . WriteLine ( "ChangeLiveStream" ) ;
309+ SaveEnabled = false ;
310+
284311
285312 // If webcam hasn't been initialized, bail.
286313 if ( _mediaFrameSourceGroupList == null ) { return ; }
0 commit comments