cordova plugin for saving base64 image
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Base64ImageSaverPlugin.cs 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Microsoft.Xna.Framework.Media;
  2. using System;
  3. using System.IO;
  4. using System.Text;
  5. using WPCordovaClassLib.Cordova;
  6. using WPCordovaClassLib.Cordova.Commands;
  7. using WPCordovaClassLib.Cordova.JSON;
  8. public class Base64ImageSaverPlugin : BaseCommand
  9. {
  10. public Base64ImageSaverPlugin()
  11. {
  12. }
  13. public void saveImageDataToLibrary(string jsonArgs)
  14. {
  15. try
  16. {
  17. var options = JsonHelper.Deserialize<string[]>(jsonArgs);
  18. string imageData = options[0];
  19. byte[] imageBytes = Convert.FromBase64String(imageData);
  20. using (var imageStream = new MemoryStream(imageBytes))
  21. {
  22. imageStream.Seek(0, SeekOrigin.Begin);
  23. string fileName = String.Format("c2i_{0:yyyyMMdd_HHmmss}", DateTime.Now);
  24. var library = new MediaLibrary();
  25. var picture = library.SavePicture(fileName, imageStream);
  26. if (picture.Name.Contains(fileName))
  27. {
  28. DispatchCommandResult(new PluginResult(PluginResult.Status.OK,
  29. "Image saved: " + picture.Name));
  30. }
  31. else
  32. {
  33. DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR,
  34. "Failed to save image: " + picture.Name));
  35. }
  36. }
  37. }
  38. catch (Exception ex)
  39. {
  40. DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, ex.Message));
  41. }
  42. }
  43. }