admin管理员组文章数量:1420205
var myImage = canvas.toDataURL("image/png");
I think myImage
has the image bytes encoded in png format
now how to save myImage
as a file (in images folder)?
var myImage = canvas.toDataURL("image/png");
I think myImage
has the image bytes encoded in png format
now how to save myImage
as a file (in images folder)?
2 Answers
Reset to default 6Instead of using .toDataUrl
, you need to use .msToBlob
:
var blob = canvas.msToBlob();
Then, you'll need to write this out to disk:
var output;
var input;
var outputStream;
Windows.Storage.ApplicationData.current.localFolder.createFileAsync("yourFile",
Windows.Storage.CreationCollisionOption.replaceExisting).
then(function(file) {
return file.openAsync(Windows.Storage.FileAccessMode.readWrite);
}).then(function(stream) {
outputStream = stream;
output = stream.getOutputStreamAt(0);
input = blob.msDetachStream();
return Windows.Storage.Streams.RandomAccessStream.copyAsync(input, output);
}).then(function() {
return output.flushAsync();
}).done(function() {
input.close();
output.close();
outputStream.close();
});
In your applications app data folder, you will now have the image written to disk.
If you wish to place it somewhere else -- e.g. my pictures etc -- then you'll just need to use one of the other storage folders. See the sample here. Note that to access the pictures library you need to add that capability to your manifest (just a checkbox in the package.appxmanifest editor in VS)
There are many other imaging options too for more plex manipulation of the output file. See the imaging sample for code.
I found this to be the most helpful bit of code from the simple imaging sample. It lets you encode to PNG or JPG rather than just dumping the canvas data.
Helpers.getFileFromSavePickerAsync().then(function (file) {
filename = file.name;
switch (file.fileType) {
case ".jpg":
encoderId = Imaging.BitmapEncoder.jpegEncoderId;
break;
case ".bmp":
encoderId = Imaging.BitmapEncoder.bmpEncoderId;
break;
case ".png":
default:
encoderId = Imaging.BitmapEncoder.pngEncoderId;
break;
}
return file.openAsync(Windows.Storage.FileAccessMode.readWrite);
}).then(function (_stream) {
stream = _stream;
// BitmapEncoder expects an empty output stream; the user may have selected a
// pre-existing file.
stream.size = 0;
return Imaging.BitmapEncoder.createAsync(encoderId, stream);
}).then(function (encoder) {
var width = id("outputCanvas").width;
var height = id("outputCanvas").height;
var outputPixelData = Context.getImageData(0, 0, width, height);
encoder.setPixelData(
Imaging.BitmapPixelFormat.rgba8,
Imaging.BitmapAlphaMode.straight,
width,
height,
96, // Horizontal DPI
96, // Vertical DPI
outputPixelData.data
);
return encoder.flushAsync();
}).then(function () {
WinJS.log && WinJS.log("Saved new file: " + filename, "sample", "status");
id("buttonSave").disabled = false;
id("buttonRender").disabled = false;
}).then(null, function (error) {
WinJS.log && WinJS.log("Failed to save file: " + error.message, "sample", "error");
}).done(function () {
stream && stream.close();
});
本文标签: javascriptHow to save html5 canvas as an image file in window 8 metro appStack Overflow
版权声明:本文标题:javascript - How to save html5 canvas as an image file in window 8 metro app? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745326024a2653594.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论