Skip to content

Commit 0cd47de

Browse files
author
Alex Petuschak
committed
Ported the plugin to WP8
1 parent a4dd083 commit 0cd47de

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

src/wp8/Canvas2ImagePlugin.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
9+
public class Canvas2ImagePlugin : BaseCommand
10+
{
11+
public Canvas2ImagePlugin()
12+
{
13+
}
14+
15+
public void saveImageDataToLibrary(string jsonArgs)
16+
{
17+
try
18+
{
19+
var options = JsonHelper.Deserialize<string[]>(jsonArgs);
20+
21+
string imageData = options[0];
22+
byte[] imageBytes = Convert.FromBase64String(imageData);
23+
24+
using (var imageStream = new MemoryStream(imageBytes))
25+
{
26+
imageStream.Seek(0, SeekOrigin.Begin);
27+
28+
string fileName = String.Format("c2i_{0:yyyyMMdd_HHmmss}", DateTime.Now);
29+
var library = new MediaLibrary();
30+
var picture = library.SavePicture(fileName, imageStream);
31+
32+
if (picture.Name.Contains(fileName))
33+
{
34+
DispatchCommandResult(new PluginResult(PluginResult.Status.OK,
35+
"Image saved: " + picture.Name));
36+
}
37+
else
38+
{
39+
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR,
40+
"Failed to save image: " + picture.Name));
41+
}
42+
}
43+
}
44+
catch (Exception ex)
45+
{
46+
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, ex.Message));
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)