【问题标题】:Code only checks the brightness of the first pixel of the bitmap/image in C#代码仅检查 C# 中位图/图像的第一个像素的亮度
【发布时间】:2021-06-02 22:52:22
【问题描述】:

我目前正在使用 C# 开发图像到 ASCII 转换器,但遇到了问题。我希望程序遍历每个像素,获取亮度并添加正确的字符。但在我的代码中,它只检查图像的第一个像素,即像素 0-0,并成功将其打印在 .txt 文件中不显示任何错误消息。我尝试使用 StreamWriter,但结果相同,这意味着它可能与我的 for 循环有关。我做错了什么?

        Bitmap bm = new Bitmap(source);

        int width = bm.Width; // Image width
        int height = bm.Height; // Image height

        for (int i = 0; i < width; i++) 
        {
            int w = 0;
            w++;
            int h = 0;

            file = source + " ASCII.txt";

            if(i > width)
            {
                h++;
            }

            System.Drawing.Color pixelColor = bm.GetPixel(i, h);
            float brightness = pixelColor.GetBrightness();

            using (StreamWriter writer = File.CreateText(file))
            {
                writer.Close();  // Creating file
            }
                
            // ASCII print

            if (brightness < 0.14)
            {
                File.AppendAllText(file, "@");

                if (w > width)
                { 
                    File.AppendAllText(file, "\n");
                }
            }

            if (brightness > 0.14 && brightness < 0.28)
            {
               
                 File.AppendAllText(file, "%");

                 if (w > width)
                 {
                     File.AppendAllText(file, "\n");
                 }
                
            }

            if (brightness > 0.28 && brightness < 0.42)
            {
                
                File.AppendAllText(file, "#");

                if (w > width)
                {
                    File.AppendAllText(file, "\n");
                }
                
            }

            if (brightness > 0.42 && brightness < 0.56)
            {
                
                File.AppendAllText(file, "*");

                if (w > width)
                {
                    File.AppendAllText(file, "\n");
                }
                
            }

            if (brightness > 0.56 && brightness < 0.7)
            {
               
                File.AppendAllText(file, "+");

                if (w > width)
                {
                    File.AppendAllText(file, "\n");
                }
                
            }

            if (brightness > 0.7 && brightness < 0.84)
            {
                
                File.AppendAllText(file, "=");

                if (w > width)
                {
                    File.AppendAllText(file, "\n");
                }
                
            }

            if (brightness > 0.84 && brightness < 0.98)
            {
               
                File.AppendAllText(file, "-");

                if (w > width)
                {
                    File.AppendAllText(file, "\n");
                }
                
            }

            if (brightness > 0.98)
            {
                File.AppendAllText(file, ":");

                if (w > width)
                {
                        File.AppendAllText(file, "\n");
                }
            }
        }

【问题讨论】:

标签: c# text bitmap ascii


【解决方案1】:

不是 C# 专家。

但是在你的for循环条件下,i 宽度时你增加 h,这会被击中吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    相关资源
    最近更新 更多