Saving multiple images into isolated storage with wp7 - windows-phone-7

my codes are able to work for saving 1 image into the isolated storage in windows phone 7 but how to change it so that it able to save more than 1 images into the isolated storage.For now, whenever i wanted to save a new image the newest image will overlap the old image so anyone could help me to change my code or with sample that works.Thanks alot
My code :
private void saveButtonClick(object sender, RoutedEventArgs e)
{
try
{
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isf.FileExists("myImage.jpg"))
isf.DeleteFile("myImage.jpg");
using (IsolatedStorageFileStream isfs = isf.CreateFile("myImage.jpg"))
{
var bmp = new WriteableBitmap(myImageElement, myImageElement.RenderTransform);
bmp.SaveJpeg(isfs, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}

YOur files are being overwritten as you're giving each one the same name.
If you want multiple files they'll need to have unique names. Do this by removing the hardcoded name in your code and using appropriate/unique names for each file.

Related

XAML Image source has issues displaying a deep nested path

This is quite vexing.
I am working on an app for image management. Part of the value is the ability to store images in sub-folders based on image properties, eg. creation date.
If I store the image source in a shallow folder (app\images\img.jpg), everything works fine.
If I store the image in KnownFolders.Pictures\source\year\month\day\img.jpg, Image does not render. (Yes, that specific path won't work, I am trying to give you a sense of how the path is constructed)...
The file is actually there. The path is correct (I can open it in a browser, e.g.). The app has access to the file.
But it does not render the bitmap.
I tried to render the bitmap manually using
new BitmapImage(new Uri("KnownFolders.Pictures\source\year\month\day\img.jpg"),UriKind.Absolute))
That does not render anything. (Again, assume the path is valid and has a file at its bottom).
What Am I Missing?
The head scratcher: for GIF anims, I am using Thomas Levesque's useful component: https://github.com/XamlAnimatedGif. That one, unfortunately, does only render gifs... and it does so even when the path is the one given above. So the Standard IMAGE control does not render correctly, but Thomas's control does... infuriating.
An UWP app can't load a BitmapImage from an absolute URL to a file in a folder structure below the Pictures Library Folder.
So this won't work:
var relativePath = #"source\year\month\day\img.jpg";
var imageFile = await KnownFolders.PicturesLibrary.GetFileAsync(relativePath);
var bitmapImage = new BitmapImage(new Uri(imageFile.Path));
However, you could do this:
var relativePath= #"source\year\month\day\img.jpg";
var imageFile = await KnownFolders.PicturesLibrary.GetFileAsync(relativePath);
var bitmapImage = new BitmapImage();
using (var stream = await imageFile.OpenAsync(FileAccessMode.Read))
{
await bitmapImage.SetSourceAsync(stream);
}
So, after way too much time spent on this...
First, link to DataContextChanged of the IMAGE element. In there, parse the DataContext out. If you are using the IMAGE outside of an ItemsControl etc, this is not required...
private async void ImageView_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
{
if (sender is Image)
{
Image img = (Image)sender;
if (img.DataContext is ImageView)
{
MyViewDataContext dc = (MyViewDataContext)img.DataContext;
img.Source = await dc.bitmap();
}
}
}
And here the implementation of MyViewDataContext.bitmap() which has a property called source that yields, you guessed it, absolute paths:
public async Task<BitmapImage> MyViewDataContext.bitmap()
{
if (_bitmap == null)
{
try
{
StorageFile file = await StorageFile.GetFileFromPathAsync(source);
bool r = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.CheckAccess(file);
if (r)
{
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
// create a new bitmap, coz the old one must be done for...
_bitmap = new BitmapImage();
// And get that bitmap sucked in from stream.
await _bitmap.SetSourceAsync(fileStream);
}
}
}
catch (Exception e)
{
_bitmap = null;
}
}
return _bitmap;
}
BitmapImage _bitmap;
I cache the resulting bitmap until I dispose of this MyViewDataContext.
I am now most concerned about memory. This one worries me:
How to dispose BitmapImage cache?
So, as a tech debt, I am going to address the potential mem leaks later, once this whole thing is on the test bench and I can take a look at its runtime behavior...
To access the folders and libraries represented by the properties of this class, specify the corresponding capabilities in your app manifest. For example, to access KnownFolders.PicturesLibrary, specify the Pictures Library capability in the app manifest.
Hope this will help
KnowFolders

WP7: Get image's name which images are stored in Libary picture?

I have some images that are stored in Media Libary, view here. Now I need to get the name of an image after I choose the image by using photo chooser task. I used photo chooser task to select an image and then got the path of the image. My pupose is get the name from the path:
private void button1_Click(object sender, RoutedEventArgs e)
{
PhotoChooserTask objPhotoChooser = new PhotoChooserTask();
objPhotoChooser.Completed += new EventHandler<PhotoResult>(PhotoChooseCall);
objPhotoChooser.Show();
}
void PhotoChooseCall(object sender, PhotoResult e)
{
switch (e.TaskResult)
{
case TaskResult.OK:
BinaryReader objReader = new BinaryReader(e.ChosenPhoto);
image1.Source = new BitmapImage(new Uri(e.OriginalFileName));
MessageBox.Show("Photo's name: " + e.OriginalFileName.ToString());
break;
case TaskResult.Cancel:
MessageBox.Show("Cancelled");
break;
case TaskResult.None:
MessageBox.Show("Nothing Entered");
break;
}
}
Output:
Photo's name: \Applications\Data\C80566AB-E17E-495C-81A1-3FCAE34D3DEDE\Data\PlatformData\PhotoChooser-a8208960-3597-40fc-9b4f-869afcf822b6.jpg
After I choose the same image. The name of it will change (PhotoChooser-a8208960-3597-40fc-9b4f-869afcf822b6.jpg will change). I think it's not the name of the photo.
So:
Can we get the name of the image?
And how do we do?
You can get the actual file names. Use the MediaLibrary for that:
MediaLibrary lib = new MediaLibrary();
var collection = lib.RootPictureAlbum;
foreach (var p in collection.Albums[0].Pictures)
Debug.WriteLine(p.Name);
Notice that I am specifying the album index inside the root picture album. 0 will be for sample pictures, and so on. If you need to grab the contents of the image, just use p.GetImage(); to get the readable stream.
No. You cannot get the name of the file as it is in the pictures hub. You are given a copy of the stream and that has a different, temporary, name.

Win Phone 7 quiz app

Could any1 please suggest a method by which i can store all my questions , Multiple Choice answers and the correct answer. So that i can call them and then display in a text box and radio buttons . And as when the user answers a question correctly i should be able to move to the next question.
This was my approach. Used data serialization, created a class with Data memebers which will store question id , questions and answers. then created an object for it in while page is loading. But i am unable to display the questions. Please help me out.
Depending on the number of questions, you might find it easier and faster to use a local database.
I am a little confused at your approach. Serialization by itself doesnt actually persist data. Perhaps that is your problem. I have found that storing the XML to IsolatedStorage is one of the easier ways to persist data.
I created an IsolatedStorage class that looks like this for saving an XDocument object.
public static void SaveDataToIsolatedStorage(string filePath, FileMode fileMode, XDocument xDoc)
{
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream location = new IsolatedStorageFileStream(filePath, fileMode, storage))
{
System.IO.StreamWriter file = new System.IO.StreamWriter(location);
xDoc.Save(file);
}
}
}
Here is my reader.
private static XDocument ReadDataFromIsolatedStorageXmlDoc()
{
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!storage.FileExists(filePath))
{
return new XDocument();
}
using (var isoFileStream = new IsolatedStorageFileStream(filePath, FileMode.OpenOrCreate, storage))
{
using (XmlReader reader = XmlReader.Create(isoFileStream))
{
return XDocument.Load(reader);
}
}
}
}

Saving multiple images to isolated storage

I was trying to save multiple images into isolated storage by using creating a imageFolder in isolated storage and storing all my images inside.But it have an error so please anyone could help me solve the error or got other method way help me thanks.IF possible I would appreciate if you guys can show me your code that works. Actually my code would like to be under a button event handler.Thanks And the error is :Operation not permitted on IsolatedStorageFileStream.
My Code :
private void SaveToLocalStorage(string imageFolder, string imageFileName)
{
imageFileName = name.Text;
MessageBox.Show(imageFileName);
var isf = IsolatedStorageFile.GetUserStoreForApplication();
if (isf.DirectoryExists(imageFolder))
{
isf.CreateDirectory(imageFolder);
}
string filePath = Path.Combine(imageFolder, imageFileName);
MessageBox.Show(filePath);
using (var stream = isf.CreateFile(filePath))
{
var bmp= new WriteableBitmap(inkCanvas, inkCanvas.RenderTransform);
bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
}
}
First off, you probably want to be creating the directory if it DOESN'T exist, not if it does:
if (!isf.DirectoryExists(imageFolder))
{
isf.CreateDirectory(imageFolder);
}

xna - why cant i see the bitmap class while adding photos through photoChooser

I am trying to add photos to my game through photochooser.
I am using all the right usings (images, shell, etc.) but can't get to the bitmap class
What am I missing?
Update:
I tried using System.Windows.Media.Imaging.
and still can't find the bitmapimage class.
I can't find using System.Windows.dll.
can you be more specific? I get a sgwigly line below bitmapImage.
void photoChooserTask_Completed(object sender, PhotoResult result)
{
if (result.TaskResult == TaskResult.OK)
{
var bitmapImage=new
System.Windows.Media.Imaging.BitmapImage bmp =
new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(result.ChosenPhoto);
}
}
If you're trying to use System.Bitmap then this isn't supported on the phone.
You probably want System.Windows.Media.Imaging.BitmapImage (in System.Windows.dll) instead.

Resources