2016-07-27 15 views
3

私のUWPアプリケーションでイメージのサイズを変更しようとしています。ほとんどの場合、追加されたコードは機能しますが、await encoder.FlushAsync();ArgumentExceptionをスローすることがあります。BitmapEncoder flush throw Args例外

私はMSDN( https://msdn.microsoft.com/en-us/library/windows/apps/windows.graphics.imaging.bitmapencoder.bitmaptransform.aspx)にオーバー向かってきたし、彼らは(「備考」で)教えて

あなたはBitmapTransform部材を用いインデックス付きのピクセル形式で保存された画像を拡大しようとすると、FlushAsyncが失敗しましたHRESULT WINCODEC_ERR_INVALIDPARAMETERを指定します。代わりに、GetPixelDataAsyncを使用してスケーリングされたピクセルデータを取得し、次にSetPixelDataを使用してエンコーダで設定する必要があります。

私はそれをやろうとしましたが、2つのコメント行(繰り返しのため何とか間違っています)を見てください。 2行目(SetPixelData)にはエンコーダがbuffer allocated not sufficient例外で私に報酬を与えます。

この問題の原因となる画像例:http://www.spiegel.de/images/image-1028227-hppano-lqbn.jpg。ユニットテストはこちらhttps://github.com/famoser/OfflineMedia/blob/master/Famoser.OfflineMedia.UnitTests/Presentation/ImageResizeTest.cs

ArgumentExceptionを避けるにはどうすればよいですか?画像が「インデックス付きのピクセル形式」であることを確認するにはどうすればよいのですか。この形式のサイズを変更するにはどうすればよいですか?

答えて

1

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;     
    } 
} 
+0

おかげでたくさん!私がそれを試すことができるとすぐに、私は試してみて、あなたの答えを受け入れるでしょう。 –

+0

残念ながら、これは問題を解決しませんでした。 'GetPixelDataAsync'と' SetPixelData'は正常に動作するようですが、 'pixelencoder.FlushAsync();'は 'ArgumentException'をスローします。この問題が私の質問につながる画像へのリンクを追加しました。 –

+1

@FlorianMoser、遅く応答して申し訳ありません、私はあなたの提供された画像でコードの最初のブロックをテストしますが、私は問題を再現できませんか? 2番目のリンクから、そのイメージをダウンロードしたことが判明しました。これはダウンロードで可能ですか?私は画像をダウンロードしてからそれを変換するテストはしませんでした。私はあなたの画像を画像のlibに入れ、FileOpenPickerを使ってこの画像を選択しました。 –

関連する問題