ImageConverter.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Data.Converters;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Globalization;
  8. using System.IO;
  9. using Avalonia.Media.Imaging;
  10. using SixLabors.ImageSharp.Memory;
  11. using SixLabors.ImageSharp.Formats;
  12. using System.Collections;
  13. using System.IO.Compression;
  14. using SkiaSharp;
  15. namespace AvaloniaApplicationTestNew.Resources
  16. {
  17. internal class ImageConverter : IValueConverter
  18. {
  19. public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
  20. {
  21. if (value is byte[]) return new Bitmap(new MemoryStream((byte[])value));
  22. return null;
  23. }
  24. public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  25. {
  26. if (value is Bitmap)
  27. {
  28. byte[] byteArray;
  29. Bitmap bitmap = (Bitmap)value;
  30. using (MemoryStream stream = new MemoryStream())
  31. {
  32. bitmap.Save(stream);
  33. byteArray = stream.ToArray();
  34. return byteArray;
  35. }
  36. }
  37. return null;
  38. }
  39. }
  40. }