2016-11-08 36 views
-1

以下代碼行爲非常奇怪。它在圖像底部添加了一些額外的間距,我看不出爲什麼。碼結果:c#圖片大小調整添加額外的像素

enter image description here

這是我一起工作的代碼:

public static Image ReDraw(this Image main, int w, int h, 
     CompositingQuality quality = CompositingQuality.Default, //linear? 
     SmoothingMode smoothing_mode = SmoothingMode.None, 
     InterpolationMode ip_mode = InterpolationMode.NearestNeighbor) 
    { 
     //size 
     double dbl = (double)main.Width/(double)main.Height; 

     //preserve size ratio 
     if ((int)((double)h * dbl) <= w) 
      w = (int)((double)h * dbl); 
     else 
      h = (int)((double)w/dbl); 

     //draw 
     Image newImage = new System.Drawing.Bitmap(w, h); 
     Graphics thumbGraph = Graphics.FromImage(newImage); 
     thumbGraph.CompositingQuality = quality; 
     thumbGraph.SmoothingMode = smoothing_mode; 
     thumbGraph.InterpolationMode = ip_mode; 
     thumbGraph.Clear(Color.Transparent); 
     thumbGraph.DrawImage(main, 0, 0, w, h); 

     thumbGraph.DrawImage(main, 
     new System.Drawing.Rectangle(0, 0, w, h), 
     new System.Drawing.Rectangle(0, 0, main.Width, main.Height), 
     System.Drawing.GraphicsUnit.Pixel); 

     return newImage; 
    } 
+0

嘗試一些現有的庫? –

+0

不是一種選擇,這似乎太簡單了,無法開始向便攜式應用程序添加庫 –

+0

您是**重新創建輪子**? –

回答

-1

thumbGraph.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

+1

如果你想添加這個作爲答案,解釋爲什麼這應該工作,像這樣拋出的信息沒有用的SO,這就是爲什麼有人低估你。 – Gusman