[英]How can I find Point of a Pixel Colour?
我正在尝试查找具有特定颜色的像素的坐标。
目前,我已经获得了图像的屏幕截图,但是我不知道从这里到哪里。
现有代码:
public Form1()
{
InitializeComponent();
}
private Bitmap Img;
private void button2_Click(object sender, EventArgs e)
{
var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap.
var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
// Take the screenshot from the upper left corner to the right bottom corner.
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0,
0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
// Save the screenshot to the specified path that the user has chosen.
bmpScreenshot.Save("Screenshot.png", ImageFormat.Png);
}
//Want to get the location of the colour: #AA00FF
private Point GetLocation()
{
Img = new Bitmap("Screenshot.png");
Point p = new Point(1, 1);
//This should read:
//p = Img.GetLocationOfPixel('FF00AA');
return p;
}
循环遍历:
private Point GetLocation()
{
Img = new Bitmap("Screenshot.png");
Color pixelColor;
for (int y = 0; y < Img.Height; y++)
{
for (int x = 0; x < Img.Width; x++)
{
pixelColor = Img.GetPixel(x, y);
if (pixelColor.R == 84 && pixelColor.G == 96 && pixelColor.B == 103)
{
MessageBox.Show($"Location is Y:{y.ToString()} X:{x.ToString()}");
Point p = new Point(x, y);
return p;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.