C#图像中心缩放、移动及文字旋转、镜像

C#中使用Graphics可以很方便的绘图,在绘完图后,往往需要对图进行缩放和移动。缩放时,将鼠标当前的位置作为缩放的中心来缩放,看效果图

中心缩放的核心在于计算图形新的原点,请看代码

 

 public partial class Form1 : Form
    {
        #region 内部变量
        private Graphics _g = null;
        private Image _imageCache = null;

        /// <summary>
        /// 单元格的宽(100%)
        /// </summary>
        private int _cellWidth_px = 100;
        /// <summary>
        /// 单元格的高(100%)
        /// </summary>
        private int _cellHeight_px = 100;

        private float _zoomOld = 1.0f;
        private float _zoom = 1.0f;
        private float _zoomMin = 0.1f;
        private float _zoomMax = 1000f;


        /// <summary>
        /// 表格的左上角
        /// </summary>
        private PointF _gridLeftTop = new PointF(200, 200);

        private bool _leftButtonPress = false;

        private PointF _mousePosition = new PointF(0, 0);
        #endregion

        public Form1()
        {
            InitializeComponent();

            //设置Paint参数以便能更好的控制Paint.
            SetStyle(ControlStyles.AllPaintingInWmPaint |
               ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
            this.MouseWheel += Form1_MouseWheel; ;
        }

        private void Form1_MouseWheel(object sender, MouseEventArgs e)
        {
            var delta = e.Delta;
            if (Math.Abs(delta) < 10)
            {
                return;
            }
            var mousePosition = new PointF();
            mousePosition.X = e.X;
            mousePosition.Y = e.Y;
            _zoomOld = _zoom;

            if (delta < 0)
            {
 
  • 8
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值