1. 程式人生 > >Winform圖片區域性放大效果

Winform圖片區域性放大效果

有兩個picturebox控制元件,name預設沒有修改。其中picturebox1的img賦予初始值,picturebox2的visible=false;其餘不變,程式比較簡單,看以參考下。

[csharp] view plain copy print?
  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.Threading;  
  9. using System.Threading.Tasks;  
  10. using System.Windows.Forms;  
  11. namespace 區域性放大  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         Thread thDraw;  
  16.         delegatevoid myDrawRectangel();  
  17.         myDrawRectangel mydraw;  
  18.         private Point ptBegin = new Point();  
  19.         private
    bool blIsDrawRectangle = false;  
  20.         public Form1()  
  21.         {  
  22.             InitializeComponent();  
  23.         }  
  24.         privatevoid pictureBox1_Paint(object sender, PaintEventArgs e)  
  25.         {  
  26.             //畫矩形 初始值為false否則 一開始就會顯示矩形框
  27.             if (blIsDrawRectangle)  
  28.             {  
  29.                 e.Graphics.DrawRectangle(new
     Pen(Brushes.Black, 1), ptBegin.X, ptBegin.Y, 50, 50);  
  30.             }  
  31.         }  
  32.         privatevoid Form1_Load(object sender, EventArgs e)  
  33.         {  
  34.             mydraw = new myDrawRectangel(ShowDrawRectangle);  
  35.             thDraw = new Thread(Run);  
  36.             thDraw.Start();  
  37.         }  
  38.         privatevoid Run()  
  39.         {  
  40.             while (true)  
  41.             {  
  42.                 if (pictureBox1.Image != null)  
  43.                 {  
  44.                     this.BeginInvoke(mydraw);  
  45.                 }  
  46.                 Thread.Sleep(50);  
  47.             }  
  48.         }  
  49.         privatevoid Form1_FormClosed(object sender, FormClosedEventArgs e)  
  50.         {  
  51.             if (thDraw != null)  
  52.             {  
  53.                 thDraw.Abort();  
  54.             }  
  55.         }  
  56.         privatevoid ShowDrawRectangle()  
  57.         {  
  58.             Rectangle rec = new Rectangle(ptBegin.X * pictureBox1.Image.Size.Width / 460, ptBegin.Y * pictureBox1.Image.Size.Height / 350,  
  59.                                            50 * pictureBox1.Image.Size.Width / 460, 50 * pictureBox1.Image.Size.Height / 350);  
  60.             Graphics g = pictureBox2.CreateGraphics();  
  61.             g.DrawImage(pictureBox1.Image, pictureBox2.ClientRectangle, rec, GraphicsUnit.Pixel);  
  62.             g.Flush();  
  63.         }  
  64.         privatevoid pictureBox1_MouseLeave(object sender, EventArgs e)  
  65.         {  
  66.             pictureBox2.Visible = false;  
  67.             blIsDrawRectangle = false;  
  68.             pictureBox1.Refresh();  
  69.         }  
  70.         privatevoid pictureBox1_MouseEnter(object sender, EventArgs e)  
  71.         {  
  72.             pictureBox2.Visible = true;  
  73.             blIsDrawRectangle = true;  
  74.         }  
  75.         privatevoid pictureBox1_MouseMove(object sender, MouseEventArgs e)  
  76.         {  
  77.             if (e.X - 25 <= 0)  
  78.             {  
  79.                 ptBegin.X = 0;  
  80.             }  
  81.             elseif (pictureBox1.Size.Width - e.X <= 25)  
  82.             {  
  83.                 ptBegin.X = pictureBox1.Size.Width - 50;  
  84.             }  
  85.             else
  86.             {  
  87.                 ptBegin.X = e.X - 25;  
  88.             }  
  89.             if (e.Y - 25 <= 0)  
  90.             {  
  91.                 ptBegin.Y = 0;  
  92.             }  
  93.             elseif (pictureBox1.Size.Height - e.Y <= 25)  
  94.             {  
  95.                 ptBegin.Y = pictureBox1.Size.Height - 50;  
  96.             }  
  97.             else
  98.             {  
  99.                 ptBegin.Y = e.Y - 25;  
  100.             }  
  101.             pictureBox1.Refresh();  
  102.         }  
  103.     }  
  104. }