首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么做到焦点缩放图片!待

2013-12-06 
【急】如何做到焦点缩放图片!在线等待!我想做到鼠标放在那里,它就在哪里缩放。现在的要求还没有达到。我想的是

【急】如何做到焦点缩放图片!在线等待!
    我想做到鼠标放在那里,它就在哪里缩放。现在的要求还没有达到。我想的是在缩放的时候同时执行图片的移动事件,但是不知道改杂么做了,还请各位看看,帮忙解决一下,谢谢各位了~哪位大哥先解决了这个问题,分就给他了,不多,但是心意哈~

控件源码:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace PictureView
{
    public partial class pictureView : UserControl
    {
        private float _zoomScale;
        private Point _startPoint, _endPoint;
        private bool _isDown;
        private bool _isLoadBmp;
        private Bitmap _currBmp;

        public pictureView()
        {
            InitializeComponent();

            this.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
            this.MouseEnter += new EventHandler(panel1_MouseEnter);
        }

        private void pictureView_Load(object sender, EventArgs e)
        {
            SetStyle(ControlStyles.UserPaint, true);
            //防止窗口跳动
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            //防止控件跳动 
            SetStyle(ControlStyles.DoubleBuffer, true);

            _isLoadBmp = false;
            _isDown = false;
            _zoomScale = 1.0f;
            _startPoint = new Point(0, 0);
            _endPoint = new Point(0, 0);
        }

        public void panel1_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox1.Focus();
        }

        /// <summary>
        ///  处理鼠标滚动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void panel1_MouseWheel(object sender, MouseEventArgs e)
        {
            _zoomScale += (float)e.Delta / 1000;
            if (_zoomScale < 0.1)
                _zoomScale = 0.1f;
            if (_zoomScale > 10.0)
                _zoomScale = 10.0f;
            pictureBox1.Refresh();

            Point p = e.Location;

            Point p1 = pictureBox1.PointToClient(p);
            Point p2 = pictureBox1.PointToScreen(p);
        }

        public void FromFile(string filePath)
        {
            Image img = Image.FromFile(filePath);
            _currBmp = new Bitmap(img);
            if (pictureBox1.Width < _currBmp.Width || pictureBox1.Height < _currBmp.Height)
                _zoomScale = Math.Min((float)pictureBox1.Width / (float)_currBmp.Width, (float)pictureBox1.Height / (float)_currBmp.Height);


            else
                _zoomScale = 1.0f;
            img.Dispose();
            _isLoadBmp = true;
            pictureBox1.Refresh();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            if (_isLoadBmp)
            {
                Rectangle rect = new Rectangle(0, 0, _currBmp.Width, _currBmp.Height);
                rect.Width = (int)(rect.Width * _zoomScale);
                rect.Height = (int)(rect.Height * _zoomScale);
                rect.X = (int)((pictureBox1.Width - rect.Width) / 2) + _endPoint.X;
                rect.Y = (int)((pictureBox1.Height - rect.Height) / 2) + _endPoint.Y;
                g.FillRectangle(Brushes.White, pictureBox1.ClientRectangle);
                g.DrawImage(_currBmp, rect, new Rectangle(0, 0, _currBmp.Width, _currBmp.Height), GraphicsUnit.Pixel);
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            _isDown = true;
            _startPoint.X = e.Location.X - _endPoint.X;
            _startPoint.Y = e.Location.Y - _endPoint.Y;
            this.Cursor = Cursors.Hand;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isDown)
            {
                Point temp = e.Location;
                _endPoint.X = temp.X - _startPoint.X;
                _endPoint.Y = temp.Y - _startPoint.Y;
                pictureBox1.Refresh();
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            _isDown = false;
            this.Cursor = Cursors.Default;
        }

        private void pictureView_Resize(object sender, EventArgs e)
        {
            pictureBox1.Refresh();
        }
    }
}

图片缩放 滚动 图片移动 C#


[解决办法]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace NewTemp
{
    public partial class Form3 : Form
    {
        Point p = new Point();//图片位置
        public Form3()
        {
            InitializeComponent();
            this.MouseWheel += new MouseEventHandler(this_MouseWheel);
            Bitmap img = new Bitmap(@"F:\2.jpg");
            pictureBox1.Image = img;
            pictureBox1.Width = img.Width;
            pictureBox1.Height = img.Height;
            pictureBox1.Location = new Point(0, 0);
            pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
            getPoint(pictureBox1);
            //MessageBox.Show(p.X.ToString() + "  " + p.Y.ToString());
        }
        private void getPoint(Control c)
        {
            if (c.Parent != null)
            {
                p.X += c.Location.X;
                p.Y += c.Location.Y;
                getPoint(c.Parent);
            }
        }

        void this_MouseWheel(object sender, MouseEventArgs e)
        {
            //MessageBox.Show(e.X.ToString() +"  "+ e.Y.ToString());
            System.Drawing.Size t = pictureBox1.Size;
            int _w = t.Width;
            int w = t.Width + e.Delta;
            int h = Convert.ToInt32(t.Height + e.Delta * (Convert.ToDecimal(t.Height) / t.Width));
            if (w > 0 && h > 0)
            {
                pictureBox1.Width = w;
                pictureBox1.Height = h;
            }
            p = new Point(0, 0);
            getPoint(pictureBox1);
            //MessageBox.Show(p.X.ToString() + "  " + p.Y.ToString());
            try
            {
                Point p1 = pictureBox1.Location;
                int w1 = e.X - p.X;
                int h1 = e.Y - p.Y;
                int w2 = Convert.ToInt32(e.Delta * (Convert.ToDecimal(w1) / _w));
                int x = p1.X - w2;
                int y = Convert.ToInt32(p1.Y - w2 * (Convert.ToDecimal(h1) / w1));

                pictureBox1.Location = new Point(x, y);


            }
            catch { }
        }




        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.Location = new System.Drawing.Point(152, 57);
            this.pictureBox1.Margin = new System.Windows.Forms.Padding(0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(280, 221);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // Form3
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 267);
            this.Controls.Add(this.pictureBox1);
            this.Name = "Form3";
            this.Text = "Form3";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.PictureBox pictureBox1;
    }
}

试下这个
之前private void getPoint(Control c)
        {
            p.X += c.Location.X;
            p.Y += c.Location.Y;
            if (c.Parent != null)
                getPoint(c.Parent);
        }有问题的,

热点排行