Sin descripción

ViewShot.cs 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using ReactNative.Bridge;
  2. using ReactNative.UIManager;
  3. using System;
  4. using System.IO;
  5. using System.Windows;
  6. using System.Windows.Media;
  7. using System.Windows.Media.Imaging;
  8. namespace RNViewShot
  9. {
  10. public class ViewShot : IUIBlock
  11. {
  12. private const string ErrorUnableToSnapshot = "E_UNABLE_TO_SNAPSHOT";
  13. private int tag;
  14. private string extension;
  15. private double quality;
  16. private int? width;
  17. private int? height;
  18. private string result;
  19. private IPromise promise;
  20. public ViewShot(
  21. int tag,
  22. string extension,
  23. double quality,
  24. int? width,
  25. int? height,
  26. string result,
  27. IPromise promise)
  28. {
  29. this.tag = tag;
  30. this.extension = extension;
  31. this.quality = quality;
  32. this.width = width;
  33. this.height = height;
  34. this.result = result;
  35. this.promise = promise;
  36. }
  37. public void Execute(NativeViewHierarchyManager nvhm)
  38. {
  39. var view = nvhm.ResolveView(tag) as FrameworkElement;
  40. if (view == null)
  41. {
  42. promise.Reject(ErrorUnableToSnapshot, "No view found with reactTag: " + tag);
  43. return;
  44. }
  45. int width = (int)view.ActualWidth;
  46. int height = (int)view.ActualHeight;
  47. RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
  48. renderTargetBitmap.Render(view);
  49. BitmapEncoder image;
  50. if (extension == "png")
  51. {
  52. image = new PngBitmapEncoder();
  53. }
  54. else if (extension == "jpg" || extension == "jpeg")
  55. {
  56. image = new JpegBitmapEncoder();
  57. }
  58. else
  59. {
  60. promise.Reject(ErrorUnableToSnapshot, "Unsupported image format: " + extension + ". Try one of: png | jpg | jpeg");
  61. return;
  62. }
  63. // TODO: Allow setting quality
  64. image.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
  65. if ("file" == result)
  66. {
  67. // TODO: Allow specifying path
  68. string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "testing." + extension);
  69. using (Stream fileStream = File.Create(path))
  70. {
  71. image.Save(fileStream);
  72. }
  73. }
  74. else if ("base64" == result)
  75. {
  76. MemoryStream stream = new MemoryStream();
  77. image.Save(stream);
  78. byte[] imageBytes = stream.ToArray();
  79. string data = Convert.ToBase64String(imageBytes);
  80. promise.Resolve(data);
  81. }
  82. else if ("data-uri" == result)
  83. {
  84. MemoryStream stream = new MemoryStream();
  85. image.Save(stream);
  86. byte[] imageBytes = stream.ToArray();
  87. string data = Convert.ToBase64String(imageBytes);
  88. data = "data:image/" + extension + ";base64," + data;
  89. promise.Resolve(data);
  90. }
  91. else
  92. {
  93. promise.Reject(ErrorUnableToSnapshot, "Unsupported result: " + result + ". Try one of: file | base64 | data-uri");
  94. }
  95. }
  96. }
  97. }