如果你想动态布局图片,可能代码如下。
private void buttonLayout_Click(object sender, EventArgs e)
{
int x=0;
int y=0;
for (int index = 0; index < 6;index++ )
{
string path = @"C:\Pictures\" + index.ToString() + ".png";
if (!File.Exists(path))
{
continue;
}
Image image=Image.FromFile(path);
PictureBox pictureBox = new PictureBox();
pictureBox.Image = image;
pictureBox.BorderStyle = BorderStyle.FixedSingle;
pictureBox.Left = x;
pictureBox.Top = y;
pictureBox.Width = image.Width;
pictureBox.Height = image.Height;
pictureBox.SizeMode = PictureBoxSizeMode.Normal;
panelPictures.Controls.Add(pictureBox);
x += pictureBox.Image.Width;
}
}
如果布局更多行,请检查 x 和 y 是否在范围内。