C# Problem with getPixel & setting RTF text colour accordingly

Posted by m3n on Stack Overflow See other posts from Stack Overflow or by m3n
Published on 2010-04-16T17:44:16Z Indexed on 2010/04/16 17:53 UTC
Read the original article Hit count: 443

Filed under:
|
|
|

Heyo, I'm messing with converting images to ASCII ones. For this I load the image, use getPixel() on each pixel, then change insert a character with that colour into a richTextBox.

        Bitmap bmBild = new Bitmap(openFileDialog1.FileName.ToString()); // valid image

        int x = 0, y = 0;

        for (int i = 0; i <= (bmBild.Width * bmBild.Height - bmBild.Height); i++)
        {
            // Ändra text här
            richTextBox1.Text += "x";
            richTextBox1.Select(i, 1);

            if (bmBild.GetPixel(x, y).IsKnownColor)
            {

                richTextBox1.SelectionColor = bmBild.GetPixel(x, y);
            }
            else
            {
                richTextBox1.SelectionColor = Color.Red;
            }


            if (x >= (bmBild.Width -1))
            {
                x = 0;
                y++;

                richTextBox1.Text += "\n";
            }

           x++; 
        }

GetPixel does return the correct colour, but the text only end up black. If I change

this

richTextBox1.SelectionColor = bmBild.GetPixel(x, y);

to this

richTextBox1.SelectionColor = Color.Red;

It works fine.

Why am I not getting the right colours?

(I know it doesn't do the new lines properly, but I thought I'd get to the bottom of this issue first.)

Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about getpixel