【问题标题】:WriteableBitmap or BitmapImage to StorageFile in C++ Windows Store AppWriteableBitmap 或 BitmapImage 到 C++ Windows Store App 中的 StorageFile
【发布时间】:2014-04-18 18:16:38
【问题描述】:

所以我可以保存图像? 该图像只能以那些格式找到,它不是文件,它只是一个 BitmapImage。

我在 c# 中知道这种方式,有什么想法可以在 C++ 中实现吗?

    private static async Task<StorageFile> SaveAsJpeg(WriteableBitmap wb)
{
     byte[] pixels;
     using (var stream = wb.PixelBuffer.AsStream())
     {
        pixels = new byte[(uint)stream.Length];
        await stream.ReadAsync(pixels, 0, pixels.Length);
     }
     var name = String.Format("{0}_{1:yyyy-MM-dd_HH-mm-ss}.jpg", "MyApp", DateTime.Now);
     var outputFile = await KnownFolders.PicturesLibrary.CreateFileAsync(name, CreationCollisionOption.ReplaceExisting);
     using (var writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
     {
        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, writeStream);
        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied,
                             (uint)wb.PixelWidth, (uint)wb.PixelHeight,
                             96, 96, pixels);
        await encoder.FlushAsync();

        using (var outputStream = writeStream.GetOutputStreamAt(0))
        {
           await outputStream.FlushAsync();
        }
     }
     return outputFile;

不必是任务,它可以是无效的没问题...

【问题讨论】:

    标签: c++ windows-store-apps bitmapimage writeablebitmap storagefile


    【解决方案1】:

    首先,如果您首先加载了BitmapImage,那么您就可以访问源文件,因此最简单的解决方案是复制源文件。至于WriteableBitmap - 它有点类似于 C# 版本,但是当您开始使用类似 COM 的技术时,当然会稍微复杂一些。看看詹姆斯对我关于获取像素的问题的回答是否对您有所帮助:

    How to get access to WriteableBitmap.PixelBuffer pixels with C++?

    【讨论】:

    • 真的很复杂,我只能告诉你我不能使用源文件流。
    • 嗯,使用源流是最好的方法 - 重新编码图像是一个糟糕的性能选择。如果您绝对不能这样做(例如,如果图像位于可能已被删除的 SD 卡上)- 请使用 WriteableBitmaps 和我链接的答案中的像素缓冲区访问方法。
    • 我想了解如何将 Ibuffer 保存到文件中,对此有何想法?
    • 与您的示例中的方式相同。你有具体的问题吗?正如我所说 - 您可以按照链接中描述的方式从 IBuffer 获取字节数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    相关资源
    最近更新 更多