2

我有一个简单的文件,它读取单色位图(即黑白)并打印出 x 来表示黑色部分。但是,我注意到由于某种原因我需要添加代码

img.RotateFlip(RotateFlipType.Rotate270FlipY);

让它“正常”显示。换句话说,似乎我的代码或内置函数的实现中的某些内容在读取图像时正在旋转和翻转图像,这与我的意图背道而驰。

我打赌这是一个比“按预期工作”更愚蠢的错误,所以我在下面包含了相关代码:

    private void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            // Stream objects for capturing image data
            StreamReader sr = new StreamReader(openFileDialog1.OpenFile());
            MemoryStream memStream = new MemoryStream();

            // Image objects containing image data
            Bitmap img = (Bitmap) Bitmap.FromStream(sr.BaseStream);


            /**** HACK ****/
            //img.RotateFlip(RotateFlipType.Rotate270FlipY);
            /****      ****/


            // Save the image data to our memory stream
            img.Save(memStream, ImageFormat.Gif);

            // Save data to a byte array
            byte[] imgData = memStream.ToArray();

            // Print image data
            for (int x = 0; x < img.Width; ++x)
            {
                Color rgb = new Color();
                for (int y = 0; y < img.Height; ++y)
                {
                    rgb = img.GetPixel(x, y);

                    if (rgb.ToArgb().Equals(Color.White.ToArgb()))
                    {
                       textBox1.AppendText(" ");
                    }
                    else
                    {
                        textBox1.AppendText("x");
                    }
                }
                textBox1.AppendText(Environment.NewLine);
            }
        }
    }

我读到了字母“B”的位图,它给了我以下信息。

没有“黑客”:

     x                         x        
     x                         x        
     x                         x        
     xx                       xx        
     xxxxxxxxxxxxxxxxxxxxxxxxxxx        
     xxxxxxxxxxxxxxxxxxxxxxxxxxx        
     xxxxxxxxxxxxxxxxxxxxxxxxxxx        
     x            x            x        
     x            x            x        
     x            x            x        
     x            x            x        
     x            x            x        
     x            x            x        
     x            x            x        
     x            x            x        
                  x            x        
      x          xx                     
      x         xxxx          x         
      xx       xxx x          x         
       xx     xxxx xx        xx         
       xxxxxxxxxx   xxx    xxx          
         xxxxxxx    xxxxxxxxxx          
           xxx       xxxxxxx            
                       xxx              

使用“黑客”:

     xxxxxxxxxxxxxxx                   
        xxxx         xxx               
         xxx           xxx             
         xxx            xx             
         xxx             xx            
         xxx             xx            
         xxx             xxx           
         xxx             xxx           
         xxx             xxx           
         xxx            xxx            
         xxx           xxxx            
         xxx          xxxx             
         xxx         xxxx              
         xxxxxxxxxxxxxx                
         xxx          xxx              
         xxx            xxx            
         xxx             xxx           
         xxx             xxx           
         xxx              xxx          
         xxx              xxx          
         xxx              xxx          
         xxx              xx           
         xxx             xxx           
         xxx             xx            
         xxx            xxx            
        xxxx          xxx              
     xxxxxxxxxxxxxxxx                  

我非常期待有人指出我的错误。:)

提前致谢!

4

4 回答 4

4

您的问题是您正在使用内部循环构建行以放入文本框中,但是您的内部循环在 y 上循环。尝试改变这些循环的嵌套,看看你会得到什么。此外,您的 y 循环需要从顶部开始,否则图像将上下颠倒。

    for (int y = img.Height - 1; y >= 0; --y)
    {
        Color rgb = new Color();
        for (int x = 0; x < img.Width; ++x)
        {
            rgb = img.GetPixel(x, y);

            if (rgb.ToArgb().Equals(Color.White.ToArgb()))
            {
               textBox1.AppendText(" ");
            }
            else
            {
                textBox1.AppendText("x");
            }
        }
        textBox1.AppendText(Environment.NewLine);
    }
于 2012-08-02T20:33:59.393 回答
2

您正在横向打印它。您的y循环应该是外部循环,以便您一次写入一行。

于 2012-08-02T20:32:59.150 回答
1

你有你的宽度和高度循环向后。你最里面的循环是从图像的顶部到图像的底部。

于 2012-08-02T20:33:25.227 回答
1

看起来你的内部循环正在扫描渐进式 Y 值,而你的外观使用不同的 X 值。假设输入数据处于“正常”扫描顺序,这将导致您看到的旋转。——卡尔

于 2012-08-02T20:34:53.087 回答