2013-04-17 52 views
1

在我的程序中,用戶可以上傳特定尺寸的圖像,然後我調整圖像大小。如果圖像很大,它效果很好,但是如果圖像很小,我嘗試調整大圖像變得模糊(基本上它只是縮放而不是調整大小)...有人可以幫我怎麼做。謝謝!無法將小圖像調整爲較大圖像

這裏是我的程序

public static Image ScaleBySize(Image imgPhoto, int size) 
     { 
      int logoSize = size; 

      float sourceWidth = imgPhoto.Width; 
      float sourceHeight = imgPhoto.Height; 
      float destHeight = 0; 
      float destWidth = 0; 
      int sourceX = 0; 
      int sourceY = 0; 
      int destX = 0; 
      int destY = 0; 




      if (sourceWidth > size || sourceWidth < size) { destWidth = size; } 
      if (sourceHeight > size || sourceHeight < size) { destHeight = size; } 


      Bitmap bmPhoto = new Bitmap((int)destWidth, (int)destHeight,PixelFormat.Format32bppPArgb); 
      bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); 

      Graphics grPhoto = Graphics.FromImage(bmPhoto); 
      grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; 
      grPhoto.DrawImage(imgPhoto,new Rectangle(destX, destY, (int)destWidth, (int)destHeight),new Rectangle(sourceX, sourceY, (int)sourceWidth, (int)sourceHeight),GraphicsUnit.Pixel); 
      grPhoto.Dispose(); 

      return bmPhoto; 
     } 
+0

你最好的選擇是讓圖像尺寸小於你所需的尺寸。只調整大小以消除失真 – Tommy

回答

2

當然你大的圖像會模糊不清,因爲在小的圖像沒有細節。該程序只是「拉伸」每個像素以填充更大的區域。

+0

有沒有辦法做到這一點??? – user207888

+0

@ user207888 - 不,沒有。在給定尺寸的圖像中只有很多信息。取一個50px x 50px的圖像並試圖使其填充200px x 200px會導致它扭曲,因爲每個像素現在基本上被製成16個像素 - 主要失真。只有在CSI中,你可以炸開一個顆粒狀圖像,並使其與原始圖像一樣清晰:) – Tommy

+0

哦,K,可能是我然後只是居中小圖像和調整大圖像..謝謝! – user207888