ProfileImageSelectionDialog.axaml.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Interactivity;
  4. using Avalonia.Markup.Xaml;
  5. using Ryujinx.Ava.Common.Locale;
  6. using Ryujinx.Ava.Ui.Windows;
  7. using Ryujinx.HLE.FileSystem;
  8. using SixLabors.ImageSharp;
  9. using SixLabors.ImageSharp.Processing;
  10. using System.IO;
  11. using Image = SixLabors.ImageSharp.Image;
  12. namespace Ryujinx.Ava.Ui.Controls
  13. {
  14. public class ProfileImageSelectionDialog : StyleableWindow
  15. {
  16. private readonly ContentManager _contentManager;
  17. public bool FirmwareFound => _contentManager.GetCurrentFirmwareVersion() != null;
  18. public byte[] BufferImageProfile { get; set; }
  19. public ProfileImageSelectionDialog(ContentManager contentManager)
  20. {
  21. _contentManager = contentManager;
  22. DataContext = this;
  23. InitializeComponent();
  24. #if DEBUG
  25. this.AttachDevTools();
  26. #endif
  27. }
  28. public ProfileImageSelectionDialog()
  29. {
  30. DataContext = this;
  31. InitializeComponent();
  32. #if DEBUG
  33. this.AttachDevTools();
  34. #endif
  35. }
  36. private void InitializeComponent()
  37. {
  38. AvaloniaXamlLoader.Load(this);
  39. }
  40. private async void Import_OnClick(object sender, RoutedEventArgs e)
  41. {
  42. OpenFileDialog dialog = new();
  43. dialog.Filters.Add(new FileDialogFilter
  44. {
  45. Name = LocaleManager.Instance["AllSupportedFormats"],
  46. Extensions = { "jpg", "jpeg", "png", "bmp" }
  47. });
  48. dialog.Filters.Add(new FileDialogFilter { Name = "JPEG", Extensions = { "jpg", "jpeg" } });
  49. dialog.Filters.Add(new FileDialogFilter { Name = "PNG", Extensions = { "png" } });
  50. dialog.Filters.Add(new FileDialogFilter { Name = "BMP", Extensions = { "bmp" } });
  51. dialog.AllowMultiple = false;
  52. string[] image = await dialog.ShowAsync(this);
  53. if (image != null)
  54. {
  55. if (image.Length > 0)
  56. {
  57. string imageFile = image[0];
  58. ProcessProfileImage(File.ReadAllBytes(imageFile));
  59. }
  60. Close();
  61. }
  62. }
  63. private async void SelectFirmwareImage_OnClick(object sender, RoutedEventArgs e)
  64. {
  65. if (FirmwareFound)
  66. {
  67. AvatarWindow window = new(_contentManager);
  68. await window.ShowDialog(this);
  69. BufferImageProfile = window.SelectedImage;
  70. Close();
  71. }
  72. }
  73. private void ProcessProfileImage(byte[] buffer)
  74. {
  75. using (Image image = Image.Load(buffer))
  76. {
  77. image.Mutate(x => x.Resize(256, 256));
  78. using (MemoryStream streamJpg = new())
  79. {
  80. image.SaveAsJpeg(streamJpg);
  81. BufferImageProfile = streamJpg.ToArray();
  82. }
  83. }
  84. }
  85. }
  86. }