Paul
Paul

Reputation: 26690

WinForms: PictureBox initialization issue

If I put PictureBox initialization into form's constructor or form.Load / form.Shown handler nothing is drawn on PictureBox. If initialization is being made just before drawing, graphics appears.

Why this code draws an array on PictureBox:

public partial class Form1 : Form
{
    Bitmap drawArea;

    public Form1()
    {
        InitializeComponent();
    }

    private void drawArray(int[] arr, PictureBox box)
    {
        //=========== Attention to this code ================
        drawArea = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height);
        pictureBox1.Image = drawArea;
        //===================================================
        using (Graphics g = Graphics.FromImage(drawArea))
        {
            Pen mypen = new Pen(Brushes.Black);
            g.Clear(Color.White);
            for (int i = 0; i < arr.Length; i++)
                g.DrawLine(mypen, i*2, drawArea.Height,
                   i*2, drawArea.Height - arr[i]);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int[] ar1 = randomArray(20, 1, 20);
        drawArray(ar1, pictureBox1);
    }
}

but this code doesn't?

public partial class Form1 : Form
{
    Bitmap drawArea;

    public Form1()
    {
        InitializeComponent();
        //=========== Attention to this code ================
        //Breakpoint here: pictureBox1.Size.Width==409, pictureBox1.Size.Height==205
        drawArea = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height);
        pictureBox1.Image = drawArea;
        //===================================================
    }

    private void drawArray(int[] arr, PictureBox box)
    {
        using (Graphics g = Graphics.FromImage(drawArea))
        {
            Pen mypen = new Pen(Brushes.Black);
            g.Clear(Color.White);
            for (int i = 0; i < arr.Length; i++)
                g.DrawLine(mypen, i*2, drawArea.Height,
                  i*2, drawArea.Height - arr[i]);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int[] ar1 = randomArray(20, 1, 20);
        drawArray(ar1, pictureBox1);
    }
}

It does not even work if I make a second button and put the initialization code in its handler (and of course click second button before clicking the first).

No exception is thrown.

Upvotes: 1

Views: 2001

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39152

Not sure...just Invalidate() the PictureBox in drawArray() so it refreshes itself:

    private void drawArray(int[] arr, PictureBox box)
    {
        using (Graphics g = Graphics.FromImage(drawArea))
        {
            Pen mypen = new Pen(Brushes.Black);
            g.Clear(Color.White);
            for (int i = 0; i < arr.Length; i++)
                g.DrawLine(mypen, i * 2, drawArea.Height,
                  i * 2, drawArea.Height - arr[i]);
        }
        box.Invalidate();
    }

*Why were you passing the PictureBox anyways, if you weren't using it?

Upvotes: 1

Related Questions