How save BitmapImage WinRT

前端 未结 5 869
攒了一身酷
攒了一身酷 2020-12-21 06:20

I have BitmapImage:

BitmapImage image = new BitmapImage();
image.SetSource(memStream);

I want to save the image to the disk to see in the f

相关标签:
5条回答
  • 2020-12-21 07:03

    You can't save or modify a BitmapImage. You need to use a WriteableBitmap instead. If you really have no choice but start with a BitmapImage - check its UriSource property to either download the same image again or load a WriteableBitmap with same content as the starting BitmapImage. Note that UriSource might be null in some cases - even if the BitmapImage was loaded by giving it a URI - it might be reset manually or by setting CacheMode to BitmapCache on an Image that's using the given BitmapImage as its Source.

    0 讨论(0)
  • 2020-12-21 07:12

    have a look at this link. there's a snippet showing some manipulation of WriteableBitmap http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.imaging.writeablebitmap.aspx

    The example is reading but writing shouldn't be difficult.

    To iterate through the pixels, have a look at PixelData Property.

    0 讨论(0)
  • 2020-12-21 07:14

    Here's a code I found sometime ago in the web. I'm not sure, but if I rember right it was from the sdk samples or a winrt blog

    It all comes down to the WritabelBitmap Image ( like the other already posted), create a decoder and push it to the stream.

      /// <summary>
            /// 
            /// </summary>
            /// <param name="writeableBitmap"></param>
            /// <param name="outputFile"></param>
            /// <param name="encoderId"></param>
            /// <returns></returns>
            public static async Task SaveToFile(
                this WriteableBitmap writeableBitmap,
                IStorageFile outputFile,
                Guid encoderId)
            {
                try
                {
                    Stream stream = writeableBitmap.PixelBuffer.AsStream();
                    byte[] pixels = new byte[(uint)stream.Length];
                    await stream.ReadAsync(pixels, 0, pixels.Length);
    
                    using (var writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        var encoder = await BitmapEncoder.CreateAsync(encoderId, writeStream);
                        encoder.SetPixelData(
                            BitmapPixelFormat.Bgra8,
                            BitmapAlphaMode.Premultiplied,
                            (uint)writeableBitmap.PixelWidth,
                            (uint)writeableBitmap.PixelHeight,
                            96,
                            96,
                            pixels);
                        await encoder.FlushAsync();
    
                        using (var outputStream = writeStream.GetOutputStreamAt(0))
                        {
                            await outputStream.FlushAsync();
                        }
                    }
                }
                catch (Exception ex)
                {
                    string s = ex.ToString();
                }
            }
    
    0 讨论(0)
  • 2020-12-21 07:21

    If your trying to take a ui element from your xaml and save it as an image your out of luck. In wpf there is a method on writable bitmap that takes a UIelement as a parameter but that isn't there in windows store apps.

    You can use DirectX but this is a very complicated approach. Most of the drawing apps on the store use JavaScript as its much easier to do the save.

    0 讨论(0)
  • 2020-12-21 07:24

    You can save BitmapImage in the following way.

    Windows.Storage.StorageFolder localFolder =   Windows.Storage.ApplicationData.Current.LocalFolder;
            StorageFile sampleFile = await localFolder.CreateFileAsync("image.png");
            await FileIO.WriteBytesAsync(sampleFile, bytes);
    

    image.png will be saved in the LocalFolder of the Application.

    Hope it helps!

    • Binaya Raj Shrestha
    0 讨论(0)
提交回复
热议问题