如何从具有定义的最大颜色深度的屏幕中获取像素颜色?

How to get a pixel color from a screen with the defined max color depth?

Clickermann 有函数 ColorMode(n)。

功能: 该过程通过对其应用过滤器来修改当前屏幕截图,减少调色板中的颜色数量

我想用 C# 做同样的事情。

我需要根据最大颜色深度从屏幕上获取像素颜色,因为它必须处理具有很多相似颜色的区域(例如渐变)

// Creates an empty bitmap with the size of the current screen
Bitmap screenshotBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
// Creates a new graphics object that can capture the screen
Graphics graphics = Graphics.FromImage(screenshotBitmap);
// Draw a screenshot on the bitmap
graphics.CopyFromScreen(0, 0, 0, 0, screenshotBitmap.Size);
// Clone bitmap and make it 4 bit
Rectangle screenshotBitmapRectangle = new Rectangle(0, 0, screenshotBitmap.Width, screenshotBitmap.Height);
Bitmap screenshotBitmap4BitPerPixel = screenshotBitmap.Clone(screenshotBitmapRectangle, PixelFormat.Format4bppIndexed);

用位图做任何你想做的事

screenshotBitmap4BitPerPixel.Save(@"C:\Users\FreePhoenix\Desktop\Bitmap.bmp");