2017-08-16 72 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