c#关闭后运行的表格

c#关闭后运行的表格,c#,forms,winforms,C#,Forms,Winforms,我有一个有两个表单的应用程序。Form1有pictureBox和按钮,Form2有一个代码,当我调用表单时: private void button1_Click(object sender, EventArgs e) { new Form2().Show(); } 它变成了一个变焦镜头,我可以在Form1 pictureBox中使用 问题是,当Form2(镜头)运行时,我单击ESC关闭Form2,它会关闭,但会不断增加内存消耗。甚至form2(lens)的错误也会触发,比如在调用接近

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

private void button1_Click(object sender, EventArgs e)
{
   new Form2().Show(); 
}
它变成了一个变焦镜头,我可以在Form1 pictureBox中使用

问题是,当Form2(镜头)运行时,我单击ESC关闭Form2,它会关闭,但会不断增加内存消耗。甚至form2(lens)的错误也会触发,比如在调用接近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);
} 
PictureBox pictureBox1=新的PictureBox();//我有一个画框
int zoom=1;//用于缩放值的变量
公共表格1()
{
pictureBox1.Dock=DockStyle.Fill;//占据表单的整个区域
pictureBox1.BorderStyle=BorderStyle.FixedSingle;//具有清晰表示的单个边框
控件。添加(pictureBox1);//将控件添加到窗体
FormBorderStyle=FormBorderStyle.None;//使窗体无边框,使其看起来像镜头一样
Timer Timer=new Timer();//为频繁更新设置计时器
timer.Interval=100;//设置计时器的间隔
timer.Tick+=timer\u Tick;//选择事件以执行愿望操作
timer.Start();//启动计时器
printscreen=新位图(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);//使用位图存储屏幕图像
}
无效计时器勾号(对象发送方,事件参数e)
{
var graphics=graphics.FromImage(以图像形式打印屏幕);//获取捕获屏幕的图像
graphics.CopyFromScreen(0,0,0,0,printscreen.Size);//获取screen的副本
var position=Cursor.position;//获取光标的位置
var lensbmp=新位图(50,50);//为镜头创建位图
var i=0;//行计数的变量
var j=0;//列计数的变量
for(int row=position.X-25;row

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

您有一个危险的计时器,因为它没有在表单作用域中声明,所以它仍然可以继续运行

改为在表单级别声明它:

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         
}

另外,当图像和图形对象不再使用时,也要确保将它们处理掉。

是否从第一个窗体打开另一个窗体?类似于打开主窗体的登录窗体?谢谢,我的朋友。确实如此。之后,我只需要调用timer.Stop和close()操作。谢谢。