2010-12-17 7 views
2

私は、次のコードを持っている:それはすべてのアイデア、なぜtbmp.EndInit();WPFでビットマップを拡大/縮小しようとすると、TransformedBitmap.EndInitでOverflowExceptionが発生しますか?

OverflowExceptionをスロー

private void Process(string path) 
    { 
     using (FileStream fs = File.OpenRead(path)) 
     { 
      JpegBitmapDecoder decoder = new JpegBitmapDecoder(fs,BitmapCreateOptions.PreservePixelFormat,BitmapCacheOption.Default); 
      BitmapSource bmps = decoder.Frames.First(); 
      double targetScale = 800.0/600.0; 
      double scaleX = bmps.PixelWidth*targetScale; 
      double scaleY = bmps.PixelHeight*targetScale; 
      TransformedBitmap tbmp = new TransformedBitmap(); 
      tbmp.BeginInit(); 
      tbmp.Source = bmps; 
      tbmp.Transform = new ScaleTransform(scaleX, scaleY); 
      tbmp.EndInit(); 
      JpegBitmapEncoder encoder = new JpegBitmapEncoder(); 
      encoder.Frames.Add(BitmapFrame.Create(tbmp)); 
      using (FileStream fs2 = File.OpenWrite(path+".jpg")) 
      { 
       Debug.WriteLine(path+".jpg"); 
       encoder.Save(fs2); 
      } 
     } 
    } 

を?

更新:このメソッドがParallelQueryを通じて呼び出されることに言及する価値はあります。しかし、それは別のスレッドにある可能性のあるものに依存しません。

+1

_scaleX_と_scaleY_は**巨大**となります。それは多分です。 –

答えて

2

あなたはすでに必要なスケーリングを計算しています(800/600)。画像のサイズを掛けないでください。修正:

tbmp.Transform = new ScaleTransform(targetScale, targetScale); 
+0

それは機能していなかった、ちょうどそれらを大きくしました。 –

+0

質問が変更されました。この回答は、例外がある理由、つまり元の質問を示しています。 –

+0

ああ、私の悪い、私は時々そのトラックを緩めているようだ、私はそれを元に戻して新しいトピックを開く:) –

1

私の推測では、あなたのスケールが大きいからです。たとえば、元の画像が1600x1200 ... 2,133.33333x1600倍に拡大され、最終的な画像サイズは3,413,333 x 1,920,000です。これはかなり大きな画像です!私はあなたが望んでいた疑いがある

:すべての後に

double scaleX = targetScale/bmps.PixelWidth; 
double scaleY = targetScale/bmps.PixelHeight; 

は、私は元の画像が大きければ、あなたはストレッチしたいと仮定すると、少ない以上ではありません。

+0

私はそれをそれに変更しましたが、今では1 x 1と黒(1黒のピクセル)の画像を取得します。 –

関連する問題