Question

I'm trying to take and save a photo using a windows surface device.

I'm using the code below to take a photo and this work but I'd like to automatically create a directory on the device's local drive and save this photo there without any dialog prompts.

So the code I use to capture to photo is as follows:

CameraCaptureUI camera = new CameraCaptureUI();

StorageFile file = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file!=null)
{
    using (IRandomAccessStream ras=await file.OpenAsync(FileAccessMode.Read))
    {
        BitmapImage source = new BitmapImage();
        source.SetSource(ras);

        imageBuildingPhoto.Source = source; // this is just an image control.
    }
}

So after this I'd like to automatically save the photo to a new directory. e.g.

My Pictures\NewDirectory\Photo1.jpg

Anybody got any idea how I can do this?

This is a windows store application written using C#4.5 and XAML.

Thanks in advance

Was it helpful?

Solution

Use the CopyAsync method on the StorageFile object you get back (file). You can specify a directory and file name. If you need to create your own directory structure, you will need to enable access to the appropriate library in the Package Manifest then create it in code. You will then use the StorageFolder class and its CreateFolderAsync method to create folders.

http://aka.ms/30Days has some great resources for learning about scenarios like this. Might be worth checking out.

OTHER TIPS

Your code will need to look to see if that folder exists and create it if it does not. Your app will need to declare the capability to access the user's Photos library in the app manifest, too.

To take a picture, your code is correct. I have a walkthrough in case you want to verify it against some other code: http://blog.jerrynixon.com/2012/10/walkthrough-capturing-photos-in-your.html

To interact with the file system, this can be tricky, but I have a longer write up on that if you want to reference it: http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html

The answer to your question is, yes you can. I have done it in my own apps. Now, it's just a matter of you implementing it in yours. You will find it to be pretty easy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top