I have a simple file that reads in a monochrome bitmap (i.e. black and white) and prints out x’s to represent the black portion. However, I noticed that for some reason I need to add the code
img.RotateFlip(RotateFlipType.Rotate270FlipY);
for it to show up “normally.” In other words, it seems that something in either my code or the implementation of the built-in functions is rotating and flipping the image as it’s read in, acting counter to what I intend.
I’m betting on this is a silly mistake more than “working as intended,” so I’m including the relevant code below:
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);
}
}
}
I read in a bitmap of the letter ‘B’ and it gives me the following.
Without the “hack”:
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
With the “hack”:
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
I very much look forward to having my mistake pointed out to me. 🙂
Thanks in advance!
Your problem is that you’re building rows with your inner loop to put into the text box, but your inner loop loops over y. Try changing the nesting of those loops, and see what you get. Also, your y loop needs to start at the top, or the image will be upside down.