C#实现放大镜

时间:2022-01-23 02:15:36

winform实现一个跟随鼠标移动放大功能

实现步骤:

1、创建一个Form1,一个计时器timer1和一个图片显示控件pictureBox1

2、核心代码

          int magnification = ;//倍率,调节放大倍数,可由TrackBar控制调节
int mx; //鼠标x坐标
int my; //鼠标y坐标
const int imgWidth = ;//放大后图片的宽度
const int imgHeight = ;//放大后图片的高度 //对定时器添加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 / (*magnification),
Cursor.Position.Y - imgHeight / (*magnification)),
new Point(, ),
new Size(imgWidth / magnification, imgHeight / magnification));
IntPtr dc1 = g.GetHdc();
g.ReleaseHdc(dc1);
pictureBox1.Image = (Image)bt;
}