Skip to content

Commit

Permalink
支持多种输出大小
Browse files Browse the repository at this point in the history
Fix #2
  • Loading branch information
liesauer committed Apr 8, 2022
1 parent 9876494 commit 49ae7a5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 26 deletions.
3 changes: 3 additions & 0 deletions pic2meme/ChangeLogWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public ChangeLogWindow()
this.MinWidth = this.MaxWidth = this.Width;
this.MinHeight = this.MaxHeight = this.Height;
ChangeLog.Text = @"
v1.3 - 2022/04/08
1. 支持多种输出大小
v1.2.2 - 2022/04/08
1. 优化UI
2. 优化预览图生成速度
Expand Down
16 changes: 11 additions & 5 deletions pic2meme/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:pic2meme" xmlns:hc="https://handyorg.github.io/handycontrol"
mc:Ignorable="d"
Title="微信表情包工具 v1.2.2" Height="535.632" Width="538.297" AllowDrop="True" Drop="Window_Drop" KeyDown="Window_KeyDown" ResizeMode="CanMinimize">
Title="微信表情包工具 v1.3" Height="562.632" Width="538.297" AllowDrop="True" Drop="Window_Drop" KeyDown="Window_KeyDown" ResizeMode="CanMinimize">
<Grid>
<Label x:Name="Notice" Content="拖动或粘贴图片到此处进行表情包转换" FontSize="22" Margin="30,20,30,0" VerticalAlignment="Top" Height="54" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Stretch" />
<hc:GifImage x:Name="Preview" Stretch="Uniform" Height="200" Width="200" Margin="128,151,129.5,127.5" />
<hc:GifImage x:Name="Preview" Stretch="Uniform" Height="200" Width="200" Margin="166,191,166.5,116" />


<Button x:Name="Usage" Content="使用说明" HorizontalAlignment="Left" Margin="30,0,0,27" VerticalAlignment="Bottom" Width="116" Height="52" FontSize="20" Click="Usage_Click" FocusVisualStyle="{x:Null}" />
<Button x:Name="ChangeLog" Content="更新历史" HorizontalAlignment="Left" Margin="207,0,0,27" VerticalAlignment="Bottom" Width="116" Height="52" FontSize="20" Click="ChangeLog_Click" FocusVisualStyle="{x:Null}" />
<Button x:Name="BuyMyACoffee" Content="支持一下" HorizontalAlignment="Stretch" Margin="386,0,30.5,27" VerticalAlignment="Bottom" Height="52" FontSize="20" Click="BuyMyACoffee_Click" FocusVisualStyle="{x:Null}" />
<RadioButton x:Name="CovertMode1" Content="模式1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="80,94,0,0" Height="38" Width="85" FontSize="20" GroupName="CovertMode" IsChecked="True" Checked="CovertMode1_Checked" FocusVisualStyle="{x:Null}" />
<RadioButton x:Name="CovertMode2" Content="模式2" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,94,70.5,0" Height="38" FontSize="20" GroupName="CovertMode" Checked="CovertMode2_Checked" FocusVisualStyle="{x:Null}" Width="85" />
<Label x:Name="PreviewLabel" Content="" Margin="166,240,166.5,217" BorderThickness="0" FontSize="18" Width="200" Height="50" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />
<RadioButton x:Name="CovertMode1" Content="模式1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="135,86,0,0" Height="38" Width="85" FontSize="20" GroupName="CovertMode" IsChecked="True" Checked="ReCovert" FocusVisualStyle="{x:Null}" />
<RadioButton x:Name="CovertMode2" Content="模式2" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,86,19.5,0" Height="38" FontSize="20" GroupName="CovertMode" Checked="ReCovert" FocusVisualStyle="{x:Null}" Width="85" />
<Label x:Name="PreviewLabel" Content="" Margin="166,266,166.5,191" BorderThickness="0" FontSize="18" Width="200" Height="50" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />
<RadioButton x:Name="SizeMode_Raw" Content="原始" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="135,132,0,0" Height="38" Width="70" FontSize="20" GroupName="SizeMode" IsChecked="True" Checked="ReCovert" FocusVisualStyle="{x:Null}" />
<RadioButton x:Name="SizeMode_40" Content="40" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="243,132,0,0" Height="38" Width="70" FontSize="20" GroupName="SizeMode" IsChecked="False" Checked="ReCovert" FocusVisualStyle="{x:Null}" />
<RadioButton x:Name="SizeMode_80" Content="80" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="334,132,0,0" Height="38" Width="70" FontSize="20" GroupName="SizeMode" IsChecked="False" Checked="ReCovert" FocusVisualStyle="{x:Null}" />
<RadioButton x:Name="SizeMode_120" Content="120" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,132,34.5,0" Height="38" FontSize="20" GroupName="SizeMode" IsChecked="False" Checked="ReCovert" FocusVisualStyle="{x:Null}" Width="70" />
<Label Content="转换模式:" HorizontalAlignment="Left" Margin="20,87,0,0" VerticalAlignment="Top" HorizontalContentAlignment="Left" BorderThickness="0" Width="108" FontSize="20" Height="36"/>
<Label Content="输出大小:" HorizontalAlignment="Left" Margin="20,133,0,0" VerticalAlignment="Top" HorizontalContentAlignment="Left" BorderThickness="0" Width="108" FontSize="20" Height="36"/>
</Grid>
</Window>
51 changes: 38 additions & 13 deletions pic2meme/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ private int GetCovertMode()
return 0;
}

private int GetSizeMode()
{
if (SizeMode_Raw.IsChecked.GetValueOrDefault(false)) return 1;
if (SizeMode_40.IsChecked.GetValueOrDefault(false)) return 2;
if (SizeMode_80.IsChecked.GetValueOrDefault(false)) return 3;
if (SizeMode_120.IsChecked.GetValueOrDefault(false)) return 4;

return 0;
}

private async void pic2meme()
{
if (!_init) return;
Expand All @@ -67,13 +77,25 @@ private async void pic2meme()
Notice.Content = "转换中...";

var covertMode = GetCovertMode();
var sizeMode = GetSizeMode();

if (covertMode == 0)
{
Notice.Content = $"转换失败:不支持的转换模式{covertMode}";

return;
}
if (sizeMode == 0)
{
Notice.Content = $"转换失败:不支持的输出大小{sizeMode}";

return;
}

var forceSize = 0;
if (sizeMode == 2) forceSize = 40;
if (sizeMode == 3) forceSize = 80;
if (sizeMode == 4) forceSize = 120;

var sourceImage = "";

Expand Down Expand Up @@ -211,16 +233,29 @@ private async void pic2meme()
return;
}
}
if (sizeMode > 1)
{
sourceImage = tmpImage;
tmpImage = GenerateTempFile(".gif");
if (covertMode == 1)
{
covertTask = Utils.Any2GIF(sourceImage, tmpImage, forceSize);
}
else if (covertMode == 2)
{
covertTask = Utils.Any2GIF2(sourceImage, tmpImage, forceSize);
}
}
}
else
{
if (covertMode == 1)
{
covertTask = Utils.Any2GIF(sourceImage, tmpImage);
covertTask = Utils.Any2GIF(sourceImage, tmpImage, forceSize);
}
else if (covertMode == 2)
{
covertTask = Utils.Any2GIF2(sourceImage, tmpImage);
covertTask = Utils.Any2GIF2(sourceImage, tmpImage, forceSize);
}
}
}
Expand Down Expand Up @@ -351,17 +386,7 @@ private void BuyMyACoffee_Click(object sender, RoutedEventArgs e)
buymeacoffee.ShowDialog();
}

private void CovertMode1_Checked(object sender, RoutedEventArgs e)
{
pic2meme();
}

private void CovertMode2_Checked(object sender, RoutedEventArgs e)
{
pic2meme();
}

private void CovertMode3_Checked(object sender, RoutedEventArgs e)
private void ReCovert(object sender, RoutedEventArgs e)
{
pic2meme();
}
Expand Down
16 changes: 8 additions & 8 deletions pic2meme/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ public static (int width, int height) GetImageSize(string filePath)
return (width, height);
}

public static Task<bool> Any2GIF(string filePath, string savePath, int maxsize = 0)
public static Task<bool> Any2GIF(string filePath, string savePath, int forceSize = 0)
{
try
{
return ISImage.LoadAsync(filePath).ContinueWith(task => {
var image = task.Result;
if (maxsize > 0 && (image.Width > maxsize || image.Height > maxsize))
int max = Math.Max(image.Width, image.Height);
if (forceSize > 0 && max != forceSize)
{
int max = Math.Max(image.Width, image.Height);
float scale = max / maxsize;
float scale = max / forceSize;
image.Mutate(x => x.Resize((int)(image.Width / scale), (int)(image.Height / scale)));
}
image.SaveAsGif(savePath);
Expand All @@ -102,16 +102,16 @@ public static Task<bool> Any2GIF(string filePath, string savePath, int maxsize =
}
}

public static Task<bool> Any2GIF2(string filePath, string savePath, int maxsize = 0)
public static Task<bool> Any2GIF2(string filePath, string savePath, int forceSize = 0)
{
try
{
return ISImage.LoadAsync(filePath).ContinueWith(task => {
var image = task.Result;
if (maxsize > 0 && (image.Width > maxsize || image.Height > maxsize))
int max = Math.Max(image.Width, image.Height);
if (forceSize > 0 && max != forceSize)
{
int max = Math.Max(image.Width, image.Height);
float scale = max / maxsize;
float scale = max / forceSize;
image.Mutate(x => x.Resize((int)(image.Width / scale), (int)(image.Height / scale)));
}
var gifEncoder = new GifEncoder();
Expand Down
Binary file modified screenshot_00.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 49ae7a5

Please sign in to comment.