using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Windows.Forms; namespace Lskj.Main.Control { public partial class BackgroundPanel :Panel { private Image _originalBackgroundImage; // 公开属性:设置/获取背景图片 public Image BackgroundImageCustom { get => _originalBackgroundImage; set { _originalBackgroundImage = value; Invalidate(); // 设置图片后立即重绘 } } public BackgroundPanel() { // 关键设置:关闭Panel默认背景,启用双缓冲减少闪烁 this.BackgroundImage = null; this.DoubleBuffered = true; this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true); } // 重写OnPaint方法,手动绘制背景图 protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); // 图片为空时直接返回 if (_originalBackgroundImage == null) return; // 1. 计算等比缩放比例(以Panel的ClientSize为基准) float scaleX = (float)this.ClientSize.Width / _originalBackgroundImage.Width; float scaleY = (float)this.ClientSize.Height / _originalBackgroundImage.Height; float scale = Math.Min(scaleX, sc