No Description

RNViewShotModule.cs 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Newtonsoft.Json.Linq;
  2. using ReactNative.Bridge;
  3. using ReactNative.UIManager;
  4. using System;
  5. using System.IO;
  6. using System.Collections.Generic;
  7. using Windows.ApplicationModel.Core;
  8. using Windows.UI.Core;
  9. namespace RNViewShot
  10. {
  11. /// <summary>
  12. /// A module that allows JS to share data.
  13. /// </summary>
  14. class RNViewShotModule : ReactContextNativeModuleBase
  15. {
  16. private const string ErrorUnableToSnapshot = "E_UNABLE_TO_SNAPSHOT";
  17. private readonly ReactContext _reactContext;
  18. /// <summary>
  19. /// Instantiates the <see cref="RNViewShotModule"/>.
  20. /// </summary>
  21. public RNViewShotModule(ReactContext reactContext) : base(reactContext)
  22. {
  23. this._reactContext = reactContext;
  24. }
  25. /// <summary>
  26. /// The name of the native module.
  27. /// </summary>
  28. public override string Name
  29. {
  30. get
  31. {
  32. return "RNViewShot";
  33. }
  34. }
  35. [ReactMethod]
  36. public void takeSnapshot(int tag, JObject options, IPromise promise)
  37. {
  38. string format = options["format"] != null ? options.Value<string>("format") : "png";
  39. //Bitmap.CompressFormat compressFormat =
  40. // format.equals("png")
  41. // ? Bitmap.CompressFormat.PNG
  42. // : format.equals("jpg") || format.equals("jpeg")
  43. // ? Bitmap.CompressFormat.JPEG
  44. // : format.equals("webm")
  45. // ? Bitmap.CompressFormat.WEBP
  46. // : null;
  47. //if (compressFormat == null)
  48. //{
  49. // promise.reject(ErrorUnableToSnapshot, "Unsupported image format: " + format + ". Try one of: png | jpg | jpeg");
  50. // return;
  51. //}
  52. double quality = options["quality"] != null ? options.Value<double>("quality") : 1.0;
  53. int? width = options["width"] != null ? options.Value<int?>("width") : null;
  54. int? height = options["height"] != null ? options.Value<int?>("height") : null;
  55. string result = options["result"] != null ? options.Value<string>("result") : "file";
  56. bool snapshotContentContainer = options["snapshotContentContainer"] != null ? options.Value<bool>("snapshotContentContainer") : false;
  57. UIManagerModule uiManager = this._reactContext.GetNativeModule<UIManagerModule>();
  58. uiManager.AddUIBlock(new ViewShot(tag));
  59. //try
  60. //{
  61. // string name = options["filename"] != null ? options.Value<string>("filename") : null;
  62. // //File tmpFile = "file" == result ? createTempFile(this._reactContext, format, name) : null;
  63. // UIManagerModule uiManager = this._reactContext.GetNativeModule<UIManagerModule>();
  64. // //uiManager.addUIBlock(new ViewShot(tag, format, compressFormat, quality, width, height, tmpFile, result, snapshotContentContainer, promise));
  65. //}
  66. //catch (Exception e)
  67. //{
  68. // promise.reject(ErrorUnableToSnapshot, "Failed to snapshot view tag " + tag);
  69. //}
  70. //Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
  71. //Graphics graphics = Graphics.FromImage(bitmap as Image);
  72. //graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
  73. //bitmap.Save("c:\\screenshot.jpeg", ImageFormat.Jpeg);
  74. promise.Resolve("Format: " + format + " Quality: " + quality);
  75. }
  76. }
  77. }