c#表单关闭后运行

时间:2017-08-16 20:44:48

标签: c# forms winforms

我有2个表单的应用程序。 Form1有pictureBox和按钮,Form2有一个代码,当我调用表单时:

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

它变成了一个变焦镜头,我可以在Form1 pictureBox中使用。

问题是,当Form2(镜头)运行并且我单击ESC关闭form2时,它会关闭但不断增加消耗内存。即使形成2(镜头)的错误也会触发,就像在边界处移动鼠标一样,即使在接近form2之后也是如此。

以下是Lens form2的代码:

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

这是一种关闭此form2并在form1继续运行时停止所有方法的方法吗?它持续增加内存消耗,例如1mb。

1 个答案:

答案 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         
}

此外,请确保在不再使用图像和图形对象时将其丢弃。