在Windows Phone 8.1上保存和检索图像到文件



我是windows phone编程新手!我的问题是如何保存和检索图像到一个文件在windows phone 8.1?我已经看到了一些链接,但它们都是在windows phone 7上。[http://www.geekchamp.com/tips/all-about-wp7-isolated-storage--read-and-save-images] [1]我不会这样做,但这段代码只适用于windows phone 7

//为单独存储的JPEG文件创建一个文件名。

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

这是我如何保存一个UI元素到png:

            StorageFolder localFolder = ApplicationData.Current.TemporaryFolder;
            StorageFile file = await localFolder.CreateFileAsync(file.png, CreationCollisionOption.ReplaceExisting);
            var renderTargetBitmap = new RenderTargetBitmap();
            await renderTargetBitmap.RenderAsync(element);
            var pixels = await renderTargetBitmap.GetPixelsAsync();
            using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
                byte[] bytes = pixels.ToArray();
                encoder.SetPixelData(
                    BitmapPixelFormat.Bgra8,
                    BitmapAlphaMode.Straight,
                    (uint)renderTargetBitmap.PixelWidth,
                    (uint)renderTargetBitmap.PixelHeight,
                    DisplayInformation.GetForCurrentView().LogicalDpi,
                    DisplayInformation.GetForCurrentView().LogicalDpi,
                    bytes);
                await encoder.FlushAsync();
            }

最新更新