c# 如何让picturebox控件 居中显示在panel中

如题,因为我需要动态改变pic控件的大小,但是改变大小之后他的位置如何重新定位呢,求高手给段代码... 如题,因为我需要动态改变pic控件的大小,但是改变大小之后他的位置如何重新定位呢,求高手给段代码 展开
 我来答
du瓶邪
推荐于2016-03-24 · TA获得超过2.4万个赞
知道大有可为答主
回答量:1.7万
采纳率:100%
帮助的人:2746万
展开全部
示例:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ToolTipTest
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.ComponentModel.IContainer components;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(736, 273);
this.panel1.TabIndex = 0;
this.panel1.SizeChanged += new System.EventHandler(this.panel1_SizeChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(736, 273);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private PictureBox[] p =new PictureBox[100];
private void Form1_Load(object sender, System.EventArgs e)
{

for(int i=0;i<p.Length;i++)
{
p[i]=new PictureBox();
p[i].BorderStyle=BorderStyle.FixedSingle;
this.panel1.Controls.Add(p[i]);
}

SetControlsLocation();

}

private void panel1_SizeChanged(object sender, System.EventArgs e)
{
SetControlsLocation();
}
private void SetControlsLocation()
{
this.panel1.Hide();
int ControlWidth =this.panel1.Width/10;
int ControlHeight =this.panel1.Height/10;
int x =0;
int y=0;
foreach (Control tmpCtr in this.panel1.Controls)
{
tmpCtr.Width =ControlWidth;
tmpCtr.Height =ControlHeight;
if(this.panel1.Width-x<ControlWidth)
{
x=0;
y+=ControlHeight;
}
tmpCtr.Left =x;
tmpCtr.Top=y;
tmpCtr.BringToFront();
x+=ControlWidth;

}
this.panel1.Show();
}

}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
tq0cdxa
2013-06-28 · TA获得超过123个赞
知道小有建树答主
回答量:210
采纳率:0%
帮助的人:206万
展开全部
完全填充this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
固定的话只有计算位置了
pictureBox1.Location = new System.Drawing.Point((pictureBox1.Parent.Size.Width - pictureBox1.Size.Width) / 2, (pictureBox1.Parent.Size.Height - pictureBox1.Size.Height) / 2);
非固定可能是这个
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
syht2000
高粉答主

2013-06-28 · 关注我不会让你失望
知道大有可为答主
回答量:3万
采纳率:79%
帮助的人:1.4亿
展开全部

位置你可以自己算出来,大概是

picturebox.Top=(panel.Height-picturebox.Height)/2;
pictureboxT.Left=(panel.Width-picturebox.Width)/2;
追问
因为我的图片很大有2800*3200等等,我想动态给pic控件设置Size是根据图片的比例缩小pic的size,这应该如何设置啊
追答
我手上没有vs,只说原理,代码你可以自己改一下。先将比如picbox的visiable设为false;然后加载图片,用picbox.image.width和height应该可以得到图片的长和宽(假定宽x,高为y),用x/y*1.0可以得到比例,假定是0.8(同时可以知道较长的边为y),假定你最终需要将picbox的长宽均不超过500,那就可以picbox.height=500;picbox.width=500*0.8就行了。将picbox模式为缩放模式,再显示出来就可以了。
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式