2017-08-16 70 views
1

我有一个应用程序2种形式。 Form1上有图片框和按钮,窗体2有,当我打电话的形式代码:c#窗体关闭后运行

private void button1_Click(object sender, EventArgs e) 
{ 
    new Form2().Show(); 
} 

它原来是变焦镜头,我可以在Form1的图片框使用。

问题是,当Form2(透镜)运行时,我单击ESC关闭窗体2时,它会关闭,但会持续增加消耗内存。即使form2(镜头)所具有的错误仍在触发,就像移动鼠标到边界太远,即使在call2接近form2之后。

下面是代码的镜头窗口2:

PictureBox pictureBox1 = new PictureBox(); // Have a picture box 
int zoom = 1; // Variable for zoom value 
public Form1() 
{ 
    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 timer = new Timer(); // Have a timer for frequent update 
    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 
     { 
      lensbmp.SetPixel(i, j, printscreen.GetPixel(row, column)); // Place current region pixel to lens bitmap 
      j++; // Increase row count 
     } 
     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 + 20; // Place form nearer to cursor X value 
    Top = position.Y + 20; // 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 
    { 
     Close(); // Close the form 
     Dispose(); // Dispose the form 
    } 
    base.OnKeyDown(e); 
} 

那是一个办法关闭这个窗口2并停止所有方法,同时保持Form1的乳宁?它会持续增加内存消耗,如1mb每秒。

+0

你是否从第一个表单打开另一个表单?就像登录窗体打开主窗体一样? –

回答

1

您有一个危险的计时器,因为它没有在窗体范围声明,所以它仍然可以继续运行。

在形式层面,而不是它声明:

PictureBox pictureBox1 = new PictureBox(); 
int zoom = 1; 
Timer timer = new Timer(); 

public Form1() 
{ 
    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   
} 

此外,请确保您处理图像和图形对象的,同样,当他们不再使用。

+0

Tnk你我的朋友。它的无情。之后,我只需要用close()动作调用timer.Stop。 Tnk你。 – Brugo