我在 c# 中遇到问题,当我尝试用黑色填充整个 8x8px .bmp 文件时,它什么也没做,也没有错误

I'm having a problem in c# where when i try to fill an entire 8x8px .bmp file with black it doesn't do anything and there are no errors

我在 c# 中遇到问题,当我尝试用黑色填充整个 8x8px .bmp 文件时,它没有执行任何操作并且没有错误,这是我的代码:

using System;
using System.Windows;
using System.Drawing;

namespace Data_Block_Test
{
    class Program
    {
        static string GetBytesFromBinaryString(String str, String output)
        {
            string result = "";
            string s = str.Replace(" ", String.Empty); ;

            while (s.Length > 0)
            {
                var first8 = s.Substring(0, 8);
                s = s.Substring(8);
                var number = Convert.ToInt32(first8, 2);
                result += (char)number;
            }

            return result;
        }

        static void Main(string[] args)
        {
            string output1 = "";

            Console.WriteLine(GetBytesFromBinaryString("01110011 01110101 01110011", output1));

            Bitmap img = new Bitmap("image.png");

            for (int x = 0; x < img.Width; x++)
            {
                for (int y = 0; y < img.Height; y++)
                {
                    Console.WriteLine("before: x = {0}, y = {1}, argb = {2}", x, y, img.GetPixel(x, y));
                    img.SetPixel(x, y, System.Drawing.Color.Black);
                    Console.WriteLine("after: x = {0}, y = {1}, argb = {2}", x, y, img.GetPixel(x, y));
                }
            }

            Console.WriteLine("Press enter to exit");
            Console.ReadLine();
        }
    }
}

我正在逐像素填充它以进行实验 编辑:也请忽略 GetBytesFromBinaryString() 和所有对它的引用,因为它与我正在尝试做的事情无关

我已经查看了代码,您需要在“FOR”语句之后包含 SAVE 方法。

保存方式:

img.Save("out.png");