MSDN
public partial class Form1 : Form
{
Bitmap DrawArea;
public Form1()
{
InitializeComponent();
DrawArea = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height);
pictureBox1.Image = DrawArea;
}
private void Form1_Load(object sender, EventArgs e)
{
Graphics g;
g = Graphics.FromImage(DrawArea);
Pen mypen = new Pen(Brushes.Black);
g.DrawLine(mypen, 0, 0, 200, 200);
g.Clear(Color.White);
g.Dispose();
}
private void Button1_Click(object sender, EventArgs e)
{
Graphics g;
g = Graphics.FromImage(DrawArea);
Pen mypen = new Pen(Color.Black);
g.DrawLine(mypen, 0,0,200,150);
pictureBox1.Image = DrawArea;
g.Dispose();
}
private
void
pictureBox1_Paint(
object
sender, PaintEventArgs e)
{
int
picBoxWidth = pictureBox1.Size.Width;
int
picBoxHeight = pictureBox1.Size.Height;
int
halfWidth = pictureBox1.Size.Width / 2;
int
halfHeight = pictureBox1.Size.Height / 2;
Graphics objGraphic = e.Graphics;
//**請注意這一行**
Pen pen =
new
Pen(Color.Black);
int
b = 2;
int
m = 2;
for
(
int
x = 0; x < 426; x++)
{
int
y = m * x + b; x = (y - b) / m;
objGraphic.DrawLine(pen, x, y, -x, -y);
System.Drawing.Drawing2D.GraphicsState graph = objGraphic.Save();
objGraphic.Restore(graph);
}
objGraphic.DrawLine(pen, 0, halfHeight, picBoxWidth, halfHeight);
objGraphic.DrawLine(pen, halfWidth, 0, halfWidth, picBoxHeight);
objGraphic.DrawLine(pen, 20, 20, 300, 300);
}
from:http://trufflepenne.blogspot.tw/2010/11/cpicturebox.html