How save BitmapImage WinRT

2019-01-28 10:01发布

问题:

I have BitmapImage:

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

I want to save the image to the disk to see in the future. I can not find a work example of how to do this? The second question. I need to get the color of a pixel (like: Color cl=image.GetColorPixel(X,Y)), how can I do that? Thank you!

回答1:

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


回答2:

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


回答3:

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.



回答4:

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.



回答5:

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.