Problems inserting image from directory
I am having some problems inserting a image i have in my directory. I search through the resources I have on the internet and manage the relevant code to be use.
pictureArray[]
> is a field
Created using this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
class Field
{
public PictureBox picbox;
public Field()
{
picbox = null;
}
}
}
At the top of the code, i initalize it:
private Field[] pictureArray = new Field[100];
The portion of my code as following:
pictureArray[0].picbox = new PictureBox();
pictureArray[0].picbox.Image = System.Drawing.Image.FromFile(@"..\Postlogger\test.jpg");
pictureArray[0].picbox.Location = new System.Drawing.Point(17, 19);
pictureArray[0].picbox.Name = "picBox";
pictureArray[0].picbox.Size = new System.Drawing.Size(50, 50);
this.groupBox1.Controls.Add(pictureArray[0].picbox);
开发者_高级运维
My image doesn't appear on my screen. Any suggestion to what i have done wrongly? Thanks!
I can see problem in your code :)
You have declared array of reference type
private Field[] pictureArray = new Field[100];
If you try and debug you will see all elements in pictureArray
are null
. On the next line you are doing
pictureArray[0].picbox = new PictureBox();
I believe it is throwing exception here as pictureArray[0]
is null
. Please instantiate Field
object before referring it.
pictureArray[0] = new Field();
pictureArray[0].picbox = new PictureBox();
This should work now.
精彩评论