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; }