A
A
Anton_repr2019-03-30 06:47:25
C++ / C#
Anton_repr, 2019-03-30 06:47:25

How to set a color for a number?

There is a code that converts a string to binary. Now we need to break these numbers into pixels. Ie 1 - white color, 0 - black color. We have to conditionally walk through the numbers, draw pixels if 1 or 0. How to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2019-03-30
@Anton_repr

Here, for example, filling a PictureBox with pixels in one row

var str = "hello world";
            var sb = new StringBuilder();
            foreach (var c in str)
            {
                sb.Append(Convert.ToString(c, 2));
            }

            str = sb.ToString();

            var image = new Bitmap(str.Length, 1);
            
            for (var index = 0; index < str.Length; index++)
            {
                var c = str[index];
                switch (c)
                {
                    case '1':
                        image.SetPixel(index,0, Color.Black);
                        break;
                    case '0':
                        image.SetPixel(index,0, Color.White);
                        break;
                }
            }
            pictureBox1.Image = image;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question