更新 Utils

This commit is contained in:
Hakoyu
2023-09-09 21:30:32 +08:00
parent c24055f446
commit c66011c763
4 changed files with 25 additions and 27 deletions

View File

@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace VPet.ModMaker.Models;
@ -12,4 +15,17 @@ public static class Extensions
{
return source.IndexOf(value, comparisonType) >= 0;
}
public static string GetSourceFile(this BitmapImage image)
{
return ((FileStream)image.StreamSource).Name;
}
public static void CloseString(this ImageSource source)
{
if (source is BitmapImage image)
{
image.StreamSource?.Close();
}
}
}

View File

@ -169,7 +169,7 @@ public class ModInfoModel : I18nModel<I18nModInfoModel>
}
);
}
var imagePath = Utils.GetImageSourceFile(Image.Value);
var imagePath = Image.Value.GetSourceFile();
var targetImagePath = Path.Combine(path, Path.GetFileName(imagePath));
if (imagePath != targetImagePath)
File.Copy(imagePath, targetImagePath, true);
@ -311,7 +311,7 @@ public class ModInfoModel : I18nModel<I18nModInfoModel>
Directory.CreateDirectory(foodPath);
foreach (var food in Foods)
{
var foodImagePath = Utils.GetImageSourceFile(food.Image.Value);
var foodImagePath = food.Image.Value.GetSourceFile();
var targetImagePath = Path.Combine(
foodPath,
$"{food.Id.Value}{Path.GetExtension(foodImagePath)}"

View File

@ -7,15 +7,10 @@ using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;
namespace VPet.ModMaker;
namespace VPet.ModMaker.Models;
internal static class Utils
public static class Utils
{
public static string GetImageSourceFile(BitmapImage image)
{
return ((FileStream)image.StreamSource).Name;
}
public static BitmapImage LoadImageToStream(string imagePath)
{
BitmapImage bitmapImage = new();
@ -25,9 +20,9 @@ internal static class Utils
return bitmapImage;
}
public static BitmapImage LoadImageToStream(BitmapImage image)
public static BitmapImage LoadImageToStream(this BitmapImage image)
{
return LoadImageToStream(GetImageSourceFile(image));
return LoadImageToStream(image.GetSourceFile());
}
public static BitmapImage LoadImageToMemoryStream(string imagePath)
@ -42,21 +37,8 @@ internal static class Utils
return bitmapImage;
}
public static BitmapImage LoadImageToMemoryStream(BitmapImage image)
public static BitmapImage LoadImageToMemoryStream(this BitmapImage image)
{
return LoadImageToMemoryStream(GetImageSourceFile(image));
}
public static void ShowDialogX(this Window window, Window owner)
{
owner.IsEnabled = false;
window.Owner = owner;
window.Closed += (s, e) =>
{
owner.IsEnabled = true;
};
window.ShowDialog();
return LoadImageToMemoryStream(image.GetSourceFile());
}
}
public delegate void ShowDialogXHandler(Window window);

View File

@ -160,7 +160,7 @@
</Compile>
<Compile Include="SimpleObservable\ObservableCommand.cs" />
<Compile Include="SimpleObservable\ObservableValue.cs" />
<Compile Include="Utils.cs" />
<Compile Include="Models\Utils.cs" />
<Compile Include="Views\ModEdit\FoodEdit\FoodEditWindow.xaml.cs">
<DependentUpon>FoodEditWindow.xaml</DependentUpon>
</Compile>