ProfileImageSelectionDialog.axaml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Avalonia.Controls;
  2. using Avalonia.Interactivity;
  3. using Avalonia.VisualTree;
  4. using FluentAvalonia.UI.Controls;
  5. using FluentAvalonia.UI.Navigation;
  6. using Ryujinx.Ava.Common.Locale;
  7. using Ryujinx.Ava.UI.Models;
  8. using Ryujinx.Ava.UI.Windows;
  9. using Ryujinx.HLE.FileSystem;
  10. using SixLabors.ImageSharp;
  11. using SixLabors.ImageSharp.Processing;
  12. using System.IO;
  13. using Image = SixLabors.ImageSharp.Image;
  14. namespace Ryujinx.Ava.UI.Controls
  15. {
  16. public partial class ProfileImageSelectionDialog : UserControl
  17. {
  18. private ContentManager _contentManager;
  19. private NavigationDialogHost _parent;
  20. private TempProfile _profile;
  21. public bool FirmwareFound => _contentManager.GetCurrentFirmwareVersion() != null;
  22. public ProfileImageSelectionDialog()
  23. {
  24. InitializeComponent();
  25. AddHandler(Frame.NavigatedToEvent, (s, e) =>
  26. {
  27. NavigatedTo(e);
  28. }, RoutingStrategies.Direct);
  29. }
  30. private void NavigatedTo(NavigationEventArgs arg)
  31. {
  32. if (Program.PreviewerDetached)
  33. {
  34. switch (arg.NavigationMode)
  35. {
  36. case NavigationMode.New:
  37. (_parent, _profile) = ((NavigationDialogHost, TempProfile))arg.Parameter;
  38. _contentManager = _parent.ContentManager;
  39. break;
  40. case NavigationMode.Back:
  41. _parent.GoBack();
  42. break;
  43. }
  44. DataContext = this;
  45. }
  46. }
  47. private async void Import_OnClick(object sender, RoutedEventArgs e)
  48. {
  49. OpenFileDialog dialog = new();
  50. dialog.Filters.Add(new FileDialogFilter
  51. {
  52. Name = LocaleManager.Instance[LocaleKeys.AllSupportedFormats],
  53. Extensions = { "jpg", "jpeg", "png", "bmp" }
  54. });
  55. dialog.Filters.Add(new FileDialogFilter { Name = "JPEG", Extensions = { "jpg", "jpeg" } });
  56. dialog.Filters.Add(new FileDialogFilter { Name = "PNG", Extensions = { "png" } });
  57. dialog.Filters.Add(new FileDialogFilter { Name = "BMP", Extensions = { "bmp" } });
  58. dialog.AllowMultiple = false;
  59. string[] image = await dialog.ShowAsync(((TopLevel)_parent.GetVisualRoot()) as Window);
  60. if (image != null)
  61. {
  62. if (image.Length > 0)
  63. {
  64. string imageFile = image[0];
  65. _profile.Image = ProcessProfileImage(File.ReadAllBytes(imageFile));
  66. }
  67. _parent.GoBack();
  68. }
  69. }
  70. private void SelectFirmwareImage_OnClick(object sender, RoutedEventArgs e)
  71. {
  72. if (FirmwareFound)
  73. {
  74. _parent.Navigate(typeof(AvatarWindow), (_parent, _profile));
  75. }
  76. }
  77. private static byte[] ProcessProfileImage(byte[] buffer)
  78. {
  79. using (Image image = Image.Load(buffer))
  80. {
  81. image.Mutate(x => x.Resize(256, 256));
  82. using (MemoryStream streamJpg = new())
  83. {
  84. image.SaveAsJpeg(streamJpg);
  85. return streamJpg.ToArray();
  86. }
  87. }
  88. }
  89. }
  90. }