Layer.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using PixiEditorDotNetCore3.Models.Tools;
  2. using System;
  3. using System.ComponentModel;
  4. using System.Runtime.InteropServices;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Media;
  8. using System.Windows.Media.Imaging;
  9. namespace PixiEditorDotNetCore3.Models.Layers
  10. {
  11. public class Layer : BasicLayer
  12. {
  13. private WriteableBitmap _layerBitmap;
  14. public string Name { get; set; }
  15. public bool IsVisible { get; set; } = true;
  16. public WriteableBitmap LayerBitmap
  17. {
  18. get => _layerBitmap;
  19. set
  20. {
  21. _layerBitmap = value;
  22. RaisePropertyChanged("LayerBitmap");
  23. }
  24. }
  25. public Layer(string name,int width, int height)
  26. {
  27. Name = name;
  28. Layer layer = LayerGenerator.Generate(width, height);
  29. LayerBitmap = layer.LayerBitmap;
  30. Width = width;
  31. Height = height;
  32. }
  33. public Layer(WriteableBitmap layerBitmap)
  34. {
  35. LayerBitmap = layerBitmap;
  36. Width = (int)layerBitmap.Width;
  37. Height = (int)layerBitmap.Height;
  38. }
  39. public void ApplyPixels(BitmapPixelChanges pixels)
  40. {
  41. LayerBitmap.Lock();
  42. foreach (var coords in pixels.ChangedPixels)
  43. {
  44. LayerBitmap.SetPixel(Math.Clamp(coords.Key.X, 0, Width - 1), Math.Clamp(coords.Key.Y, 0, Height - 1),
  45. coords.Value);
  46. }
  47. LayerBitmap.Unlock();
  48. }
  49. public byte[] ConvertBitmapToBytes()
  50. {
  51. LayerBitmap.Lock();
  52. byte[] byteArray = LayerBitmap.ToByteArray();
  53. LayerBitmap.Unlock();
  54. return byteArray;
  55. }
  56. public byte[] ConvertBitmapToBytes(WriteableBitmap bitmap)
  57. {
  58. bitmap.Lock();
  59. byte[] byteArray = bitmap.ToByteArray();
  60. bitmap.Unlock();
  61. return byteArray;
  62. }
  63. public void Resize(int newWidth, int newHeight)
  64. {
  65. LayerBitmap.Resize(newWidth, newHeight, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
  66. Height = newHeight;
  67. Width = newWidth;
  68. }
  69. }
  70. }