Miko Kronn
Miko Kronn

Reputation: 2454

How can I change size of PictureBox?

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?

Upvotes: 3

Views: 19682

Answers (3)

user4977970
user4977970

Reputation: 1

Change in the properties window of the picturebox control. click on the picturebox. set size field.

Upvotes: -1

Daniel Peñalba
Daniel Peñalba

Reputation: 31847

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.

Bitmap img = new Bitmap(dgOpenFile.FileName);
picture.Image = img;
picture.Size = picture.Image.Size;

Upvotes: 5

dzendras
dzendras

Reputation: 4751

Do you have pictureBox1.MaximumSize set to anything else than {0;0} ? When it is set to {1;1} for instance it won't get bigger than this even if you set it's size intentionally (like in the handler).

Hope this helps.

Upvotes: 2

Related Questions