BitmapEncoder SetPixelData内存分配

时间:2013-05-03 09:59:34

标签: c# microsoft-metro winrt-async

我正在开发一个Metro应用程序,我需要生成一个动画GIF图像。

我发现this tutorial,女巫似乎是Metro应用动画GIF的唯一资源。

运行此代码时,会在SetPixelData方法上抛出异常,告诉我分配的缓冲区不足(即使我的Visual Studio环境是英文的,该消息也是我的操作系统语言,我认为这可能是相关的。)

我缩小了图像尺寸(源和输出)和帧数,但我仍然遇到此错误。 (我在同一个应用程序中操纵更大的图像和字节数组)。

知道这个内存问题可能来自哪里?我的StorageFile有问题吗?

2 个答案:

答案 0 :(得分:2)

当传递到SetPixelData的frameWidth \ Height与像素数据不匹配时,我看到了这个异常。

我最终得到了以下这个例子。当尺寸与pixelData不匹配时,我看到了你提到的异常。

我认为这在Windows 8.1中更稳定,因为它不会重现它。

BitmapDecoder decoder = await BitmapDecoder.CreateAsync(sourceStream);
                BitmapTransform transform = new BitmapTransform()
                {
                    ScaledHeight = 900,
                    ScaledWidth = 600
                };
                PixelDataProvider pixelData = await decoder.GetPixelDataAsync(BitmapPixelFormat.Rgba8,
                                                                              BitmapAlphaMode.Straight,
                                                                              transform,
                                                                              ExifOrientationMode.RespectExifOrientation,
                                                                              ColorManagementMode.DoNotColorManage);

                StorageFile destinationFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(Path.Combine(Database.rootMoviesFoldersPaths, movie.LocalId + ".jpg"));
                using (var destinationStream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, destinationStream);
                    encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, 600, 900, 96, 96, pixelData.DetachPixelData());
                    await encoder.FlushAsync();

                    movie.HasFolderImage = true;

                    return true;
                }
            }

答案 1 :(得分:-1)

将bitsize乘以bitdepth。