Question

I want to set an image choosen from picture library as background. So I take the originalname of the choosen photo in IsolatedStorageSetting. But later i do not manage to get the stream of the file from the path.. here the code:

bitmapimage.UriSource = new Uri(Settings.BackgroundPhotoUrl, UriKind.Absolute);
BackgroundImg.ImageSource = bitmapimage;

But this code does not work. No exception. Just the background is black. So I have tried to save the Stream in IsolatedStorageSetting (I do not relly like this solution!!) but in this case I obtain an exception:

Operation denied

Here the code:

Settings.MyStream = e.ChosenPhoto

In the end, I have tried to save the image in isolated storage:

using (System.IO.IsolatedStorage.IsolatedStorageFile isf = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
{
    isf.CopyFile(e.OriginalFileName, "background" + System.IO.Path.GetExtension(e.OriginalFileName), true);
}

But also in this case i obtain an operation denied exception

How can I solve the problem?? Thanx

Was it helpful?

Solution

It seems you are misunderstanding streams. A stream is a pointer to a position in a file you can read from or write to. If you are you are using a photo chooser then the result will give you a stream to the file. You need to read the bytes from that at stream and save then you your local storage. Then you can access the image from there.

In my Live Countdown app I use the WriteableBitmap class to save the Jpeg to a stream. Something along the lines of this:

var store =     
    System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
var newPath = "MyFileName.png";

if (store.FileExists(newPath)) store.DeleteFile(newPath);

var stream = store.CreateFile(newPath);

BitmapImage i = new BitmapImage();
i.SetSource(photoResult.ChosenPhoto);
WriteableBitmap imageToSave = new WriteableBitmap(i);
imageToSave.SaveJpeg(stream, 173, 173, 0, 100);

stream.Flush();
stream.Close();

That is kind of the flow. I had to take parts from different functions and put them together as the app allows the user to scale the app first. There is scaling the SaveJpeg method parameters as I am saving the image for a tile.

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