If the user selects "OK", you're storing the selected path in
textBox1
. You're then trying to open the file based on the value of
textBox4
, which almost certainly doesn't contain a valid file path.
If the user selects "Cancel", you're
still trying to open the file based on the value of
textBox4
.
Change your code to use the correct path, and only process the path if the user selects "OK":
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog open = new OpenFileDialog())
{
open.Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg";
if (open.ShowDialog() == DialogResult.OK)
{
textBox1.Text = open.FileName;
Bitmap bitmap = new Bitmap(open.FileName);
byte[] bimage = File.ReadAllBytes(open.FileName);
}
}
}
Also, do yourself a favour and give your controls meaningful names. You might remember that
textBox1
is the image path and
textBox42
is the frood-name
now, but when you come back to your code in a few weeks' time, you'll have forgotten what these controls represent.