C# &引用;值不在预期范围内;尝试从图像中读取颜色

C# &引用;值不在预期范围内;尝试从图像中读取颜色,c#,wpf,image,image-processing,C#,Wpf,Image,Image Processing,我有这样的图像: 详情如下: 我尝试使用以下代码在此图像中查找任何指定的颜色: private void UpdateCursorEllipse(Color searchColor) { // Scan the canvas image for a color which matches the search color CroppedBitmap cb; Color tempColor = new Color(); byte[] pixels = new byte[4];

我有这样的图像:

详情如下:

我尝试使用以下代码在此图像中查找任何指定的颜色:

private void UpdateCursorEllipse(Color searchColor)
{
  // Scan the canvas image for a color which matches the search color
  CroppedBitmap cb;
  Color tempColor = new Color();
  byte[] pixels = new byte[4];
  int searchY = 0;
  int searchX = 0;
  searchColor.A = 255;
  for (searchY = 0; searchY <= canvasImage.Width - 1; searchY++)
  {
    for (searchX = 0; searchX <= canvasImage.Height - 1; searchX++)
    {
      cb = new CroppedBitmap(ColorImage.Source as BitmapSource, new Int32Rect(searchX, searchY, 1, 1));
      cb.CopyPixels(pixels, 4, 0);
      tempColor = Color.FromArgb(255, pixels[2], pixels[1], pixels[0]);
      if (tempColor == searchColor) break;
    }
    if (tempColor == searchColor) break;
  }
private void updatecursorrelipse(颜色搜索颜色)
{
//扫描画布图像以查找与搜索颜色匹配的颜色
裁剪位图cb;
Color tempColor=新颜色();
字节[]像素=新字节[4];
int searchY=0;
int searchX=0;
searchColor.A=255;

对于(searchY=0;searchY使用以下方法获取位图源中像素的
颜色:

private static Color GetPixelColor(BitmapSource bitmap, int x, int y)
{
    if (bitmap.Format == PixelFormats.Bgr24 ||
        bitmap.Format == PixelFormats.Bgr32)
    {
        var pixel = new byte[bitmap.Format.BitsPerPixel / 8];
        bitmap.CopyPixels(new Int32Rect(x, y, 1, 1), pixel, pixel.Length, 0);
        return Color.FromRgb(pixel[2], pixel[1], pixel[0]);
    }

    if (bitmap.Format == PixelFormats.Bgra32)
    {
        var pixel = new byte[4];
        bitmap.CopyPixels(new Int32Rect(x, y, 1, 1), pixel, pixel.Length, 0);
        return Color.FromArgb(pixel[3], pixel[2], pixel[1], pixel[0]);
    }

    if (bitmap.Format == PixelFormats.Rgb24)
    {
        var pixel = new byte[bitmap.Format.BitsPerPixel / 8];
        bitmap.CopyPixels(new Int32Rect(x, y, 1, 1), pixel, pixel.Length, 0);
        return Color.FromRgb(pixel[0], pixel[1], pixel[2]);
    }

    // other formats

    return new Color();
}
然后像这样循环所有像素以找到特定颜色:

private static bool FindColor(BitmapSource bitmap, Color color, out int x, out int y)
{
    x = 0;

    for (y = 0; y < bitmap.PixelHeight; y++)
    {
        for (x = 0; x < bitmap.PixelWidth; x++)
        {
            if (GetPixelColor(bitmap, x, y) == color)
            {
                return true;
            }
        }
    }

    return false;
}
private static bool FindColor(位图源位图、颜色、out int x、out int y)
{
x=0;
对于(y=0;y
使用以下方法获取位图源中像素的
颜色:

private static Color GetPixelColor(BitmapSource bitmap, int x, int y)
{
    if (bitmap.Format == PixelFormats.Bgr24 ||
        bitmap.Format == PixelFormats.Bgr32)
    {
        var pixel = new byte[bitmap.Format.BitsPerPixel / 8];
        bitmap.CopyPixels(new Int32Rect(x, y, 1, 1), pixel, pixel.Length, 0);
        return Color.FromRgb(pixel[2], pixel[1], pixel[0]);
    }

    if (bitmap.Format == PixelFormats.Bgra32)
    {
        var pixel = new byte[4];
        bitmap.CopyPixels(new Int32Rect(x, y, 1, 1), pixel, pixel.Length, 0);
        return Color.FromArgb(pixel[3], pixel[2], pixel[1], pixel[0]);
    }

    if (bitmap.Format == PixelFormats.Rgb24)
    {
        var pixel = new byte[bitmap.Format.BitsPerPixel / 8];
        bitmap.CopyPixels(new Int32Rect(x, y, 1, 1), pixel, pixel.Length, 0);
        return Color.FromRgb(pixel[0], pixel[1], pixel[2]);
    }

    // other formats

    return new Color();
}
然后像这样循环所有像素以找到特定颜色:

private static bool FindColor(BitmapSource bitmap, Color color, out int x, out int y)
{
    x = 0;

    for (y = 0; y < bitmap.PixelHeight; y++)
    {
        for (x = 0; x < bitmap.PixelWidth; x++)
        {
            if (GetPixelColor(bitmap, x, y) == color)
            {
                return true;
            }
        }
    }

    return false;
}
private static bool FindColor(位图源位图、颜色、out int x、out int y)
{
x=0;
对于(y=0;y
哪个值?您当然可以跟踪这个值。任何值,我都尝试了红色(#ff0000)。请验证像素[2]、像素[1]、像素[0]中的值,它们不应该大于255。这段代码实际上没有实现任何功能,因此,无论是否找到,它的功能都是一样的……你确定canvasImage与图片大小相同吗?@BugFinder是的,你可以看到图像细节,画布和图像尺寸是一样的。如果找到颜色,它会在粒子上设置椭圆位置在画布上单击一个点,如果没有,则将椭圆设置为左上角,我还没有粘贴该代码。对于哪个值?您当然可以跟踪该值。对于任何值,我尝试了红色(#ff0000)。请验证像素[2]、像素[1]、像素[0]中的值,它们不应该大于255。这段代码实际上没有实现任何功能,因此,无论是否找到,它的功能都是一样的……你确定canvasImage与图片大小相同吗?@BugFinder是的,你可以看到图像细节,画布和图像尺寸是一样的。如果找到颜色,它会在粒子上设置椭圆位置我在画布上画了一个点,如果没有,它会将椭圆设置在左上角,我还没有粘贴代码。