Brush vBrush; int width, height, i; int x, y; Pen lpen; width = pictureBox1.Width / 20; height = pictureBox1.Height / 20; x = 0; y = 0; lpen = new Pen(Color.Gray); Graphics g = e.Graphics; for (i = 0; i < 20; i++) { x += width; g.DrawLine(lpen, x, y, x, y + pictureBox1.Height); } x = 0; y = 0; for (i = 0; i < 20; i++) { y += height; g.DrawLine(lpen, x, y, x + pictureBox1.Width, y); }
{
graphics = picBox.CreateGraphics();
} void picBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
graphics.DrawLine(new Pen(Brushes.Red), new Point(0, e.Y), new Point(picBox.Width, e.Y)); }
else if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
graphics.DrawLine(new Pen(Brushes.Red), new Point(e.X, 0), new Point(e.X, picBox.Height));
}
}
pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);在其上绘制点线
void pictureBox1_Paint(object sender, PaintEventArgs e)
{
//绘制点线的代码
}这样是不会影响背景图的,想提高点绘图性能可以只绘制需要更新的局部区域,比如你想画线,只需要重绘组成线的最后两个点构成的矩形区域即可
//由两点构成的矩形
public static Rectangle GetRectangle(Point p1, Point p2)
{
int minX = Math.Min(p1.X, p2.X);
int minY = Math.Min(p1.Y, p2.Y);
return new Rectangle(minX, minY, Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y));
}Rectangle rect = GetRectangle(点1, 点2);
rect.Inflate(2, 2);
pictureBox1.Invalidate(rect);
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
float X1;
float Y1; private void button1_Click(object sender, EventArgs e)
{
this.pictureBox1.Load(Application.StartupPath + "\\QQ图片20131023104740.jpg");
} private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
string dd;
dd = e.Button.ToString();
this.label1.Text = dd;
Graphics g=pictureBox1.CreateGraphics();
if (e.Button.ToString() == "Left")
{
g.DrawLine(new Pen(Color.DarkGray, 3),X1,Y1,e.X,e.Y );
X1 = e.X;
Y1 = e.Y;
} }
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
X1 = e.X;
Y1 = e.Y;
}
}
}
我在picturebox里的paint事件中
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Brush vBrush;
int width, height, i;
int x, y;
Pen lpen;
width = pictureBox1.Width / 20;
height = pictureBox1.Height / 20;
x = 0;
y = 0;
lpen = new Pen(Color.Gray);
Graphics g = e.Graphics;
for (i = 0; i < 20; i++)
{
x += width;
g.DrawLine(lpen, x, y, x, y + pictureBox1.Height);
}
x = 0;
y = 0;
for (i = 0; i < 20; i++)
{
y += height;
g.DrawLine(lpen, x, y, x + pictureBox1.Width, y);
}
strModule = "ECG";
vBrush = new SolidBrush(Color.Green);
g.DrawString(strModule, Font, vBrush, 10, 10);
}那个"ECG"的字符是需要变化的
实际上我想说的就是如何可以画两个图层,下面一层画背景,上面一层画点,线,擦除,不影响下面的图层。我用的是两个picturebox,一个放在上面,一个放在下面,以下在上面的picturebox画图,代码是两个按钮的点击事件,一个画直线,另外一个想画直线将原来的直线擦除一部分,但是没反应。private void btnTest_Click(object sender, EventArgs e)
{
grp = Graphics.FromImage(memoryBuffer);
Pen blackPen = new Pen(Color.Pink, 3);
grp.DrawLine(blackPen, 10, 60, 500, 60); grp.Dispose();
pictureBox2.Image = memoryBuffer;
} private void button1_Click(object sender, EventArgs e)
{
grp = Graphics.FromImage(memoryBuffer);
Pen blackPen = new Pen(Color.Transparent, 8);
grp.DrawLine(blackPen, 30, 30, 30, 90);
grp.Dispose();
pictureBox2.Image = memoryBuffer;
}
现在查到了一个方法,还是用两个picturebox控件叠加在一起,下层的picturebox1画背景,上层的picturebox2设置BackColor = Transparent,然后picturebox2.parent = picturebox1;就可以透明了。
然后再Bitmap上作图,通过以下代码可以实现擦除功能grp = Graphics.FromImage(memoryBuffer);GraphicsPath path = new GraphicsPath();
Rectangle r = new Rectangle(30, 30, 30, 90);
path.AddRectangle(r);
grp.SetClip(path);
grp.Clear(Color.Transparent);pictureBox2.Image = memoryBuffer;