Layer.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 WriteableBitmap LayerBitmap
  16. {
  17. get { return _layerBitmap; }
  18. set
  19. {
  20. _layerBitmap = value;
  21. RaisePropertyChanged("LayerBitmap");
  22. }
  23. }
  24. public Layer(string name,int width, int height)
  25. {
  26. Name = name;
  27. Layer layer = LayerGenerator.Generate(width, height);
  28. LayerBitmap = layer.LayerBitmap;
  29. Width = width;
  30. Height = height;
  31. }
  32. public Layer(WriteableBitmap layerBitmap)
  33. {
  34. LayerBitmap = layerBitmap;
  35. Width = (int)layerBitmap.Width;
  36. Height = (int)layerBitmap.Height;
  37. }
  38. public void ApplyPixels(BitmapPixelChanges pixels, Color color)
  39. {
  40. LayerBitmap.Lock();
  41. foreach (var coords in pixels.ChangedCoordinates)
  42. {
  43. LayerBitmap.SetPixel(Math.Clamp(coords.X, 0, Width - 1), Math.Clamp(coords.Y, 0, Height - 1),
  44. color);
  45. }
  46. LayerBitmap.Unlock();
  47. }
  48. public byte[] ConvertBitmapToBytes()
  49. {
  50. LayerBitmap.Lock();
  51. byte[] byteArray = LayerBitmap.ToByteArray();
  52. LayerBitmap.Unlock();
  53. return byteArray;
  54. }
  55. public byte[] ConvertBitmapToBytes(WriteableBitmap bitmap)
  56. {
  57. bitmap.Lock();
  58. byte[] byteArray = bitmap.ToByteArray();
  59. bitmap.Unlock();
  60. return byteArray;
  61. }
  62. }
  63. }