2013-12-17 84 views
0

我正在嘗試調整圖像大小的功能。調整大小後的圖像有白色邊框

public static Bitmap FixedSize(Bitmap imgPhoto, int Width, int Height, InterpolationMode im) 
{ 
if ((Width == 0) && (Height == 0)) 
    return imgPhoto; 

if ((Width < 0) || (Height < 0)) 
    return imgPhoto; 

int destWidth = Width; 
int destHeight = Height; 

int srcWidth = imgPhoto.Size.Width; 
int srcHeight = imgPhoto.Size.Height; 

if (Width == 0) 
    destWidth = (int)(((float)Height/(float)srcHeight) * (float)srcWidth); 
if (Height == 0) 
    destHeight = (int)(((float)Width/(float)srcWidth) * (float)srcHeight); 

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

Graphics grPhoto = Graphics.FromImage(bmPhoto); 
grPhoto.Clear(Color.White); 
grPhoto.InterpolationMode = im; 
grPhoto.DrawImage(imgPhoto,  
    new Rectangle(new Point(0, 0), new Size(destWidth, destHeight)), 
    new Rectangle(0, 0, srcWidth, srcHeight), 
    GraphicsUnit.Pixel);    
    grPhoto.Dispose(); 
return new Bitmap(bmPhoto); 
} 

當我調試代碼,所有的數字看起來不錯,但是當我將圖像保存它在左側和頂部邊框一條白線。任何想法什麼應該是錯的?我試圖搜索,我使用完全相同的代碼應該工作,但行仍然存在。
謝謝。

回答

0
public static Bitmap ResizeImage(Bitmap image, int percent) 
    { 
     try 
     { 
      int maxWidth = (int)(image.Width * (percent * .01)); 
      int maxHeight = (int)(image.Height * (percent * .01)); 
      Size size = GetSize(image, maxWidth, maxHeight); 
      Bitmap newImage = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb); 
      SetGraphics(image, size, newImage); 
      return newImage; 
     } 
     finally { } 
    } 

    public static void SetGraphics(Bitmap image, Size size, Bitmap newImage) 
    { 
     using (Graphics graphics = Graphics.FromImage(newImage)) 
     { 
      graphics.CompositingQuality = CompositingQuality.HighQuality; 
      graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 
      graphics.SmoothingMode = SmoothingMode.HighQuality; 
      graphics.DrawImage(image, 0, 0, size.Width, size.Height); 
     } 
    } 

標記此,如果它可以幫助你。

+0

雖然我不明白爲什麼,它的工作原理!謝謝 :-) –