Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit fee8c5d

Browse files
committed
Video preview frame resize
1 parent b6f8b54 commit fee8c5d

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

OnnxStack.UI/Dialogs/CropImageDialog.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ private BitmapSource CropAndResizeImage()
292292
try
293293
{
294294
var croppedBitmap = new CroppedBitmap(_sourceImage, rect);
295-
var scaleX = _cropWidth / croppedBitmap.PixelWidth;
296-
var scaleY = _cropHeight / croppedBitmap.PixelHeight;
295+
double scaleX = (double)_requiredWidth / croppedBitmap.PixelWidth;
296+
double scaleY = (double)_requiredHeight / croppedBitmap.PixelHeight;
297297
var scaleTransform = new ScaleTransform(scaleX, scaleY);
298298
return new TransformedBitmap(croppedBitmap, scaleTransform);
299299
}

OnnxStack.UI/UserControls/VideoInputControl.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
</StackPanel>
6969

7070
<Grid>
71-
<Image Source="{StaticResource PlaceholderImage}" Width="{Binding SchedulerOptions.Width}" Height="{Binding SchedulerOptions.Height}" Visibility="{Binding IsPreviewVisible, Converter={StaticResource InverseBooleanToHiddenConverter}}"/>
71+
<Image Source="{StaticResource PlaceholderImage}" Width="{Binding SchedulerOptions.Width}" Height="{Binding SchedulerOptions.Height}" Visibility="{Binding HasVideoResult, Converter={StaticResource InverseBooleanToHiddenConverter}}"/>
7272
<MediaElement x:Name="VideoMediaElement" Source="{Binding VideoResult.FileName}" Width="{Binding SchedulerOptions.Width}" Height="{Binding SchedulerOptions.Height}" MinHeight="512" MinWidth="512" ClipToBounds="True" LoadedBehavior="Manual" Loaded="MediaElement_Loaded" MediaEnded="MediaElement_MediaEnded" Volume="0" MouseLeftButtonDown="MediaElement_MouseDown" Visibility="{Binding IsPreviewVisible, Converter={StaticResource InverseBooleanToHiddenConverter}}" />
7373
<Image Source="{Binding PreviewImage}" Width="{Binding SchedulerOptions.Width}" Height="{Binding SchedulerOptions.Height}" Stretch="Uniform" HorizontalAlignment="Center" />
7474
</Grid>

OnnxStack.UI/Views/VideoToVideoView.xaml.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
using OnnxStack.StableDiffusion.Common;
66
using OnnxStack.StableDiffusion.Config;
77
using OnnxStack.StableDiffusion.Enums;
8+
using OnnxStack.StableDiffusion.Helpers;
89
using OnnxStack.StableDiffusion.Models;
910
using OnnxStack.UI.Commands;
1011
using OnnxStack.UI.Models;
12+
using SixLabors.ImageSharp.PixelFormats;
13+
using SixLabors.ImageSharp;
1114
using System;
1215
using System.Collections.Generic;
1316
using System.Collections.ObjectModel;
@@ -385,7 +388,7 @@ private Action<DiffusionProgress> ProgressCallback()
385388
if (progress.BatchTensor is not null)
386389
{
387390
PreviewResult = Utils.CreateBitmap(progress.BatchTensor.ToImageBytes());
388-
PreviewSource = Utils.CreateBitmap(_videoFrames.Frames[progress.BatchValue - 1]);
391+
PreviewSource = UpdatePreviewFrame(progress.BatchValue - 1);
389392
ProgressText = $"Video Frame {progress.BatchValue} of {_videoFrames.Frames.Count} complete";
390393
}
391394

@@ -402,6 +405,23 @@ private Action<DiffusionProgress> ProgressCallback()
402405
};
403406
}
404407

408+
public BitmapImage UpdatePreviewFrame(int index)
409+
{
410+
var frame = _videoFrames.Frames[index];
411+
using (var memoryStream = new MemoryStream())
412+
using (var frameImage = SixLabors.ImageSharp.Image.Load<Rgba32>(frame))
413+
{
414+
ImageHelpers.Resize(frameImage, new[] { 1, 3, _schedulerOptions.Height, _schedulerOptions.Width });
415+
frameImage.SaveAsPng(memoryStream);
416+
var image = new BitmapImage();
417+
image.BeginInit();
418+
image.CacheOption = BitmapCacheOption.OnLoad;
419+
image.StreamSource = memoryStream;
420+
image.EndInit();
421+
return image;
422+
}
423+
}
424+
405425
#region INotifyPropertyChanged
406426
public event PropertyChangedEventHandler PropertyChanged;
407427
public void NotifyPropertyChanged([CallerMemberName] string property = "")

0 commit comments

Comments
 (0)