C ++ Windows应用商店应用中的Storageable的WriteableBitmap或BitmapImage

时间:2014-04-18 18:16:38

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

所以我可以保存图像? 图像只能以那些格式找到,而不是文件只是一个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;

无需成为任务它可以无效而没问题......

1 个答案:

答案 0 :(得分:0)

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

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