C# 最终PNG中某些像素的WBMP到PNG错误

C# 最终PNG中某些像素的WBMP到PNG错误,c#,png,bmp,C#,Png,Bmp,我的代码有问题。读取wbmp数据后,获取宽度和高度,根据转换为二进制的数据设置像素。但我的最终png文件包含一张看起来像假想图片的图片,但有些像素位置不正确。(请假定宽度始终是8的倍数,即精确的x字节,因此可以忽略填充零) 我猜我的代码错了,或者在读取/转换数据时发生了错误 我花了几个小时去寻找原因,但是找不到。你们可以帮我 以下是我的代码: using System; using System.Drawing; using System.IO; using System.Collections

我的代码有问题。读取wbmp数据后,获取宽度和高度,根据转换为二进制的数据设置像素。但我的最终png文件包含一张看起来像假想图片的图片,但有些像素位置不正确。(请假定宽度始终是8的倍数,即精确的x字节,因此可以忽略填充零)

我猜我的代码错了,或者在读取/转换数据时发生了错误

我花了几个小时去寻找原因,但是找不到。你们可以帮我

以下是我的代码:

using System;
using System.Drawing;
using System.IO;
using System.Collections;

class Program
{
    static void Main(string[] args)
    {

    BinaryReader binReader = new BinaryReader(File.Open(args[0], FileMode.Open));
    byte[] aa = new byte[1000];

    int width, height;

    try
    {
        //read the bytes until the end of stream
        for (int i = 0; i<aa.Length; i++)
        {
            aa[i] = binReader.ReadByte();
        }

    }
    catch (EndOfStreamException)
    {
        Console.WriteLine("End of the Stream");
    }
    //once end is reached,
    finally
    {
        width = aa[2];
        height = aa[3];

        Console.Write("Width: " + width);
        Console.Write(" Height: " + height + "\n");

        //Conversion
        BitArray bits = new BitArray(aa); //convert bytes array to bit array
        //Conversion Ends

        Bitmap image = new Bitmap(width, height);

        int index = 32;

        for (int r = 0; r < height; r++)
        {
            for (int c = 0; c < width; c++)
            {
                if (bits[index] == false)
                {
                    image.SetPixel(c, r, Color.Black);
                    index++;

                }
                else
                {
                    image.SetPixel(c, r, Color.White);
                    index++;
                }

            }
        }
        String n = "aa";
        image.Save(n + ".png", System.Drawing.Imaging.ImageFormat.Png);


    }

    Console.Write("\nPress any key to continue . . .");
    Console.ReadKey(true);
    }


}
使用系统;
使用系统图;
使用System.IO;
使用系统集合;
班级计划
{
静态void Main(字符串[]参数)
{
BinaryReader binReader=新的BinaryReader(File.Open(args[0],FileMode.Open));
字节[]aa=新字节[1000];
int宽度、高度;
尝试
{
//读取字节,直到流结束

对于(int i=0;i)可能您可以提供转换前的图像和转换后的图像请首先提及(和/或)带有languagefirst的标记,您读取二进制数据的方式非常难看。只需使用
File.ReadAllBytes(inputFilename);