Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检测某个像素的颜色?_C#_Xna - Fatal编程技术网

C# 如何检测某个像素的颜色?

C# 如何检测某个像素的颜色?,c#,xna,C#,Xna,我想做一个系统,可以检测选定像素的颜色 Color[] pixelData; pixelData= new Color[graphics.GraphicsDevice.Viewport.Width * (graphics.GraphicsDevice.Viewport.Height)]; Terrain.GetData(pixelData); 现在我想做这样的事情 if(pixelData[graphics.GraphicsDevice.Viewport.Wid

我想做一个系统,可以检测选定像素的颜色

    Color[] pixelData;
    pixelData= new Color[graphics.GraphicsDevice.Viewport.Width * (graphics.GraphicsDevice.Viewport.Height)];
    Terrain.GetData(pixelData);
现在我想做这样的事情

    if(pixelData[graphics.GraphicsDevice.Viewport.Width * playervec.Y + playervec.X].R == 255)
    {
       rest of the code
    }
但是我不知道我应该怎么做,它会选择一个特定的像素,这可能会对你有所帮助

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;

namespace FormTest
{
  public partial class Form1 : Form
   {
    [DllImport("user32.dll")]
    static extern bool GetCursorPos(ref Point lpPoint);

    [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
    public static extern int BitBlt(IntPtr hDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);

    public Form1()
    {
        InitializeComponent();
    }

    private void MouseMoveTimer_Tick(object sender, EventArgs e)
    {
        Point cursor = new Point();
        GetCursorPos(ref cursor);

        var c = GetColorAt(cursor);
        this.BackColor = c;

        if (c.R == c.G && c.G < 64 && c.B > 128)
        {
            MessageBox.Show("Blue");
        }
    }

    Bitmap screenPixel = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
    public Color GetColorAt(Point location)
    {
        using (Graphics gdest = Graphics.FromImage(screenPixel))
        {
            using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))
            {
                IntPtr hSrcDC = gsrc.GetHdc();
                IntPtr hDC = gdest.GetHdc();
                int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, (int)CopyPixelOperation.SourceCopy);
                gdest.ReleaseHdc();
                gsrc.ReleaseHdc();
            }
        }

        return screenPixel.GetPixel(0, 0);
    }
  }
}
资料来源:

 private void PollPixel(Point location, Color color)
{
  while(true)
 {
    var c = GetColorAt(location);

    if (c.R == color.R && c.G == color.G && c.B == color.B)
    {
        DoAction();
        return;
    }

    // By calling Thread.Sleep() without a parameter, we are signaling to the
    // operating system that we only want to sleep long enough for other
    // applications.  As soon as the other apps yield their CPU time, we will
    // regain control.
    Thread.Sleep()
   }
}