[英]Location property of picturebox in c#
我有一个代码可以在面板中显示多个图像
public List<Image> images = new List<Image>();
public List<PictureBox> pictures = new List<PictureBox>();
public int top=10;
public int ItemCount = 0;
private void tsbOpen_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.Filter="Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
if (file.ShowDialog() == DialogResult.OK)
{
images.Add(Image.FromFile(file.FileName));
pictures.Add(new PictureBox());
pictures[ItemCount].Image = images[ItemCount];
pictures[ItemCount].Width = images[ItemCount].Width;
pictures[ItemCount].Height = images[ItemCount].Height;
pnlMain.Controls.Add(pictures[ItemCount]);
pnlMain.Controls[ItemCount].Location=new Point(0,top);
top += (images[ItemCount].Size.Height+10);
ItemCount++;
}
}
但是,当我选择第三张图像时,第二张和第三张图像之间的距离不同于第一张和第二张图像之间的距离。 如何使图像之间的距离相等?
尝试这样的事情:
int space = 10;
if (ItemCount > 0) pnlMain.Controls[ItemCount].Location =
new Point(0, pnlMain.Controls[ItemCount - 1].Bottom + space);
else pnlMain.Controls[ItemCount].Location = new Point(0, space );
通常你需要知道
PictureBoxes
的SizeMode
是多少? BorderStyle
比其他None
? 但是,这里的代码错误是微妙的:必须根据上方PictureBox
的Location
设置位置。 带有变量top
的代码有效,但Panel
是不滚动 Panel
。 我希望直接将其链接到最后放置的图片,即使向下滚动也可以使用。
上面的代码将下一个框相对于上一个框的Bottom
放置,间距为10个像素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.