C#Bitmap.GetPixel获得不正确的值

时间:2019-01-28 14:10:58

标签: c# bitmap

我写了一个类来模拟较低分辨率的位图,因为SetResolution方法似乎根本不适合我。

$(document).ready(function(){
 $("#book").width('40%');
//or
 $("#book").css('width','40%');
});

当我在下面的代码中使用该类时:

class LowResBitmap : IDisposable
{
    public LowResBitmap(int Height, int Width, int scale)
    {
        Internal = new Bitmap(Height, Width);
        Scale = scale;

        ScaledHeight = Height / scale;
        ScaledWidth = Width / scale;
    }

    public LowResBitmap(Bitmap Source, int Scale)
    {
        Internal = Source;
        this.Scale = Scale;
    }

    public Bitmap Internal { get; private set; }

    public int Height => Internal.Height;

    public int Width => Internal.Width;

    public int ScaledHeight { get; set; }

    public int ScaledWidth { get; set; }

    public int Scale { get; }

    public void SetPixel(int x, int y, Color color)
    {
        Internal.SetPixel(x, y, color);

        for(int dx = 1; dx < Scale; dx++)
        {
            if (x + dx >= Internal.Width) break;

            for(int dy = 1; dy < Scale; dy++)
            {
                if (y + dy >= Internal.Height) break;
                Internal.SetPixel(x + dx, y + dy, color);
            }
        }
    }

    public Color GetPixel(int x, int y) => GetPixel(x, y);

    public IEnumerable<(int x, int y)> GetPixels()
    {
        for(int x = 0; x < Internal.Width; x += Scale)
        {
            for(int y = 0; y < Internal.Height; y += Scale)
            {
                yield return (x, y);
            }
        }
    }

    public void Dispose() => Internal.Dispose();

    public LowResBitmap LowResFromBitmap(Bitmap Source, int Scale) => new LowResBitmap(Source, Scale);

    public LowResBitmap Clone() => LowResFromBitmap((Bitmap)Internal.Clone(), Scale);

    public static implicit operator Bitmap(LowResBitmap lhs) => lhs.Internal;

    public static implicit operator LowResBitmap(Bitmap lhs) => new LowResBitmap(lhs, 2);
}

通过右行代码,内部位图的像素应该已经设置为黑色,但是当我从内部位图手动获取像素时,只有像素的第一个像素返回黑色(A = 255,R = 0,G = 0,B = 0),而另一个像素返回白色(A = 255,R = 255,G = 255,B = 255)。

此外,当在PictureBox中显示时,LowResBitMap的行为正确-将单个像素设置为黑色会设置64个基础像素黑色。

我做错了什么吗?如何纠正这种情况?

0 个答案:

没有答案