2017-08-16 3 views
1

私は2つのフォームを持つアプリケーションを持っています。それはターンクローズ後に実行するフォーム#

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

iはForm1のピクチャに使用することができ、ズームレンズである:Form1がピクチャボックスやボタンを持って、Form2が、私は、フォームを呼び出し、コードを持っています。

問題は、Form2(レンズ)が動いていて、フォーム2を閉じるためにESCをクリックすると、閉じても消費メモリが増えてしまうという問題です。 form2(レンズ)が持っているエラーでさえ、form2の近くでコールした後でさえ、マウスを境界線であまりにも遠くに移動させるようなものです。ここで

は、レンズの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); 
} 

は、Form1がruningておく一方で、このForm2のを閉じて、すべてのメソッドを停止するには、そのようですか?それは、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

あなたは私の友人です。そのそのexacly。その後、私は単にclose()アクションでtimer.Stopを呼び出す必要があります。あなたにおねがいします。 – Brugo

関連する問題