在 UWP 中将画布另存为图像

TKoL

我找到了这个用于将 Canvas 导出和保存为 png 的文档它给出了以下代码:

var Imaging = Windows.Graphics.Imaging;
var picker = new Windows.Storage.Pickers.FileSavePicker();
picker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
picker.fileTypeChoices.insert("PNG file", [".png"]);
var imgData, fileStream = null;
picker.pickSaveFileAsync().then(function (file) {            
    if (file) {
        return file.openAsync(Windows.Storage.FileAccessMode.readWrite);                
    } else {
        return WinJS.Promise.wrapError("No file selected");
    }
}).then(function (stream) {
    fileStream = stream;
    var canvas = document.getElementById("canvas1");            
    var ctx = canvas.getContext("2d");
    imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);

    return Imaging.BitmapEncoder.createAsync(Imaging.BitmapEncoder.pngEncoderId, stream);
}).then(function (encoder) {
    //Set the pixel data--assume "encoding" object has options from elsewhere
    encoder.setPixelData(encoding.pixelFormat, encoding.alphaMode,
        encoding.width, encoding.height, encoding.dpiX, encoding.dpiY,
        new Uint8Array(imgData.data));
    //Go do the encoding
    return encoder.flushAsync();
}).done(function () {
    //Make sure to do this at the end
    fileStream.close();  
}, function () {
    //Empty error handler (do nothing if the user canceled the picker
});

在'encoder.setPixelData' 区域,他们使用一个名为'encoding' 的对象来设置所有值,但没有解释在前面的任何步骤中如何创建这个对象。

我如何创建这个“编码”对象来完成这个例子?

TKoL

我找到了另一种保存画布的方法,仍然没有解决我最初的问题

我可以将画布转换为 blob 流并将其保存到 png 文件中

function saveCanvasAsImage(canvasElement) {
    var picker = new Windows.Storage.Pickers.FileSavePicker();
    picker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
    picker.fileTypeChoices.insert("PNG file", [".png"]);

    var input, output = null;
    var blob = canvasElement.msToBlob();

    return picker.pickSaveFileAsync()
        .then(function (file) {
            if (file) {
                return file.openAsync(Windows.Storage.FileAccessMode.readWrite);
            } else {
                return WinJS.Promise.wrapError("No file selected");
            }
        })
        .then(function (op) {
            output = op;
            input = blob.msDetachStream();

            return Windows.Storage.Streams.RandomAccessStream.copyAsync(input, output);
        })
        .then(function() {
            return output.flushAsync();
        })
        .done(function() {
            input.close();
            output.close();
        }, function(e) {
            // handle error here
        });
}

不幸的是,它使用透明背景保存,但也许有一种方法可以让我的画布具有白色背景。

[编辑] - -

所以我找到了一种更直接地回答我原来问题的方法:

function saveCanvasAsImage(canvasElement) {
    var Imaging = Windows.Graphics.Imaging;
    var picker = new Windows.Storage.Pickers.FileSavePicker();
    picker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
    // picker.fileTypeChoices.insert("JPEG file", [".jpg"]);
    picker.fileTypeChoices.insert("PNG file", [".png"]);
    var fileStream, decoder, encoding, pixels = null;


    var encoderIds = {
        '.png': Imaging.BitmapEncoder.pngEncoderId,
        '.jpg': Imaging.BitmapEncoder.jpegEncoderId
    }

    var encoderId = encoderIds['.jpg'];

    var blob = canvasElement.msToBlob();

    return Imaging.BitmapDecoder.createAsync(Imaging.BitmapDecoder.pngDecoderId,
            blob.msDetachStream())
        .then(function (dc) {
            decoder = dc;

            return picker.pickSaveFileAsync();
        }).then(function (file) {
            if (file) {
                encoderId = encoderIds[file.fileType];
                return file.openAsync(Windows.Storage.FileAccessMode.readWrite);
            } else {
                return WinJS.Promise.wrapError("No file selected");
            }
        }).then(function(stream) {
            fileStream = stream;

            var transform = new Windows.Graphics.Imaging.BitmapTransform();

            return decoder.getPixelDataAsync(
                decoder.bitmapPixelFormat,
                decoder.bitmapAlphaMode,
                transform,
                Windows.Graphics.Imaging.ExifOrientationMode.respectExifOrientation,
                Windows.Graphics.Imaging.ColorManagementMode.colorManageToSRgb
            );

        }).then(function (pixelProvider) {

            pixels = pixelProvider.detachPixelData();

            return Imaging.BitmapEncoder.createAsync(encoderId, fileStream);
        }).then(function (encoder) {
            encoding = decoder;
            //Set the pixel data--assume "encoding" object has options from elsewhere
            encoder.setPixelData(encoding.bitmapPixelFormat, encoding.bitmapAlphaMode,
                encoding.pixelWidth, encoding.pixelHeight, encoding.dpiX, encoding.dpiY,
                pixels);
            //Go do the encoding
            return encoder.flushAsync();
        }).done(function () {
            //Make sure to do this at the end
            fileStream.close();
        }, function () {
            //Empty error handler (do nothing if the user canceled the picker
        });
}

我已经注释掉了另存为 JPG 的能力,因为这是一个新问题,但这适用于 png

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Python 3中将Turtle画布另存为图像(.png或.jpg)

来自分类Dev

如何在Selenium中将画布另存为PNG?

来自分类Dev

将画布另存为图像Internet Explorer

来自分类Dev

右键单击画布时另存为图像

来自分类Dev

如何在 UWP 中使用 C# 将网页另存为 html 文件?

来自分类Dev

如何将整个画布另存为android画布中的jpeg图像?

来自分类Dev

将Windows Store应用中的画布另存为图像文件

来自分类Dev

将fabric.js画布另存为PC上的图像

来自分类Dev

如何将奇异果画布对象另存为图像文件

来自分类Dev

将Windows Store应用中的画布另存为图像文件

来自分类Dev

Onclick将画布另存为服务器上的图像

来自分类Dev

KineticJS:如何使IE9将画布另存为图像?

来自分类Dev

在PHP上获取500 Internal Server Error并将画布另存为图像

来自分类Dev

Highcharts:将画布图像另存为IE中的本地文件

来自分类Dev

将画布另存为 jpg 时获取 1x1px 图像

来自分类Dev

在Windows中将文件另存为.RProfile

来自分类Dev

在GUI Matlab中将另存为按钮

来自分类Dev

将Wikipedia表另存为图像

来自分类Dev

将谷歌图表另存为图像

来自分类Dev

将位图另存为jpeg图像

来自分类Dev

生成div的图像并另存为

来自分类Dev

将目标纹理另存为图像

来自分类Dev

将大型网页另存为图像

来自分类Dev

JSP将“渲染的”图像另存为

来自分类Dev

在iPhone上另存为图像

来自分类Dev

Rails将文件另存为图像

来自分类Dev

将位图另存为jpeg图像

来自分类Dev

将图像数组另存为PFFiles

来自分类Dev

如何在Eclipse中将“解析树可视化”另存为图像?