如果你想在窗體上進行繪畫。通常會使用以下方法. 方法1,利用控件或窗體的paint事件中的painEventArgs private void form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics;//創建畫板, } 方法2直接重載控件或窗體的OnPaint方法 protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; } 方法3調用某控件的CreateGraphics方法 Graphics g = this.CreateGraphics(); 方法4 調用Graphics類的FromImage靜態方法 在需要更改已存在的圖像時,通常會使用此方法 Image img = Image.FromFile("g1.jpg");//建立Image對象 Graphics g = Graphics.FromImage(img);//創建Graphics對象
public Bitmap Create(int[] arry) { //獲得數組中最大值 int max = 0; for (int i = 0; i < arry.Length; i++) { if (arry[i] > max) max = arry[i]; } Bitmap bitmap = new Bitmap(arry.Length, max+10); Graphics g = Graphics.FromImage(bitmap);//創建Graphics對象 g.Clear(Color.White); Pen curPen = new Pen(Brushes.Black, 1); // g.DrawLine(curPen, 10, 0, 10, 30); //划線 ; 水平坐標形同 10,0,10,30; y坐標不同 for (int i = 0; i < arry.Length; i++) { g.DrawLine(curPen, i, arry[i], i, 0); //划線 ; 水平坐標形同 10,0,10,30; y坐標不同 } return bitmap; }