locked
Saving an Image from a Device's Camera RRS feed

  • Question

  • I have an app in which I am using a third-party assembly. Some of the methods in this assembly allow you to specify a photo in one of two ways:

    1. Passing a String with a path to the file

    2. Passing a Bitmap

    I want to get my image from the device's camera (and then get rid of it), and since Windows Store apps do not support the Bitmap type, I figured option #1 would be best. What I am trying to do is capture the image using my device's camera (which I can do, I have figured out how to get it as a StorageFile, BitmapImage, or WriteableBitmap) and then save it until I am done with it, at which point I will delete it. In a previous app of mine, I was able to save a WriteableBitmap, but in that app the WriteableBitmap was generated and the user chose a filename using a FileSavePicker. Here, the WriteableBitmap is coming from the camera (assuming a WriteableBitmap ends up being involved at all), and I want the app to pick a temporary file name (temp.png, for example). Basically, I need to know how to do the following things:

    1. Capture an image from a device's camera to a new specified file

    2. Delete a specified file

    Any help would be appreciated. Thanks.


    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Thursday, November 7, 2013 9:20 PM

Answers

  • Sorry. You said you already were able to save a WriteableBitmap so I assumed you didn't need the code for that. You can extract its PixelBuffer and pass it to a BitmapEncoder to encode it as a BMP, PNG, etc:

            async void SaveWriteableBitmapToFile(WriteableBitmap wbm, StorageFile file)
            {
                DisplayInformation dispInfo = DisplayInformation.GetForCurrentView();
    
                using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
    
                    encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight,
                        (uint)wbm.PixelWidth,
                        (uint)wbm.PixelHeight,
                        dispInfo.LogicalDpi, dispInfo.LogicalDpi,
                        wbm.PixelBuffer.ToArray());
    
                    await encoder.FlushAsync();
                }
            }

    Friday, November 8, 2013 10:54 PM
    Moderator

All replies

  • See Quickstart: Temporary application datafor information on temporary files. You can use a constant base name and set the CreationCollisionOption to overwrite or to generate a new name if it already exists.

    Double check that the library you are using is valid for Windows Store apps. Using an unavailable Bitmap type (unless it's defined by the library itself) suggests that the library won't be valid.

    Ideally you'd have a library which accepts the bitmap in a stream, but that won't necessarily be in your control.

    --Rob

    Thursday, November 7, 2013 9:28 PM
    Moderator
  • I think that pretty much covers everything necessary for text files, but I need to save an image. I am capturing the image using the following code:
    Dim dialog As New CameraCaptureUI()
    dialog.PhotoSettings.Format = photoformat
    Dim tempfile As StorageFile = Await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo)
    And I have the following code to create the new file (based on the page you sent the link for):
    Dim tempfolder As StorageFolder = ApplicationData.Current.TemporaryFolder
    Dim imgfile As StorageFile = Await tempfolder.CreateFileAsync("tempimage.png", CreationCollisionOption.ReplaceExisting)
    I assume that I will need to use the FileIO.WriteBytesAsync method to save the data to the file, but I am not sure exactly what data I need to write, or even where to get it. Now that I am able to create the temporary file, can you help me figure out how to give it the data that I want it to contain? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Friday, November 8, 2013 10:44 PM
  • Sorry. You said you already were able to save a WriteableBitmap so I assumed you didn't need the code for that. You can extract its PixelBuffer and pass it to a BitmapEncoder to encode it as a BMP, PNG, etc:

            async void SaveWriteableBitmapToFile(WriteableBitmap wbm, StorageFile file)
            {
                DisplayInformation dispInfo = DisplayInformation.GetForCurrentView();
    
                using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
    
                    encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight,
                        (uint)wbm.PixelWidth,
                        (uint)wbm.PixelHeight,
                        dispInfo.LogicalDpi, dispInfo.LogicalDpi,
                        wbm.PixelBuffer.ToArray());
    
                    await encoder.FlushAsync();
                }
            }

    Friday, November 8, 2013 10:54 PM
    Moderator