基本信息
源码名称:自定义图片控件(实现了图片自适应、缩放、拖动、旋转等功能)
源码大小:2.18M
文件格式:.zip
开发语言:C#
更新时间:2019-08-11
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 4 元 
   源码介绍

自定义图片控件,实现鼠标滚轮以当前鼠标位置缩放,鼠标拖动改变位置,自适应图片,旋转图片等。

 protected override void OnMouseWheel(MouseEventArgs e)
        {
            base.OnMouseWheel(e);
            if (_img == null)
                return;
            if (!this.ClientRectangle.Contains(e.Location))
                return;

            #region zoom
            _isAutoFit = false;
            float zoom;
            if (e.Delta > 0)
            {
                if (_currentZoom < _maxZoom)
                {
                    zoom = _currentZoom * 1.1f;
                }
                else
                {
                    zoom = _maxZoom;
                }
                if (zoom > _maxZoom)
                    zoom = _maxZoom;
            }
            else
            {
                if (_currentZoom > _minZoom)
                    zoom = _currentZoom * 0.9f;
                else
                    zoom = _minZoom;
                if (zoom < _minZoom)
                    zoom = _minZoom;
            }

            #endregion

            #region RectangleF
            //滚轮相对于zoom图片的位置
            PointF px;
            if (_desRectf.Contains(e.Location))
                px = new PointF(e.X, e.Y);
            else
                px = new PointF(_desRectf.X _desRectf.Width / 2, _desRectf.Y _desRectf.Height / 2);

            var pxScaleX = (px.X - _zoomRectf.X) / _zoomRectf.Width;
            var pxScaleY = (px.Y - _zoomRectf.Y) / _zoomRectf.Height;
            _zoomRectf.X = _zoomRectf.X - _img.Width / 100f * (zoom - _currentZoom) * pxScaleX;
            _zoomRectf.Y = _zoomRectf.Y - _img.Height / 100f * (zoom - _currentZoom) * pxScaleY;
            _zoomRectf.Width = _img.Width / 100f * zoom;
            _zoomRectf.Height = _img.Height / 100f * zoom;

            _desRectf = GetClientRectangleF();
            _desRectf.Intersect(_zoomRectf);

            CalcuSrcRectangleF();

            #endregion

            _currentZoom = zoom;

            this.Invalidate();
        }
//通过_zoomRectf和desRectf计算_srcRectf
        private void CalcuSrcRectangleF()
        {
            var srcScaleX = (_desRectf.X - _zoomRectf.X) / _zoomRectf.Width;
            var srcScaleY = (_desRectf.Y - _zoomRectf.Y) / _zoomRectf.Height;
            var srcScaleW = _desRectf.Width / _zoomRectf.Width;
            var srcScaleH = _desRectf.Height / _zoomRectf.Height;
            _srcRectf.X = _img.Width * srcScaleX;
            _srcRectf.Y = _img.Height * srcScaleY;
            _srcRectf.Width = _img.Width * srcScaleW;
            _srcRectf.Height = _img.Height * srcScaleH;
        }

        //绘制一张新的图片
        private Image CloneC(Image img, PixelFormat format = PixelFormat.Format32bppArgb)
        {
            var bmp = new Bitmap(img.Width, img.Height, format);
            using (var g = Graphics.FromImage(bmp))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height));
            }
            return bmp;
        }

        //计算自适应的相关参数
        private void CalcuAutoSize()
        {
            if (_img == null)
                return;
            var clientRectf = GetClientRectangleF();
            var scale = 1f;
            if (_img.Width > clientRectf.Width || _img.Height > clientRectf.Height)
            {
                var xscale = _img.Width / clientRectf.Width;
                var yscale = _img.Height / clientRectf.Height;
                scale = xscale > yscale ? xscale : yscale;
            }
            _currentZoom = 100 / scale;
            var autoWidth = _img.Width / scale;
            var autoHeight = _img.Height / scale;
            var zoomx = (clientRectf.Width - autoWidth) / 2;
            var zoomy = (clientRectf.Height - autoHeight) / 2;
            _srcRectf = new RectangleF(0, 0, _img.Width, _img.Height);
            _zoomRectf = new RectangleF(zoomx, zoomy, autoWidth, autoHeight);
            _desRectf = _zoomRectf;
        }