2016-07-29 25 views
0

Im次のコード(Example #2 hereから適合)を使用して画像をサイズ変更して縦横比を維持します。ただし、サイズ変更された画像の周りに白い枠線が表示され続けます。サイズ変更画像アスペクト比を保持する白線ボーダー

Bitmap ResizekeepAspectRatio(Bitmap imgPhoto, int Width, int Height) 
      { 
       int sourceWidth = imgPhoto.Width; 
       int sourceHeight = imgPhoto.Height; 
       int sourceX = 0; 
       int sourceY = 0; 
       int destX = 0; 
       int destY = 0; 

       float nPercent = 0; 
       float nPercentW = 0; 
       float nPercentH = 0; 

       nPercentW = ((float)Width/(float)sourceWidth); 
       nPercentH = ((float)Height/(float)sourceHeight); 
       if (nPercentH < nPercentW) 
       { 
        nPercent = nPercentH; 
        destX = System.Convert.ToInt16((Width - 
            (sourceWidth * nPercent))/2); 
       } 
       else 
       { 
        nPercent = nPercentW; 
        destY = System.Convert.ToInt16((Height - 
            (sourceHeight * nPercent))/2); 
       } 

       int destWidth = (int)(sourceWidth * nPercent); 
       int destHeight = (int)(sourceHeight * nPercent); 

       Bitmap bmPhoto = new Bitmap(Width, Height, 
            PixelFormat.Format24bppRgb); 
       bmPhoto.SetResolution(imgPhoto.HorizontalResolution, 
           imgPhoto.VerticalResolution); 

       Graphics grPhoto = Graphics.FromImage(bmPhoto); 
       grPhoto.Clear(Color.White); 
       grPhoto.InterpolationMode = 
         InterpolationMode.HighQualityBicubic; 

       grPhoto.DrawImage(imgPhoto, 
        new Rectangle(destX, destY, destWidth, destHeight), 
        new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), 
        GraphicsUnit.Pixel); 

       grPhoto.Dispose(); 
       return bmPhoto; 
      } 

UPDATE:

サンプルリサイズされた画像

enter image description here

はコーナーではホワイトボーダー

を明らかにズームあなたは、その余分なスペースを必要としないので、もしあなたが画像の周りに余分なスペースを持つことになりますほとんどの時間をアスペクト比を維持したいので

Bitmap bmPhoto = new Bitmap(Width, Height, 
           PixelFormat.Format24bppRgb); 
To 
Bitmap bmPhoto = new Bitmap(destWidth, destHeight, 
          PixelFormat.Format24bppRgb); 

ラインを変更するには、

+0

'float'は、おそらく計算が1つのピクセル短いことが原因となっています。 'float'を' double'に変更してください。 –

+0

@BarmakShemiraniありがとう...実際には、以下の答えが問題を解決しました。 – techno

答えて

1

てみてください新しい宛先サイズに新しい画像のサイズがフィットします

編集:

Try to comment out the line grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic 
+0

ありがとうございます。余分なスペースについては言及していません...私は、イメージ内の白い線の境界について話しています...更新を見たことがありますか? – techno

+0

grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;行をコメントアウトしてみてください。 –

+0

ありがとうございました...問題は何でしたか?このラインをコメントアウトすると出力画像の品質が低下するのですか? – techno

関連する問題