partial class Form1
{
//hidden
private void InitializeComponent()
{
this.picture = new System.Windows.Forms.PictureBox();
//hidden
this.picture.Size = new System.Drawing.Size(1, 1);
//hidden
}
#endregion
private System.Windows.Forms.PictureBox picture;
private System.Windows.Forms.Button btnLoad;
private System.Windows.Forms.OpenFileDialog dgOpenFile;
private System.Windows.Forms.Panel panel1;
}
---
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnLoad_Click(object sender, EventArgs e)
{
if (dgOpenFile.ShowDialog() == DialogResult.OK)
{
Bitmap img = new Bitmap(dgOpenFile.FileName);
picture.Width = img.Width;
picture.Height = img.Height;
picture.Image = img;
}
}
}
Why the size of PictureBox stays (1, 1) and doesn’t change to the size of image?
Try the following. I’m using this code and it’s working for me. I’m not sure what is the difference with yours (maybe first setting the image and then the size), but it really works. If it does not work, check @dzendras solution, maybe you have configured something different.