locked
Writing image data to disk RRS feed

  • Question

  • Hello im working on a winJs app that write image to disk ! 

    i've found a way on msdn : 

    var savePicker = new Windows.Storage.Pickers.FileSavePicker;
                        var Imaging = Windows.Graphics.Imaging;
                        savePicker.fileTypeChoices.insert("Photo", [".jpg"]);
                        savePicker.suggestedFileName = "WinstagramPhoto";
                        savePicker.suggestedStartLocation = pickers.PickerLocationId.desktop;
                        savePicker.fileTypeChoices.insert("Photo", [".png"]);
                    
    
                        savePicker.pickSaveFileAsync().then(function (file) {
                            if (file) {
                                return file.openAsync(Windows.Storage.FileAccessMode.readWrite);                
                            } else {
                                return WinJS.Promise.wrapError("No file selected");
                            }
                        }).then(function (stream) {
                            var fileStream = stream;
                            var canvas = document.getElementById("photoCanvas");            
                            var ctx = canvas.getContext("2d");
                            var 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
                        });
                     

    but i get the error : 

    <encoding> is undefined ! 

    can someone help please

    Wednesday, April 9, 2014 12:09 PM

Answers

  • Hi Amine,

    <encoding> is undefined, not sure where you get this code snippet, but I would recommend you to use following code for a better understanding(kind of replace of "encoding"), fill your own width and height for here.

                 encoder.setPixelData(
                        Imaging.BitmapPixelFormat.rgba8,
                        Imaging.BitmapAlphaMode.straight,
                        width,
                        height,
                        96, // Horizontal DPI
                        96, // Vertical DPI
                        new Uint8Array(imgData.data)
                        );
    

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, April 10, 2014 2:58 AM
    Moderator

All replies

  • Hi Amine,

    <encoding> is undefined, not sure where you get this code snippet, but I would recommend you to use following code for a better understanding(kind of replace of "encoding"), fill your own width and height for here.

                 encoder.setPixelData(
                        Imaging.BitmapPixelFormat.rgba8,
                        Imaging.BitmapAlphaMode.straight,
                        width,
                        height,
                        96, // Horizontal DPI
                        96, // Vertical DPI
                        new Uint8Array(imgData.data)
                        );
    

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, April 10, 2014 2:58 AM
    Moderator
  • I've found the Solution by random ! Thanks a lot 
    Thursday, April 10, 2014 4:29 PM