C# 将图片框保持在容器内居中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9375588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Keeping a PictureBox centered inside a container
提问by Saeid Yazdani
I am desiging a simple picture viewer with ability to do some basic image processing. At the moment I have the problem of keeping the PictureBoxcentered inside a TabPageall the time as well as keeping the picturebox width and size same as the picture its showing. So far I had no success.
我正在设计一个能够进行一些基本图像处理的简单图片查看器。目前,我有一个问题,即始终保持PictureBoxa 内居中TabPage,同时保持图片框的宽度和大小与其显示的图片相同。到目前为止,我没有成功。
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 aswell, no idea why it works when application starts. Would be nice if someone can tell me what properties I should take care of to achive a nicely centered picturebox wich always is as big as its image.
我也在表单的调整大小事件中调用上面的代码,不知道为什么它在应用程序启动时起作用。如果有人能告诉我我应该注意哪些属性来实现一个很好的居中的图片框,它总是和它的图像一样大,那就太好了。
采纳答案by LarsTech
It's pretty easy if you just set the Anchorstyle to none:
如果您只是将Anchor样式设置为无,则非常简单:
picBoxView = new PictureBox();
picBoxView.SizeMode = PictureBoxSizeMode.AutoSize;
picBoxView.Anchor = AnchorStyles.None;
tabImageView.Controls.Add(picBoxView);
CenterPictureBox(picBoxView, myImage);
Then just center the PictureBoxinitially whenever you change the image of the PictureBox:
然后,PictureBox每当您更改 的图像时,只需将其居中即可PictureBox:
private void CenterPictureBox(PictureBox picBox, Bitmap picImage) {
picBox.Image = picImage;
picBox.Location = new Point((picBox.Parent.ClientSize.Width / 2) - (picImage.Width / 2),
(picBox.Parent.ClientSize.Height / 2) - (picImage.Height / 2));
picBox.Refresh();
}
Having the Anchor = Nonewill center the PictureBoxcontrol for you whenever the parent container gets resized because it "isn't" anchored to the default Left and Top locations.
具有Anchor = None将集中在PictureBox控制你只要父容器被调整,因为它“不”锚定到默认Left和Top的位置。
回答by StaWho
Givien a Formwith TabControl, which has Dockset to Fill, below will keep your PictureBoxin the centre. It also sets PictureBoxsize to Bitmapsize:
给定一个Formwith TabControl,它已Dock设置为Fill,下面将使您PictureBox处于中心位置。它还将PictureBox大小设置为Bitmap大小:
public partial class Form1 : Form
{
Bitmap b = new Bitmap(320, 200);
public Form1()
{
InitializeComponent();
CenterTheBox();
}
private void Form1_Resize(object sender, EventArgs e)
{
CenterTheBox();
}
void CenterTheBox()
{
pictureBox1.Size = b.Size;
var left = (tabPage1.ClientRectangle.Width - pictureBox1.ClientRectangle.Width) / 2;
var top = (tabPage1.ClientRectangle.Height - pictureBox1.ClientRectangle.Height) / 2;
pictureBox1.Location = new Point(tabPage1.ClientRectangle.Location.X + left, tabPage1.ClientRectangle.Location.Y + top);
}
}
回答by Milan Halada
I believe your problem lies here
我相信你的问题就在这里
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;
ypoint is alwways set to tabImageView Y, althought it should be set to
ypoint 总是设置为 tabImageView Y,虽然它应该设置为
tabImageView.Location.Y + (tabImageView.Size.Height - picBoxView.Size.Height)/2
should be almost the same with xPoint
应该和 xPoint 几乎一样
tabImageView.Location.X + (tabImageView.Size.Width - picBoxView.Size.Width)/2
and
和
width = picBoxView.Image.Width;
height = picBoxView.Image.Height;

