调整位图大小后,新位图的结果是平滑bitmnap

时间:2011-01-10 09:32:18

标签: c#

我使用简单的重新大小方法将我的位图更改为新大小。 原始位图大小为320x240,我将大小改为两次

  • 至250x160
  • 对位图进行一些处理
  • 将其更改回320x240

我发现在将其更改回320x240之后,我发现位图很平滑,而不是我除外。

我怎样才能避免这种顺利出现?

调整大小方法:

private static Image resizeImage(Image imgToResize, Size size)
{

   int sourceWidth = imgToResize.Width;
   int sourceHeight = imgToResize.Height;

   float nPercent = 0;
   float nPercentW = 0;
   float nPercentH = 0;

   nPercentW = ((float)size.Width / (float)sourceWidth);
   nPercentH = ((float)size.Height / (float)sourceHeight);

   if (nPercentH < nPercentW)
      nPercent = nPercentH;
   else
      nPercent = nPercentW;

   int destWidth = (int)(sourceWidth * nPercent);
   int destHeight = (int)(sourceHeight * nPercent);

   Bitmap b = new Bitmap(destWidth, destHeight);
   Graphics g = Graphics.FromImage((Image)b);
   g.InterpolationMode = InterpolationMode.HighQualityBicubic;

   g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
   g.Dispose();

   return (Image)b;
}

2 个答案:

答案 0 :(得分:5)

可悲的是你不能。 当您将位图的大小调整为较小的大小时,信息将丢失。并且从小图像(具有较少信息)内插信息以创建具有原始尺寸的新的红外图像。正是这种插值使得到的图像具有平滑的外观。

为了避免这种情况,您唯一能做的就是找到一种方法来进行必要的处理,而无需在处理过程中调整图像大小。

答案 1 :(得分:2)

由于您正在使用HighQualityBicubic插值模式,因此将使用最高质量对图像进行预滤波和调整大小,从而产生“平滑效果”。

您可以尝试将InterpolationMode属性设置为NearestNeighbor以获得“更粗糙”的结果:

Bitmap b = new Bitmap(destWidth, destHeight);
using (Graphics g = Graphics.FromImage((Image) b)) {
    g.InterpolationMode = InterpolationMode.NearestNeighbor;
    g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
}