调整图像大小,覆盖像素的问题

时间:2018-05-15 08:01:56

标签: c# image image-processing

我正在尝试编写一个可以使用比例因子(如1.5或0.5)上下缩放图像的功能。当我向下缩小时,当我向上缩放时,屏幕上的图像将显示正确,图像上会出现某种屏幕覆盖。见下图 Scale up

Scale down

这是我写的代码:

public static void ResizeImage(Bitmap inputImage, double scale)
{
    int maxWidth = (int) (scale * inputImage.Width);
    int maxHeight = (int) (scale * inputImage.Height);


    Bitmap scaledImage = new Bitmap(maxWidth, maxHeight); 
    for(int x = 0; x < inputImage.Width; x++)
    {
        for(int y = 0; y < inputImage.Height; y++)
        {
            //Gets current Pixel
            Color pixel = inputImage.GetPixel(x, y);

            //Calucalte new position Pixel
            int newPixelX = (int) (Math.Floor(x * scale));
            int newPixelY = (int) (Math.Floor(y * scale));

            //Sets pixel in new image
            scaledImage.SetPixel(newPixelX, newPixelY, pixel); 
        }
    }
    DrawImage(Bitmap2colorm(scaledImage)); 
}

有没有人知道这个问题的解决方案,有人可以解释为什么会这样吗?

0 个答案:

没有答案
相关问题