C#實現放大鏡


winform實現一個跟隨鼠標移動放大功能

實現步驟:

1、創建一個Form1,一個計時器timer1和一個圖片顯示控件pictureBox1

2、核心代碼

          int magnification = 2;//倍率,調節放大倍數,可由TrackBar控制調節    
          int mx;                 //鼠標x坐標
          int my;                 //鼠標y坐標
          const int imgWidth = 500;//放大后圖片的寬度
          const int imgHeight = 400;//放大后圖片的高度
  
          //對定時器添加Tick事件,並設置Enabled為True
          private void timer1_Tick(object sender, EventArgs e)
          {            
             mx = Control.MousePosition.X;
             my = Control.MousePosition.Y;
        //對圖像進行放大顯示         
             Bitmap bt = new Bitmap(imgWidth / magnification, imgHeight / magnification);
             Graphics g = Graphics.FromImage(bt);
             g.CopyFromScreen(
new Point(Cursor.Position.X - imgWidth / (2*magnification),
Cursor.Position.Y - imgHeight / (2*magnification)),
new Point(0, 0),
new Size(imgWidth / magnification, imgHeight / magnification)); IntPtr dc1 = g.GetHdc(); g.ReleaseHdc(dc1); pictureBox1.Image = (Image)bt; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM