Зареждане на изображение от паметта в Windows Phone

Бих искал да мога да направя снимка, да я покажа и да запазя местоположението, за да мога да я запазя в запис и да мога да я покажа на по-късен етап.

Успях да го покажа добре с помощта на кода

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

Когато myImage е изображението, което се показва, а e е обект PhotoResult. Въпреки това, тъй като трябва да запазя това в запис, се опитах да използвам този код, за да покажа снимката въз основа на местоположението.

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;

Когато стартирам този код, получавам System.NullReferenceException. Предполагам, че е свързано с Application.GetResourceStream, но просто не съм сигурен какво се обърка.

За пояснение, бих искал да мога да заредя и покажа снимка от местоположение като „C:\Data\Users\Public\Pictures\Camera Roll\imageExample.jpg“


person Starktastic    schedule 04.02.2014    source източник


Отговори (2)


ако искате да запазите изображение в устройство с Windows Phone, трябва да използвате IsolatedStorage. Запазване на изображението =>

            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();
            }

Прочетете изображение =>

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;
person MatDev8    schedule 04.02.2014
comment
Снимката ми автоматично се записва в ролката на фотоапарата в „C:\Data\Users\Public\Pictures\Camera Roll\imageExample.jpg“, необходим ли е първият блок от код? - person Starktastic; 04.02.2014
comment
Windows phone или windows 8. Не можете да получите достъп до картина с този път, когато използвате windows phone. Когато направите снимка с кода на приложението си, снимката се запазва с този път 'C:\Data\Users\Public\Pictures\Camera Roll\imageExample.jpg'? - person MatDev8; 04.02.2014
comment
Само за тест направете това :string imageLoc = e.OriginalFileName; Uri imageUri = нов Uri(imageLoc, UriKind.Relative); StreamResourceInfo resourceInfo = Application.GetResourceStream(imageUri); BitmapImage bmp = BitmapImage(); bmp.UriSource = imageUri; myImage.Source = bmp; - person MatDev8; 04.02.2014

Ако искате да получите снимки от MediaLibrary (Camera roll, Saved Pictures ..), тогава можете да изпълните задачата си:

Вашият код може да изглежда например така (редактирах го, за да използвам само изображения от Camera Roll):
Получавате своя поток от снимки с line picture.GetImage() - този метод връща Stream, който можете да използвате например за копирайте в 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);
                 }
              }
           }
       }
}
person Romasz    schedule 04.02.2014