根据不同的屏幕分辨率变焦镜头

时间:2017-08-29 19:39:35

标签: c# winforms zoom resolution lens

我正在使用此代码创建变焦镜头,在我的计算机上正常工作,但是当我尝试在笔记本电脑上运行我的程序(不是1080p屏幕)时,它无法正常工作。

此镜头的作用是显示图像的一部分,即鼠标悬停的部分,以正方形缩放。但是在其他计算机上它没有显示鼠标所在的部分,缩放部分显示远离鼠标指针。

我应该怎么做这个代码工作在其他计算机上的决议?

public partial class FormLens : Form
{
    PictureBox pictureBox1 = new PictureBox(); // Have a picture box
    int zoom = 4; // Variable for zoom value
    Bitmap printscreen;
    Timer timer = new Timer();
    public FormLens()
    {
        InitializeComponent();


        pictureBox1.Dock = DockStyle.Fill; // Occupy the full area of the form
        pictureBox1.BorderStyle = BorderStyle.FixedSingle; // Have a single border of clear representation
        Controls.Add(pictureBox1); // Add the control to the form
        FormBorderStyle = FormBorderStyle.None; // Make the form borderless to make it as lens look

        timer.Interval = 100; // Set the interval for the timer
        timer.Tick += timer_Tick; // Hool the event to perform desire action
        timer.Start(); //Start the timer
        printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); // Have a bitmap to store the image of the screen         
    }



    void timer_Tick(object sender, EventArgs e)
    {
        var graphics = Graphics.FromImage(printscreen as Image); // Get the image of the captured screen
        graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size); // Get the copy of screen
        var position = Cursor.Position; // Get the position of cursor
        var lensbmp = new Bitmap(50, 50); // Have a bitmap for lens
        var i = 0; // Variable for row count
        var j = 0; // Variable for column count
        for (int row = position.X - 25; row < position.X + 25; row++) // Indicates row number
        {
            j = 0; // Set column value '0' for new column
            for (int column = position.Y - 25; column < position.Y + 25; column++) // Indicate column number
            {
                try
                {
                    lensbmp.SetPixel(i, j, printscreen.GetPixel(row, column)); // Place current region pixel to lens bitmap
                    j++; // Increase row count
                }
                catch
                {
                    timer.Stop();
                    timer.Dispose();
                    this.Close();                                                
                    //MessageBox.Show("Lente for dos limites da tela");
                    break;
                }


            }
            i++; // Increase column count
        }
        this.pictureBox1.Image = new Bitmap(lensbmp, lensbmp.Width * zoom, lensbmp.Height * zoom); // Assign lens bitmap with zoom level to the picture box
        Size = pictureBox1.Image.Size; // Assign optimal value to the form
        Left = position.X + 25; // Place form nearer to cursor X value
        Top = position.Y + 25; // Place form nearer to cursor Y value
        TopMost = true; // Keep the form top level
    }

    // Override OnKeyDown for zoom in and zoom out actions
    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.KeyValue == 73) // Set "i" as the key for Zoom In.
            zoom++; // Increase zoom by 1 item greater
        else if (e.KeyValue == 79) // Set "o" as the key for Zoom Out
            zoom--; // Decrease zoom by 1 item smaller
        else if (e.KeyValue == 27) // Set "Esc" to close the magnifier
        {
            timer.Stop();
            timer.Dispose();
            this.Close(); // Close the form               
            Dispose(); // Dispose the form

        }
        base.OnKeyDown(e);
    }        


}

0 个答案:

没有答案