Heyo, I'm messing with converting images to ASCII ones. For this I load the image, use getPixel() on each pixel, then 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
Your issue is being caused by using += to set the Text value. Using += is causing your formatting to be lost by re-setting the Text value and assigning a new string value.
You need to change your code to use Append() instead.
richTextBox1.Append("x");
richTextBox1.Append("\n");
From MSDN:
You can use this method to add text to the existing text in the control instead of using the concatenation operator (+) to concatenate text to the Text property.