如何改变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;
}
}
}
为什么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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试以下操作。我正在使用这段代码,它对我有用。我不确定与你的有什么区别(也许先设置图像,然后设置大小),但它确实有效。如果它不起作用,请检查@dzendras解决方案,也许您配置了不同的东西。
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.
您是否将 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.
在图片框控件的属性窗口中进行更改。
单击图片框。
设置尺寸字段。
Change in the properties window of the picturebox control.
click on the picturebox.
set size field.