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; }
分类: C#,Winform