using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace ImageScale { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.StartPosition = FormStartPosition.CenterScreen; this.pictureBox1.BorderStyle = BorderStyle.FixedSingle; this.pictureBox1.BackColor = Color.DarkGray; this.pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel); } Bitmap m_bmp; Point m_ptCanvas; Point m_ptCanvasBuf; Point m_ptBmp; float m_nScale = 1.0F; Point m_ptMouseDown; string m_strMousePt; private void Form1_Load(object sender, EventArgs e) { m_bmp = Properties.Resources.QQ20170722225306; m_ptCanvas = new Point(pictureBox1.Width / 2, pictureBox1.Height / 2); m_ptBmp = new Point(-(m_bmp.Width / 2), -(m_bmp.Height / 2)); } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.TranslateTransform(m_ptCanvas.X, m_ptCanvas.Y); g.ScaleTransform(m_nScale, m_nScale); g.DrawImage(m_bmp, m_ptBmp); g.ResetTransform(); } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Middle) { m_ptMouseDown = e.Location; m_ptCanvasBuf = m_ptCanvas; } pictureBox1.Focus(); } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Middle) { m_ptCanvas = (Point)((Size)m_ptCanvasBuf + ((Size)e.Location - (Size)m_ptMouseDown)); pictureBox1.Invalidate(); } } private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) { if (m_nScale <= 0.3 && e.Delta <= 0) return; if (m_nScale >= 4.9 && e.Delta >= 0) return; SizeF szSub = (Size)m_ptCanvas - (Size)e.Location; float tempX = szSub.Width / m_nScale; float tempY = szSub.Height / m_nScale; m_ptCanvas.X -= (int)(szSub.Width - tempX); m_ptCanvas.Y -= (int)(szSub.Height - tempY); szSub.Width = tempX; szSub.Height = tempY; m_nScale += e.Delta > 0 ? 0.2F : -0.2F; m_ptCanvas.X += (int)(szSub.Width * m_nScale - szSub.Width); m_ptCanvas.Y += (int)(szSub.Height * m_nScale - szSub.Height); pictureBox1.Invalidate(); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)