C# pictureBox大小自适应

 pictureBox显示图像 自动调整大小 缩放自适应 画图位置自适应
/// <summary>
/// 加载的图像变量
/// </summary>
Image img;
public Form1()
{
	//把图像的缩放交给 pictureBox 自行调整
	pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
	pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
}


double pfRatio = 0.0;//重要的参数;作图 自适应就靠它了
private void button1_Click(object sender, EventArgs e)
{
	img = Image.FromFile("1.jpg");
	pictureBox1.BackgroundImage = img;
	//pictureBox1 最大的显示范围
	int preW = 500;
	int preH = 1000;

	if ((double)preW / img.Width > (double)preH / img.Height)
	{
		pfRatio = (double)preH / img.Height;
		pictureBox1.Size = new Size((int)(img.Width * pfRatio), preH);
	}
	else
	{
		pfRatio = (double)preW / img.Width;
		pictureBox1.Size = new Size(preW, (int)(img.Height * pfRatio));
	}
	pictureBox1.Invalidate();
}
/*如果有在pictureBox1上作图,那也跟随调整位置和参数*/
//画str的数量
int StrShowCount = 0;
//画str的行间距
int StrShowInterval = 13;
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
	int r = 2;
    int penW = 2;
	StrShowCount = 0;
	
	Graphics g = e.Graphics;
	g.DrawString("HF,GG GL", Font, Brushes.Beige, new Point(0, (StrShowCount++) * StrShowInterval));
	g.DrawString("1.jpg", Font, Brushes.Beige, new Point(0, (StrShowCount++) * StrShowInterval));

	g.DrawEllipse(new Pen(Brushes.Beige, penW), new Rectangle(new Point((int)(200 * pfRatio) - r, (int)(200 * pfRatio) - r), new Size(r * 2, r * 2)));
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值