将 PictureBox 保持在容器内居中
我正在设计一个简单的图片查看器,能够进行一些基本的图像处理。目前,我遇到的问题是始终将 PictureBox
保持在 TabPage
内居中,并保持图片框的宽度和大小与其显示的图片相同。到目前为止我还没有成功。
我在表单构造函数中调用以下代码以将其定位在中心。它第一次可以使图片框居中:
private void SetPictureBoxOriginalSizeAndLocation(bool makeImageNull = false, DockStyle dockStyle = DockStyle.None)
{
if (makeImageNull) picBoxView.Image = null;
picBoxView.Dock = dockStyle;
var xPoint = tabImageView.Location.X + ((splitContainer.Panel2.Width / 2) / 2);
var yPoint = tabImageView.Location.Y;
var width = tabImageView.Width / 2;
var height = (tabImageView.Height / 2) - toolStripImageView.Height;
if (picBoxView.Image == null) return;
//Resize image according to width
picBoxView.Image = ImageMethods.ResizeImage(picBoxView.Image.Tag.ToString(), width, height, false);
picBoxView.Location = new Point(xPoint, yPoint);
picBoxView.Width = width;
picBoxView.Height = height;
}
但它不会将图片框的大小调整为其图像(您可以看到图片框控件的背景颜色的黑色部分):
一旦我调整表单大小,问题就会变得更糟,图片框位置将转到顶部:
我也在表单的调整大小事件中调用上面的代码,不知道为什么它在应用程序启动时起作用。如果有人能告诉我应该注意哪些属性才能实现一个居中且始终与其图像一样大的图片框,那就太好了。
I am designing a simple picture viewer with ability to do some basic image processing. At the moment I have the problem of keeping the PictureBox
centered inside a TabPage
all the time as well as keeping the picturebox width and size same as the picture its showing. So far I had no success.
I have the following code that I call in form constructor to position it in center. it works the first time to center the picturebox:
private void SetPictureBoxOriginalSizeAndLocation(bool makeImageNull = false, DockStyle dockStyle = DockStyle.None)
{
if (makeImageNull) picBoxView.Image = null;
picBoxView.Dock = dockStyle;
var xPoint = tabImageView.Location.X + ((splitContainer.Panel2.Width / 2) / 2);
var yPoint = tabImageView.Location.Y;
var width = tabImageView.Width / 2;
var height = (tabImageView.Height / 2) - toolStripImageView.Height;
if (picBoxView.Image == null) return;
//Resize image according to width
picBoxView.Image = ImageMethods.ResizeImage(picBoxView.Image.Tag.ToString(), width, height, false);
picBoxView.Location = new Point(xPoint, yPoint);
picBoxView.Width = width;
picBoxView.Height = height;
}
But it does not resize the picturebox to its image (you can see the black part that is back color for the picturebox control):
The problem is getting worse as soon as I resize the form, the picturebox position will goes to top:
I call the code above in form's resize event as well, no idea why it works when application starts. Would be nice if someone can tell me what properties I should take care of to achieve a nicely centered picturebox which always is as big as its image.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信你的问题就在这里
ypoint 总是设置为 tabImageView Y,尽管它应该设置为
应该与 xPoint 几乎相同
并且
I believe your problem lies here
ypoint is alwways set to tabImageView Y, althought it should be set to
should be almost the same with xPoint
and
如果您只需将
Anchor
样式设置为 none,这非常简单:然后每当您更改
PictureBox
的图像时,只需最初将PictureBox
居中即可:每当父容器调整大小时,
Anchor = None
都会将PictureBox
控件居中,因为它“没有”锚定到默认的左侧和顶部位置。It's pretty easy if you just set the
Anchor
style to none:Then just center the
PictureBox
initially whenever you change the image of thePictureBox
:Having the
Anchor = None
will center thePictureBox
control for you whenever the parent container gets resized because it "isn't" anchored to the default Left and Top locations.给定一个带有
TabControl
的Form
,其中Dock
设置为Fill
,下面将保留您的PictureBox< /code> 在中心。它还将
PictureBox
大小设置为Bitmap
大小:Givien a
Form
withTabControl
, which hasDock
set toFill
, below will keep yourPictureBox
in the centre. It also setsPictureBox
size toBitmap
size: