0

I have a picturebox that will filled up by OpenFileDialog() after that, I must render the histogram (chart) from it. I use the get and set property to take the image from picturebox to another class or form. But I always getting NullReferenceException. The bitmap seems not having the image after I open a image file, so it's returning nothing. I try to fill the bitmap parameter with full path of an image and it's working, but OpenFileDialog() become pointless.

  1. Click options button:

    Click options button

  2. to render histogram chart:

    To render histogram chart

Here's my code


MainForm.cs

// button for opening image
private void openImage_Click(object sender, EventArgs e)
    {
        OpenFileDialog img = new OpenFileDialog();
        img.Title = "Open Image File...";
        img.Filter = "Image File (*.bmp, *.jpg, *.jpeg, *.png |*.bmp;*.jpg; *.jpeg;*.png";

        if (img.ShowDialog() == DialogResult.OK) {
            pbInput.Image = new Bitmap(img.FileName);
            // blablabla
        }
    }

    // set and get property
    public Image getImage {
        get { return pbInput.Image; }
        set { pbInput.Image = value; }
    }

OptionsForm.cs

    private void hist1_Click(object sender, EventArgs e)
    {
        h1 = new Histogram();
        h1.FormClosed += (s, a) => hist1.Enabled = true;
        hist1.Enabled = false;
        h1.Show();

    }

Histogram.cs

public partial class Histogram : Form
{
    MainForm m = new MainForm();

    public Histogram()
    {
        InitializeComponent();

        Bitmap b = new Bitmap(m.getImage);

        //bla bla bla. . . . . *creating histogram code
    }
 }

The error message that I got:

Error message that I get

I hope this question is clear enough. Thank you..! PS: English is not my primary language, so apologize for my grammar, etc.

MarianD
  • 13,096
  • 12
  • 42
  • 54
Nggarap
  • 63
  • 8

3 Answers3

2

When you write MainForm m = new MainForm(); in your Histogram.cs,

You create a brand new object of Form that doesn't have reference to your old form's image

What you want is to be able to access old form's object reference in your new form or better get that image3 reference in Histogram.cs

One way to do it is to pass it to the constructor

Histogram button

private void hist1_Click(object sender, EventArgs e)
{
    h1 = new Histogram(this.getImage);
    h1.FormClosed += (s, a) => hist1.Enabled = true;
    hist1.Enabled = false;
    h1.Show();
}

and then your Histogram form

public partial class Histogram : Form
{
    public Histogram(Image image)
    {
        Bitmap b = new Bitmap(image);
    }
}

This will give you the Image in the histogram form.

Mihir Dave
  • 3,954
  • 1
  • 12
  • 28
  • Oh, forgot to mention. The `hist1_Click`, is on the `OptionForm.cs`. Then I cannot implementing, what are you saying (?) – Nggarap Apr 21 '20 at 00:47
  • then you have to pass Image from MainForm to OptionForm(the same way I did it in the example), keep its reference and then pass it to Histogram. – Mihir Dave Apr 21 '20 at 10:46
0

Why don't you directly assign the image to your property?

public Image getImage { get; private set; } // Auto-implemented property.

like this

if (img.ShowDialog() == DialogResult.OK) {
    getImage = new Bitmap(img.FileName);
    pbInput.Image = getImage;
}

Btw., getImage is not a good name for a property. GetSomething is generally used for methods. Just call your property Image:

public Image Image { get; private set; }
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • I think @MihirDave found the cause of the problem. You are creating a new form in Histogram.cs with `MainForm m = new MainForm();`. This new instance of the form `MainForm` does not contain the entries you made in the original form. Add the Bitmap as constructor parameter to the options and to the historgram forms. Then pass this image when opnening the forms: `new OptionsForm(getImage);` and the same with the histogram form. – Olivier Jacot-Descombes Apr 21 '20 at 15:21
0

You should set image to image property getImage = new Bitmap(img.FileName);

Rajanikant Hawaldar
  • 314
  • 1
  • 5
  • 12