2016-11-08 126 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