如何保存BitmapImage的WinRT的(How save BitmapImage WinRT)

2019-07-21 22:50发布

我的BitmapImage:

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

我想将图像保存到磁盘在未来看到的。 我无法找到如何做这个工作的例子吗? 第二个问题。 我需要得到一个像素的颜色(如: Color cl=image.GetColorPixel(X,Y)我该怎么办呢? 谢谢!

Answer 1:

下面是我发现以前的某个时候在网络代码。 我不知道,但如果我的rember权是从SDK的样品或WinRT的博客

这一切都归结到WritabelBitmap图像(如已经发布的除外),创建一个解码器,并将它推到流。

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


Answer 2:

您可以通过以下方式保存的BitmapImage。

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

image.png将被保存在应用程序的LocalFolder。

希望能帮助到你!

  • Binaya拉吉·什雷斯塔


Answer 3:

您不能保存或修改BitmapImage 。 你需要使用一个WriteableBitmap来代替。 如果你真的别无选择,只能用BitmapImage的开始-检查其UriSource财产要么重新下载相同的图像或加载WriteableBitmap内容相同的起始BitmapImage 。 需要注意的是UriSource可能在某些情况下是空-即使BitmapImage是给它一个URI加载-这可能是手动或通过重新设置CacheModeBitmapCache上的Image目前正使用给定BitmapImage作为其Source



Answer 4:

看看这个链接。 有显示WriteableBitmap的一些操作的一个片段http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.imaging.writeablebitmap.aspx

这个例子是阅读,但写作应该不是难事。

要通过迭代像素,看看PixelData取出物业。



Answer 5:

如果你想从你的XAML采取UI元素,并将其保存为图像你的运气出来。 在WPF存在是需要的UIElement作为参数上写的位图的方法,但是,是不是有在Windows应用商店中的应用程序。

您可以使用DirectX但这是一个非常复杂的方法。 大部分在商店的绘图应用程序使用JavaScript作为其很容易做保存。



文章来源: How save BitmapImage WinRT