【问题标题】:c# form running after closec#表单关闭后运行
【发布时间】:2017-08-16 20:44:47
【问题描述】:

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

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

它是一个变焦镜头,我可以在 Form1 的图片框中使用。

问题是,当 Form2(lens) 正在运行并且我单击 ESC 关闭 form2 时,它会关闭但不断增加消耗的内存。甚至 form2(lens) 的错误也会触发,例如在边界处将鼠标移动得太远,即使在调用接近 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 秒。

【问题讨论】:

  • 您是否从第一个表单打开另一个表单?类似于打开主表单的登录表单?

标签: c# forms winforms


【解决方案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         
}

此外,请确保在不再使用图像和图形对象时也将它们处理掉。

【讨论】:

  • 谢谢你,我的朋友。正是如此。之后,我只需要使用 close() 操作调用 timer.Stop。谢谢你。
猜你喜欢
  • 1970-01-01
  • 2017-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
  • 2021-11-08
相关资源
最近更新 更多