2017-02-13 56 views
0

我正在爲我正在編寫的程序放置一個模擬時鐘。我有時鐘工作,關於數學是否正確等,但是我無法從生成時鐘面的新圖像之前刪除上一個滴答的圖像。我的代碼如下,我認爲cg.Dispose();行會做到這一點,但它不想。我也嘗試在每次打勾結束時處理位圖,但是它也引發了錯誤。在表單中,我有一個pictureBox和一個長度爲1000ms的計時器。另外值得注意的是我有錶盤的代碼,但我只是沒有把它包含在這!模擬時鐘 - 在繪製新圖像之前無法刪除之前的圖像

using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 

    namespace ClockStreamlined 
    { 
public partial class Form1 : Form 
{ 
    Bitmap bmp; 
    Graphics cg; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

     timer1.Enabled = true; 
     bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); 

    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     pictureBox1.Refresh(); 
     cg = Graphics.FromImage(bmp); 
     // Get components of the DateTime 
     DateTime DateTimenow = DateTime.Now; 
     double hour = DateTimenow.Hour; 
     double minute = DateTimenow.Minute; 
     double second = DateTimenow.Second; 

     // Define increment movements for each clock hand 
     double hourHandangle = (30 * hour) + (minute * 0.5); 
     double minuteHandangle = 6 * minute; 
     double secondHandangle = 6 * second; 

     // Define xy position variables for hour minute & second 
     double x_hour = 0; 

     double x_minute = 0; 

     double x_second = 0; 

     // Calculate hour hand position 

     double y_hour = -(((pictureBox1.Width)/2) - 50) * Math.Cos((hourHandangle * 2 * Math.PI)/360); 
     if (hourHandangle >= 0 && hourHandangle <= 180) 
     { 
      x_hour = (((pictureBox1.Width)/2) - 50) * Math.Sin((hourHandangle * 2 * Math.PI)/360); 
     } 

     else 
     { 
      x_hour = -(((pictureBox1.Width)/2) - 50) * -1 * Math.Sin((hourHandangle * 2 * Math.PI)/360); 
     } 

     // Calculate minute hand position 

     double y_minute = -(((pictureBox1.Width)/2) - 30) * Math.Cos((minuteHandangle * 2 * Math.PI)/360); 

     if (minuteHandangle >= 0 && minuteHandangle <= 100) 
     { 
      x_minute = (((pictureBox1.Width)/2) - 30) * Math.Sin((minuteHandangle * 2 * Math.PI)/360); 
     } 
     else 
     { 
      x_minute = -(((pictureBox1.Width)/2) - 30) * -1 * Math.Sin((minuteHandangle * 2 * Math.PI)/360); 
     } 

     // Calculate second hand position 
     double y_second = -(((pictureBox1.Width)/2) - 10) * Math.Cos((secondHandangle * 2 * Math.PI)/360); 
     if (secondHandangle >= 0 && secondHandangle <= 100) 
     { 
      x_second = (((pictureBox1.Width)/2) - 10) * Math.Sin((secondHandangle * 2 * Math.PI)/360); 
     } 
     else 
     { 
      x_second = -(((pictureBox1.Width)/2) - 10) * -1 * Math.Sin((secondHandangle * 2 * Math.PI)/360); 
     } 

     // Get points that define hour hand 
     int y_hrpoint = Convert.ToInt32(y_hour); 
     int x_hrpoint = Convert.ToInt32(x_hour); 

     // Get points that define minute hand 
     int y_minpoint = Convert.ToInt32(y_minute); 
     int x_minpoint = Convert.ToInt32(x_minute); 

     // Get points that define second hand 
     int x_secpoint = Convert.ToInt32(x_second); 
     int y_secpoint = Convert.ToInt32(y_second); 

     // Create pen 
     Pen blackPen = new Pen(Color.Black, 3); 

     // Create points that define hour hand 
     Point point1hr = new Point(x_hrpoint + ((pictureBox1.Width)/2), y_hrpoint + ((pictureBox1.Width)/2)); 
     Point point2hr = new Point(((pictureBox1.Width)/2), ((pictureBox1.Width)/2)); 

     // Create points that define minute hand 
     Point point1min = new Point(x_minpoint + ((pictureBox1.Width)/2), y_minpoint + ((pictureBox1.Width)/2)); 
     Point point2min = new Point(((pictureBox1.Width)/2), ((pictureBox1.Width)/2)); 

     // Create points that define second hand 
     Point point1sec = new Point(x_secpoint + ((pictureBox1.Width)/2), y_secpoint + ((pictureBox1.Width)/2)); 
     Point point2sec = new Point(((pictureBox1.Width)/2), ((pictureBox1.Width)/2)); 

     // Draw to Bitmap 
     // Draw line to screen. 
     cg.DrawLine(blackPen, point1hr, point2hr); 
     cg.DrawLine(blackPen, point1min, point2min); 
     cg.DrawLine(blackPen, point1sec, point2sec); 
     pictureBox1.Image = bmp; 
     cg.Dispose(); 
     Invalidate(); 

    } 
} 
    } 

回答

1

bmptimer1_Tick函數的局部變量,並在timer1_Tick開始創建bmp位圖各一次。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     timer1.Enabled = true; 

    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);; 
     Graphics cg = Graphics.FromImage(bmp); 

     // your other code from timer1_Tick 
     // Get components of the DateTime 
     DateTime DateTimenow = DateTime.Now; 
     double hour = DateTimenow.Hour; 
     double minute = DateTimenow.Minute; 
     double second = DateTimenow.Second; 

     // .... 

     pictureBox1.Image = bmp; 
     cg.Dispose(); 
    } 
} 
+0

你的傳說,非常感謝! – acodeoficeandfire

+0

@acodeoficeandfire如果它解決了問題,則將其標記爲答案 – ColinM

1

要清除以前的圖紙您需要調用cg.Clear(Color.White)與任何你想要的背景顏色。

如果您的計時器計時器使面板失效,那麼面板的OnPaint事件將執行實際繪圖也會更好。你也並不需要使用位圖。

public Form1() 
{ 
    InitializeComponent(); 
} 

private void timer1_Tick(object sender, EventArgs e) 
{ 
    pictureBox1.Invalidate(); 
} 

private void pictureBox1_Paint(object sender, PaintEventArgs e) 
{ 
    // Compute hand positions. 
    // ... 


    // Clear the previous drawing. 
    e.Graphics.Clear(Color.White); 

    // Create pen, draw the hands. 
    using (Pen blackPen = new Pen(Color.Black, 3)) 
    { 
     e.Graphics.DrawLine(blackPen, point1hr, point2hr); 
     e.Graphics.DrawLine(blackPen, point1min, point2min); 
     e.Graphics.DrawLine(blackPen, point1sec, point2sec); 
    } 
}