无法在面板中设置 PictureBox 位置
我创建了一个面板。 这有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果在滚动离开坐标 0,0 之后添加图片框,您可能需要通过将滚动量添加到 PixelCoordinates 来解决此问题。尝试在计算中使用
imagePanel.VerticalScroll.Value
和imagePanel.HorizontalScroll.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
andimagePanel.HorizontalScroll.Value
in your calculations.