【问题标题】:How save BitmapImage WinRT如何保存 BitmapImage WinRT
【发布时间】:2013-03-19 08:52:00
【问题描述】:

我有位图图像:

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

我想将图像保存到磁盘以供将来查看。我找不到如何做到这一点的工作示例?第二个问题。我需要获取像素的颜色(例如:Color cl=image.GetColorPixel(X,Y)),我该怎么做? 谢谢!

【问题讨论】:

  • 请考虑将帖子标记为解决方案或指出您最终做了什么

标签: c# windows-runtime


【解决方案1】:

这是我之前在网上找到的代码。我不确定,但如果我没记错的话,它来自 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();
            }
        }

【讨论】:

    【解决方案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 Raj Shrestha

    【讨论】:

      【解决方案3】:

      您无法保存或修改BitmapImage。您需要改用WriteableBitmap。如果您真的别无选择,只能从 BitmapImage 开始 - 检查其UriSource 属性以再次下载相同的图像或加载与起始BitmapImage 内容相同的WriteableBitmap。请注意,UriSource 在某些情况下可能为空——即使 BitmapImage 是通过给它一个 URI 来加载的——它也可能被手动重置或通过setting CacheMode to BitmapCache on an Image that's using the given BitmapImage as its Source 重置。

      【讨论】:

        【解决方案4】:

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

        示例是阅读,但写作应该不难。

        要遍历像素,请查看 PixelData 属性。

        【讨论】:

          【解决方案5】:

          如果您试图从您的 xaml 中获取一个 ui 元素并将其保存为图像,那么您将不走运。在 wpf 中有一个可写位图上的方法,该方法将 UI 元素作为参数,但在 Windows 商店应用程序中不存在。

          您可以使用 DirectX,但这是一种非常复杂的方法。商店中的大多数绘图应用程序都使用 JavaScript,因为它更容易进行保存。

          【讨论】:

            猜你喜欢
            • 2013-03-13
            • 2014-11-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-04-06
            • 1970-01-01
            相关资源
            最近更新 更多