Starktastic
Starktastic

Reputation: 65

Loading Image from memory in Windows Phone

I'd like to be able to take a photo, display it, and keep the location so I can save it to a record and be able to display it at a later point.

I've been able to display it fine using the code

BitmapImage bmp = newBitmapImage();
bmp.SetSource(e.ChosenPhoto);
myImage.Source = bmp2;

When myImage is the image being displayed, and e is a PhotoResult object. However, as I need to save this in a record, I tried to use this code to display the photo based on the location.

string imageLoc = e.OriginalFileName;
Uri imageUri = new Uri(imageLoc, UriKind.Relative);
StreamResourceInfo resourceInfo = Application.GetResourceStream(imageUri);
BitmapImage bmp = BitmapImage();
bmp.SetSource(resourceInfo.Stream);
myImage.Source = bmp;

When I run this code, I get a System.NullReferenceException. I assume it's to do with the Application.GetResourceStream, but I'm just not certain what's going wrong.

For clarification, I'd like to be able to load and display a photo from a location such as 'C:\Data\Users\Public\Pictures\Camera Roll\imageExample.jpg'

Upvotes: 0

Views: 2794

Answers (2)

Romasz
Romasz

Reputation: 29792

If you want to get pictures from MediaLibrary (Camera roll, Saved Pictures ..) then you can accomplish your task:

Your code can look for example like this (I've edited it to use only Images from Camera Roll):
You get your picture stream with line picture.GetImage() - this method return Stream which you can use for example to copy to IsolatedStorage.

private void MyImg()
{
    using (MediaLibrary mediaLibrary = new MediaLibrary())
       foreach (PictureAlbum album in mediaLibrary.RootPictureAlbum.Albums)
       {
           if (album.Name == "Camera Roll")
           {
             PictureCollection pictures = album.Pictures;
             foreach (Picture picture in pictures)
             {
                 // example how to use it as BitmapImage
                 // BitmapImage image = new BitmapImage();
                 // image.SetSource(picture.GetImage()); 
                 // I've commented that above out, as you can get Memory Exception if
                 // you try to read all pictures to memory and not handle it properly.

                 // Example how to copy to IsolatedStorage
                 using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
                 {
                    if (!storage.DirectoryExists("SavedImg"))
                        storage.CreateDirectory("SavedImg");

                    if (storage.FileExists("SavedImg" + @"\" + picture.Name))
                        storage.DeleteFile("SavedImg" + @"\" + picture.Name);
                    using (IsolatedStorageFileStream file = storage.CreateFile("SavedImg" + @"\" + picture.Name))
                        picture.GetImage().CopyTo(file);
                 }
              }
           }
       }
}

Upvotes: 0

MatDev8
MatDev8

Reputation: 976

if you want to save image in windows phone device, you need to user IsolatedStorage. Save Image =>

            String tempJPEG = "logo.jpg";

            // Create virtual store and file stream. Check for duplicate tempJPEG files.
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myIsolatedStorage.FileExists(tempJPEG))
                {
                    myIsolatedStorage.DeleteFile(tempJPEG);
                }

                IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);


                StreamResourceInfo sri = null;
                Uri uri = new Uri(tempJPEG, UriKind.Relative);
                sri = Application.GetResourceStream(uri);

                BitmapImage bitmap = new BitmapImage();
                bitmap.SetSource(sri.Stream);
                WriteableBitmap wb = new WriteableBitmap(bitmap);

                // Encode WriteableBitmap object to a JPEG stream.
                Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);

                //wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
                fileStream.Close();
            }

Read Image =>

BitmapImage bi = new BitmapImage();

            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("logo.jpg", FileMode.Open, FileAccess.Read))
                {
                    bi.SetSource(fileStream);
                    this.img.Height = bi.PixelHeight;
                    this.img.Width = bi.PixelWidth;
                }
            }
            this.img.Source = bi;

Upvotes: 1

Related Questions