2013-04-17 51 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