Search code examples
c#drawpictureboxprogress

Draw color to every pixel in picturebox


Can someone help me: I want to draw colors every pixel to picturebox

This is what I have done so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication25
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        int x, y;
        for (y = 0; y < 200; y++)
        {
            for (x = 0; x < 200; x++)
            {
                ((Bitmap)pictureBox1.Image).SetPixel(x, y, Color.FromArgb(255, 255, 0));
            }
        }
    } 

}
}

I have added a Timer so i can see the progress in drawing every pixel.

The problem the code do draw in memory - delay - then put in picture box

I want draw color every y = 0 and x = 0 to 200 , y =1 x = 0 to 200, y=2 x=0 to 200 etc


Solution

  • Each time your timer ticks you want to draw another pixel with (different?) color, right?

    What you need to do is declare current x and y coordinates outside of timer1_Tick method and increase their values accordingly every time you draw something.

    To immediately see the results you should also call pictureBox1.Refresh() inside timer1_Tick(). Here's my quick solution to draw 200x200 bitmap with a pattern similar to this: All 24-bit RGB colors.

    public partial class Form1 : Form
    {
        private int bitmapWidth = 200;
        private int bitmapHeight = 200;
        private int currentX = 0;
        private int currentY = 0;
    
        public Form1()
        {
            InitializeComponent();
            pictureBox1.Image = new Bitmap(bitmapWidth, bitmapHeight);
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled)
            {
                timer1.Stop();
            }
            else
            {
                timer1.Start();
            }
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
            double partWidth = (bitmapWidth / (double)16);
            int r = (int)(255 * currentX / partWidth) % 256;
            int g = (int)(255 * (currentY % partWidth) / partWidth) % 256;
            int b = (int)(Math.Floor(currentX / partWidth) + Math.Floor(currentY / partWidth) * 16);
    
            ((Bitmap)pictureBox1.Image).SetPixel(currentX, currentY, Color.FromArgb(r, g, b));
            pictureBox1.Refresh();
            if (currentX < bitmapWidth - 1)
            {
                currentX++;
            }
            else if (currentY < bitmapHeight - 1)
            {
                currentX = 0;
                currentY++;
            }
            else
            {
                timer1.Stop();
            }
        }
    }
    

    You can alter colors produced by calculating them off of currentX, currentY and partWidth. Note: I assumed you draw a square bitmap. Drawing to a rectangle would require some more work. Same thing applies to different sizes.