如何改变PictureBox的大小?

发布于 2024-10-10 09:55:49 字数 1116 浏览 3 评论 0原文

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;
        }
    }
}

为什么PictureBox的大小一直是(1, 1),没有改变图片的大小?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

日暮斜阳 2024-10-17 09:55:49

尝试以下操作。我正在使用这段代码,它对我有用。我不确定与你的有什么区别(也许先设置图像,然后设置大小),但它确实有效。如果它不起作用,请检查@dzendras解决方案,也许您配置了不同的东西。

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

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;
源来凯始玺欢你 2024-10-17 09:55:49

您是否将 pictureBox1.MaximumSize 设置为 {0;0} 以外的任何值?
例如,当它设置为 {1;1} 时,即使您有意设置它的大小(例如在处理程序中),它也不会变得比这更大。

希望这有帮助。

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.

花间憩 2024-10-17 09:55:49

在图片框控件的属性窗口中进行更改。
单击图片框。
设置尺寸字段。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文