无法在面板中设置 PictureBox 位置

发布于 2024-08-27 15:08:36 字数 943 浏览 7 评论 0原文

我创建了一个面板。 这有 autoscroll = true

一开始我添加了 6 个带有图像的 256x256 图片框。 我存储了最后一个图片框的位置,以便我知道在哪里放置新的图片框。

我还在面板的右上角添加了一个图片框(位置(8744,8744)),以便面板将拉伸到 9000px。

稍后,当我在面板中滚动时,我可以按下按钮并向面板添加图片框。问题是,当我设置图片框的位置并将其添加到面板时,它在视觉上完全错误。

添加更多图像的代码。

private void addPictureBox(Point pixelCoordinates, Bitmap image)
{
        PictureBox pNewImage = new PictureBox();
        imagePanel.Controls.Add(pNewImage);

        pNewImage.Image      = image;
        pNewImage.Name       = "image_:" + pixelCoordinates.X + "_" + pixelCoordinates.Y;
        pNewImage.Location   = pixelCoordinates;
        pNewImage.Size       = new System.Drawing.Size(256, 256);
        pNewImage.Visible    = true;
        pNewImage.BackColor  = Color.White;

        imagePanel.Update();

}

如果我调试并观察面板,它会说新的图片框具有我设置的位置,但从视觉上看,事实并非如此。

我注意到这就是真正发生的事情: 图片框的位置是我滚动的位置+ location.X。

有人知道我该如何解决这个问题吗?

提前致谢。

I have a created a panel.
This has autoscroll = true

At the start i added 6 pictureboxes that are 256x256 with images.
I store the last picturebox location, so that i know where to put a new picturebox.

I also add a picturebox to the upper right of the panel(location(8744,8744)), so that the panel will stretch to 9000px.

Later on when i scroll around in the panel, i can push a button and add a picturebox to the panel. The problem is that when i set the location of the picturebox and add it to the panel, it comes out totally wrong, visually.

Code for adding more images.

private void addPictureBox(Point pixelCoordinates, Bitmap image)
{
        PictureBox pNewImage = new PictureBox();
        imagePanel.Controls.Add(pNewImage);

        pNewImage.Image      = image;
        pNewImage.Name       = "image_:" + pixelCoordinates.X + "_" + pixelCoordinates.Y;
        pNewImage.Location   = pixelCoordinates;
        pNewImage.Size       = new System.Drawing.Size(256, 256);
        pNewImage.Visible    = true;
        pNewImage.BackColor  = Color.White;

        imagePanel.Update();

}

If i debug and watches the panel, it says that the new picturebox has the location i set, but visually, it's not.

I have noticed that this is what really happens:
The location of the picturebox is from where i have scrolled + location.X.

Anyone got an idea how i can fix this?

Thanks in advance.

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

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

发布评论

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

评论(1

握住我的手 2024-09-03 15:08:36

如果在滚动离开坐标 0,0 之后添加图片框,您可能需要通过将滚动量添加到 PixelCoordinates 来解决此问题。尝试在计算中使用 imagePanel.VerticalScroll.ValueimagePanel.Horizo​​ntalScroll.Value

If the pictureboxes are being added after you have scrolled away from the coordinates 0,0 you may need to account for this by adding the scroll amount to pixelCoordinates. Try using imagePanel.VerticalScroll.Value and imagePanel.HorizontalScroll.Value in your calculations.

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