【问题标题】:Save and Retrieve Image to a file on Windows Phone 8.1将图像保存并检索到 Windows Phone 8.1 上的文件
【发布时间】:2015-07-24 11:25:12
【问题描述】:

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

【问题讨论】:

    标签: image file windows-phone-8.1 isolatedstorage


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多