如果你想在窗体上进行绘画。通常会使用以下方法.
方法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;
}