Windows Forms PictureBox - how to display an image in a specific area of a form
I am using the following code to open and display an image in one of my forms with fileDialog
:
private void btnExplorer_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
PictureBox PictureBox1 = new PictureBox();
PictureBox1.Image = new Bitmap(openFileDialog1.FileName);
// Add the new control to its parent controls collection
this.Controls.Add(PictureBox1);
}
catch (Exception ex)
{
MessageBox.Show("Error loading image" + ex.Message);
}
}
}
The problem is that my image appears in the upper left corner of my form when I left almost a quarter of my right side for this purpose. How can I show it there?
+3
source to share
2 answers
As I said in my comment, here's how: How to manage positions in Windows formats .
PictureBox PictureBox1 = new PictureBox();
PictureBox1.Image = new Bitmap(openFileDialog1.FileName);
PictureBox1.Location = new Point(20, 100); //20 from left and 100 from top
this.Controls.Add(PictureBox1);
Or change it later:
PictureBox1.Top += 50; //increase distance from top with 50
+8
source to share