2番目の行(私はSetPixelDataを試しています)では、エンコーダは十分な例外が割り当てられていないバッファで私に報酬を与えます。
ときSetPixelData
、ピクセルデータがGetPixelDataAsync
からそれと一致しないためです。あなたは、このようなサンプルコードのためにすることができます
if (file != null)
{
BitmapImage bmp = new BitmapImage();
using(var imageStream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(imageStream);
InMemoryRandomAccessStream pixelras = new InMemoryRandomAccessStream();
BitmapEncoder pixelencoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, pixelras);
BitmapTransform transform = new BitmapTransform();
transform.InterpolationMode = BitmapInterpolationMode.Fant;
transform.ScaledHeight = 400;
transform.ScaledWidth = 400;
var provider = await decoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
transform,
ExifOrientationMode.RespectExifOrientation,
ColorManagementMode.DoNotColorManage);
var pixels = provider.DetachPixelData();
pixelencoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, 400,
400, decoder.DpiX, decoder.DpiY, pixels);
try
{
await pixelencoder.FlushAsync();
}
catch(Exception ex)
{
}
bmp.SetSource(pixelras);
img.Source = bmp;
}
}
にはどうすればイメージは、「インデックス付きのピクセル形式」である知っています、そしてどのように私もこの形式のサイズを変更することができますか?
それは
を言われているので、あなたがBitmapTransform部材を用いインデックス付きのピクセル形式で保存された画像を拡大しようとすると、私は、インデックス付きのピクセル形式の画像を検出するために、任意の効果的な方法を見つけることが、できませんでしたFlushAsyncは、HRESULT WINCODEC_ERR_INVALIDPARAMETERで失敗します。代わりに、GetPixelDataAsyncを使用してスケーリングされたピクセルデータを取得し、次にSetPixelDataを使用してエンコーダで設定する必要があります。
例えば、例外をキャッチし、再びSetPixelData
を使用使用する方法である:
if (file != null)
{
BitmapImage bmp = new BitmapImage();
using(var imageStream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(imageStream);
InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(ras, decoder);
encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant;
encoder.BitmapTransform.ScaledHeight = 400;
encoder.BitmapTransform.ScaledWidth = 400;
try
{
await encoder.FlushAsync();
bmp.SetSource(ras);
}
catch (Exception ex)
{
if (ex.HResult.ToString() == "WINCODEC_ERR_INVALIDPARAMETER")
{
InMemoryRandomAccessStream pixelras = new InMemoryRandomAccessStream();
BitmapEncoder pixelencoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, pixelras)
BitmapTransform transform = new BitmapTransform();
transform.InterpolationMode = BitmapInterpolationMode.Fant;
transform.ScaledHeight = 400;
transform.ScaledWidth = 400;
var provider = await decoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
transform,
ExifOrientationMode.RespectExifOrientation,
ColorManagementMode.DoNotColorManage);
var pixels = provider.DetachPixelData();
pixelencoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, 400,
400, decoder.DpiX, decoder.DpiY, pixels);
try
{
await pixelencoder.FlushAsync();
bmp.SetSource(pixelras);
}
catch
{
}
}
}
img.Source = bmp;
}
}
おかげでたくさん!私がそれを試すことができるとすぐに、私は試してみて、あなたの答えを受け入れるでしょう。 –
残念ながら、これは問題を解決しませんでした。 'GetPixelDataAsync'と' SetPixelData'は正常に動作するようですが、 'pixelencoder.FlushAsync();'は 'ArgumentException'をスローします。この問題が私の質問につながる画像へのリンクを追加しました。 –
@FlorianMoser、遅く応答して申し訳ありません、私はあなたの提供された画像でコードの最初のブロックをテストしますが、私は問題を再現できませんか? 2番目のリンクから、そのイメージをダウンロードしたことが判明しました。これはダウンロードで可能ですか?私は画像をダウンロードしてからそれを変換するテストはしませんでした。私はあなたの画像を画像のlibに入れ、FileOpenPickerを使ってこの画像を選択しました。 –