将图像保存并检索到Windows Phone 8.1上的文件

时间:2015-07-24 11:25:12

标签: image file windows-phone-8.1 isolatedstorage

我是Windows手机编程的新手!我的问题是如何在Windows Phone 8.1中保存和检索图像到文件?我已经看到了一些链接,但所有链接都在windows phone 7中。 [unordered associative container 我不会这样,但这段代码只运行到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();
        }

1 个答案:

答案 0 :(得分:0)

这是我将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();
            }