You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#实现读取160×160迷宫图像并打印所有像素黑白位置信息

Hey there! Let's get your maze pixel-reading code sorted out to meet your exact requirement. Your current code has a small hiccup since GetPixel() returns a Color object, not an integer—here's the fixed and expanded version that does exactly what you need:

Maze Pixel Reader: Detect Black/White and Print Coordinates

Full Working Code

using System;
using System.Drawing;

class MazePixelProcessor
{
    static void Main()
    {
        // Load the maze image (ensure the path is correct relative to your executable)
        using (Bitmap maze = new Bitmap("images/maze.png"))
        {
            // Optional: Validate the image is 160x160 as expected
            if (maze.Width != 160 || maze.Height != 160)
            {
                Console.WriteLine("⚠️ Warning: Loaded image isn't 160×160—double-check your file!");
            }

            // Iterate over every pixel in the image
            for (int x = 0; x < maze.Width; x++)
            {
                for (int y = 0; y < maze.Height; y++)
                {
                    Color pixel = maze.GetPixel(x, y);
                    string colorTag;

                    // Check if pixel is pure black or white
                    if (pixel.R == 0 && pixel.G == 0 && pixel.B == 0)
                    {
                        colorTag = "b";
                    }
                    else if (pixel.R == 255 && pixel.G == 255 && pixel.B == 255)
                    {
                        colorTag = "w";
                    }
                    else
                    {
                        // Handle non-black/white pixels (adjust if your maze uses grays)
                        colorTag = "?";
                    }

                    // Print in the requested format: X, Y: tag
                    Console.WriteLine($"{x}, {y}: {colorTag}");
                }
            }
        }
    }
}

Important Details to Note

  • Resource Management: We use a using block for the Bitmap to automatically clean up the image resource after we're done—this prevents memory leaks.
  • Dimension Check: The optional check alerts you if the loaded image doesn't match your 160×160 requirement, which helps catch file path or image size mistakes early.
  • Color Detection: The code checks for pure black (RGB 0,0,0) and pure white (RGB 255,255,255). If your maze uses near-black or near-white pixels (like dark grays), you could modify this to check if the average RGB value is below a threshold (e.g., < 30 for black) or above (e.g., > 225 for white).
  • Skip the Array (If You Don't Need It): Your original code creates a 2D array to store pixel values, but if you only need to print the results, you can skip that entirely (as shown here) to save memory. If you do need to store the colors later, just swap the int[][] for a Color[][] array.

内容的提问来源于stack exchange,提问作者Sport

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:10:52