c# winfrom 放大镜功能

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Threading;
 10 
 11 namespace test
 12 {
 13     public partial class Form2 : Form
 14     {
 15         private bool blIsDrawRectangle = true;
 16         private Point ptBegin = new Point();
 17         Thread thDraw;
 18         delegate void myDrawRectangel();
 19         myDrawRectangel myDraw;
 20         public Form2()
 21         {
 22             InitializeComponent();
 23         }
 24 
 25         private void pictureBox1_Paint(object sender, PaintEventArgs e)
 26         {
 27             if (blIsDrawRectangle)
 28             {
 29                 e.Graphics.DrawRectangle(new Pen(Brushes.Black, 1), ptBegin.X, ptBegin.Y, 50, 50);
 30             }
 31         }
 32 
 33         private void Form2_Load(object sender, EventArgs e)
 34         {
 35             myDraw = new myDrawRectangel(ShowDrawRectangle);
 36             thDraw = new Thread(Run);
 37             thDraw.Start();
 38         }
 39         private void Run()
 40         {
 41             while (true)
 42             {
 43                 if (pictureBox1.Image != null)
 44                 {
 45                     this.BeginInvoke(myDraw);
 46                 }
 47                 Thread.Sleep(50);
 48             }
 49         }
 50         private void ShowDrawRectangle()
 51         {
 52             Rectangle rec = new Rectangle(ptBegin.X * pictureBox1.Image.Size.Width / 460,
 53                 ptBegin.Y * pictureBox1.Image.Size.Height / 350, 50 * pictureBox1.Image.Size.Width / 460,
 54                 50 * pictureBox1.Image.Size.Height / 350);
 55             Graphics g = pictureBox2.CreateGraphics();
 56             g.DrawImage(pictureBox1.Image, pictureBox2.ClientRectangle, rec, GraphicsUnit.Pixel);
 57             g.Flush();
 58         }
 59 
 60         private void pictureBox1_MouseLeave(object sender, EventArgs e)
 61         {
 62             blIsDrawRectangle = false;
 63             pictureBox1.Refresh();
 64         }
 65 
 66         private void pictureBox1_MouseEnter(object sender, EventArgs e)
 67         {
 68             blIsDrawRectangle = true;
 69         }
 70 
 71         private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
 72         {
 73             if (e.X - 25 <= 0)
 74             {
 75                 ptBegin.X = 0;
 76             }
 77             else if (pictureBox1.Size.Width - e.X <= 25)
 78             {
 79                 ptBegin.X = pictureBox1.Size.Width - 50;
 80             }
 81             else
 82             {
 83                 ptBegin.X = e.X - 25;
 84             }
 85 
 86             if (e.Y - 25 <= 0)
 87             {
 88                 ptBegin.Y = 0;
 89             }
 90             else if (pictureBox1.Size.Height-e.Y<=25)
 91             {
 92                 ptBegin.Y = pictureBox1.Size.Height - 50;
 93             }
 94             else
 95             {
 96                 ptBegin.Y = e.Y - 25;
 97             }
 98             pictureBox1.Refresh();
 99         }
100     }
101 }
View Code

原文地址:https://www.cnblogs.com/mby2012/p/7324398.html