开发者

Delegate returns right color, but still wrong

开发者 https://www.devze.com 2023-02-04 18:23 出处:网络
I have a delegate private delegate Color ColorDel(int x, int y); which returns a color at a point, to be used with Bitmap.GetPixel(x,y)

I have a delegate

private delegate Color ColorDel(int x, int y);

which returns a color at a point, to be used with Bitmap.GetPixel(x,y)

Even though i put the color into the bitmap directly before, as a Color.Red, the return co开发者_如何转开发lor is the same to all the ARGB but not on the name, which is ffff0000 instead of Red as the Color.Red actually is. ToKnownColor doesnt do the trick either.

Any input on the matter?

Edit, code:

class ColorDelegateTest
    {
        private delegate Color ColorDel(int x, int y);

        private static bool FoundColor(int x, int y, Color color, ColorDel dlgt)
        {
            var theColor = dlgt.Invoke(x, y);
            //theColor = "{Name=ffff0000, ARGB=(255, 255, 0, 0)}"
            //Color.Red = "{Name=Red, ARGB=(255, 255, 0, 0)}"
            var r = dlgt.Invoke(x, y) == color; //False
            var t = dlgt.Invoke(x, y) == Color.FromArgb(255, 255, 0, 0); //True
            var f = dlgt.Invoke(x, y) == Color.Red; //False
            if (r || t || f)
                return true;
            return false;
        }
        private static void ItterateColors()
        {
            int xMax = 300;
            int yMax = 300;
            Bitmap bmp = new Bitmap(xMax, yMax);

            ColorDel colorDelegate = new ColorDel(bmp.GetPixel);

            for (int x = 0; x < xMax; x++)
                for (int y = 0; y < yMax; y++)
                {
                    bmp.SetPixel(x, y, Color.Red);
                    FoundColor(x, y, Color.Red, colorDelegate);
                }
        }
    }

Erik


Color.Red includes a bit indicating that it is a known color, while Color.FromArgb(255, 0, 0) does't so they don't compare as equal. One way to do the comparison is to just compare with .ToArgb():

dlgt.Invoke(x, y).ToArgb() == Color.Red.ToArgb()


try using Color.FromName("Red") instead of Color.Red

P.S. Would have helped a lot to answer if more detailed question was there

0

精彩评论

暂无评论...
验证码 换一张
取 消