Skip to content

Commit

Permalink
Update image effects
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Jan 6, 2025
1 parent bfbe0f6 commit d61ad80
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 7 deletions.
28 changes: 22 additions & 6 deletions src/PicView.Avalonia/Views/EffectsView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<StackPanel Width="500">
<Expander Margin="0,15,0,10">
<Expander IsExpanded="True" Margin="0,15,0,10">
<Expander.Header>
<TextBlock
Classes="txt"
Expand Down Expand Up @@ -69,30 +69,46 @@
x:Name="ContrastSlider" />


<!-- Gamma -->
<Panel Margin="0,10">
</StackPanel>

</Expander>

<Expander IsExpanded="True" Margin="0,15,0,10">
<Expander.Header>
<TextBlock
Classes="txt"
FontFamily="/Assets/Fonts/Roboto-Bold.ttf#Roboto"
FontSize="14"
Padding="10,5"
Text="{CompiledBinding Effects}" />
</Expander.Header>
<StackPanel Margin="15,20">

<!-- Emboss -->
<Panel IsEnabled="False">
<TextBlock
Classes="txt"
HorizontalAlignment="Left"
Text="{CompiledBinding Gamma}"
Text="{CompiledBinding Emboss}"
VerticalAlignment="Center" />
<TextBlock
Classes="txt"
HorizontalAlignment="Right"
Text="{Binding Path=Value, ElementName=GammaSlider}"
Text="{Binding Path=Value, ElementName=EmbossSlider}"
VerticalAlignment="Center" />
</Panel>

<customControls:CustomSlider
Height="30"
IsEnabled="False"
IsSnapToTickEnabled="True"
LargeChange="5"
Margin="0,1,0,1"
Maximum="100"
Minimum="0"
SmallChange="1"
TickFrequency="1"
x:Name="GammaSlider" />
x:Name="EmbossSlider" />


</StackPanel>
Expand Down
99 changes: 98 additions & 1 deletion src/PicView.Avalonia/Views/EffectsView.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,109 @@
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Media.Imaging;
using ImageMagick;
using PicView.Avalonia.Navigation;
using PicView.Avalonia.ViewModels;

namespace PicView.Avalonia.Views;

public partial class EffectsView : UserControl
{
private Percentage _brightness = new(0);
private Percentage _contrast = new(0);

public EffectsView()
{
InitializeComponent();
Loaded += (_, _) => { };
Loaded += (_, _) =>
{
BrightnessSlider.ValueChanged += async (_, e) => await BrightnessChanged(e).ConfigureAwait(false);
ContrastSlider.ValueChanged += async (_, e) => await ContrastChanged(e).ConfigureAwait(false);
EmbossSlider.ValueChanged += async (_, e) => await EmbossChanged(e).ConfigureAwait(false);
if (DataContext is not MainViewModel vm)
{
return;
}

if (!NavigationHelper.CanNavigate(vm))
{
return;
}

// using var magick = new ImageMagick.MagickImage(vm.FileInfo);
// BrightnessSlider.Value = magick.get

};
}

private async Task BrightnessChanged(RangeBaseValueChangedEventArgs e)
{
_brightness = new Percentage(e.NewValue);
if (DataContext is not MainViewModel vm)
{
return;
}

if (!NavigationHelper.CanNavigate(vm))
{
return;
}
await SetBrightnessContrast(vm);
}

private async Task ContrastChanged(RangeBaseValueChangedEventArgs e)
{
_contrast = new Percentage(e.NewValue);
if (DataContext is not MainViewModel vm)
{
return;
}

if (!NavigationHelper.CanNavigate(vm))
{
return;
}
await SetBrightnessContrast(vm);
}

private async Task SetBrightnessContrast(MainViewModel vm)
{
using var magick = new MagickImage();
await magick.ReadAsync(vm.FileInfo.FullName).ConfigureAwait(false);
magick.BrightnessContrast(_brightness, _contrast);
magick.Format = MagickFormat.WebP;
magick.BackgroundColor = MagickColors.Transparent;
magick.Settings.BackgroundColor = MagickColors.Transparent;
magick.Settings.FillColor = MagickColors.Transparent;
await using var memoryStream = new MemoryStream();
await magick.WriteAsync(memoryStream);
memoryStream.Position = 0;
var bitmap = new Bitmap(memoryStream);
vm.ImageSource = bitmap;
}

private async Task EmbossChanged(RangeBaseValueChangedEventArgs e)
{
if (DataContext is not MainViewModel vm)
{
return;
}

if (!NavigationHelper.CanNavigate(vm))
{
return;
}
using var magick = new MagickImage();
await magick.ReadAsync(vm.FileInfo.FullName).ConfigureAwait(false);
magick.Format = MagickFormat.WebP;
magick.BackgroundColor = MagickColors.Transparent;
magick.Settings.BackgroundColor = MagickColors.Transparent;
magick.Settings.FillColor = MagickColors.Transparent;
magick.Emboss(0, e.NewValue);
await using var memoryStream = new MemoryStream();
await magick.WriteAsync(memoryStream);
memoryStream.Position = 0;
var bitmap = new Bitmap(memoryStream);
vm.ImageSource = bitmap;
}
}

0 comments on commit d61ad80

Please sign in to comment.