2017-03-25 12 views
0

ImageProcessorを使用して画像の解像度や画質を低下させていますが、イメージの結果のサイズそれは5メガバイト未満です。私は画像の寸法を3840-2160に設定しようとしましたが、より良いオプションを使用したいと思います。5 mbより大きいイメージがそのサイズよりも小さくなっていることを確認してください。C#

private static byte[] redimensionImage(ref byte[] photoBytes) 
    { 
     var byteCuantity = ConvertBytesToMegabytes(photoBytes.Count()); 
     ISupportedImageFormat format = new JpegFormat(); 

     using (MemoryStream inStream = new MemoryStream(photoBytes)) 
     { 
      using (MemoryStream outStream = new MemoryStream()) 
      { 
       // Initialize the ImageFactory using the overload to preserve EXIF metadata. 
       using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true)) 
       { 
        // Load, resize, set the format and quality and save an image. 
        using (var imageProcessor = imageFactory.Load(inStream)) 
        { 
         var originalHeight = imageProcessor.Image.Size.Height; 
         var originalWidth = imageProcessor.Image.Size.Width; 

         //calculate aspect ratio 
         var aspect = originalWidth/(float)originalHeight; 
         int newWidth, newHeight; 

         var dimenssionTooSmall = false; 
         if (originalWidth <= originalHeight && originalWidth < 100) 
         { 
          //calculate new dimensions based on aspect ratio 
          newHeight = (int)(100/aspect); 
          var resizeLayer = new ResizeLayer(new Size(100, newHeight), ResizeMode.Min); 
          imageProcessor.Resize(resizeLayer); 
          dimenssionTooSmall = true; 
         } 
         else if (originalHeight < originalWidth && originalHeight < 100) 
         { 
          //calculate new dimensions based on aspect ratio 
          newWidth = (int)(100/aspect); 
          var resizeLayer = new ResizeLayer(new Size(newWidth, 100), ResizeMode.Min); 
          imageProcessor.Resize(resizeLayer); 
          dimenssionTooSmall = true; 
         } 

         if (byteCuantity > 1 || dimenssionTooSmall) 
         { 
          //format.Quality = 6; 

          imageProcessor.Resize(new ResizeLayer(new Size(3840, 2160), ResizeMode.Min)); 

          imageProcessor.Format(format); 
          imageProcessor.Save(outStream); 
          return outStream.ToArray(); 
         } 
         else 
         { 
          return inStream.ToArray(); 
         } 
        } 
       } 


      } 
     } 
    } 

おかげに関して:

は、ここでは、私のコードです。

+0

変換後のバイト数を取得できますか?それはどれくらい大きいかを教えてくれるでしょう。 – mariocatch

+0

はい私は可能ですが、正しいサイズになるまで再処理を避けたいと思います。 –

答えて

0

ビットマップとして保存していない限り、残念ながら再処理なしでこれを行うことはできません。

イメージを保存すると、イメージを圧縮する多くのプロセスがあります(イメージを圧縮しないビットマップを除く)。プロセス自体を実際に通過することなく、ファイルサイズを予測することはできません。

大きなフォーマットのサンプルをさまざまなフォーマットにリサイズし、出力サイズを収集して将来の処理のためのおおまかな見積もりを与えることによって、ガイドラインとして機能する独自のルックアップテーブルを作成することができます。

関連する問題