【问题标题】:Location property of picturebox in c#c#中图片框的位置属性
【发布时间】:2015-03-14 14:55:38
【问题描述】:

我有一个代码可以在面板中显示多个图像

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

但是当我选择第三张图片时,第二张和第三张图片之间的距离与第一张和第二张之间的距离不同。如何使图像之间的距离相等?

【问题讨论】:

  • 您想如何布局 PB?都是一排还是多排?
  • 再次检查,我认为您的代码运行良好!你是什​​么意思'距离..变得异常。'??这会随着您选择的图片而改变吗?
  • 只有一点点你可能想要改进:如果你的 PB 有一个边框,你应该设置他们的客户大小,而不是外部边界:pictures[ItemCount].ClientSize = images[ItemCount].Size; - 嗯,当然有一个 FlowLayoutPanel,它将做所有的布局工作!
  • 我想将 PB 放置在多行一列中。问题是第三个PB的位置离第二个picturebox很远。
  • 所以它是可重现的?您是否总是以相同的顺序选择相同的图像?试试加这个:pictures[ItemCount].BorderStyle = BorderStyle.Fixed3D;看看效果更好!

标签: c# location picturebox


【解决方案1】:

试试这样的:

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

一般你需要知道

  • PictureBoxesSizeMode 是什么?
  • 除了None之外,他们还有BorderStyle吗?

但这里代码中的错误更微妙:Location 必须相对于上面的PictureBox 所在位置进行设置。带有变量top 的代码可以工作,但前提是Panel 没有滚动。直接将其链接到放置的最后一张图片,即使您向下滚动也能正常工作,我希望..

上面的代码将下一个框放置在与前一个框的Bottom 相关的位置,间距为 10 像素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多