您好,现在在做一个关于类似windows画图的软件,但是碰到一个问题,就是实现填充功能的时候,只能填充一个对象,当填充第二个对象的时候,之前填充的图案就消失了,能否请大神帮忙解决下,以下是我贴的代码,麻烦了。
private void Form1_Paint(object sender, PaintEventArgs e)//实现无闪烁绘图
{
//Rectangle rect = e.ClipRectangle;
BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current;
BufferedGraphics myBuffer = currentContext.Allocate(e.Graphics, e.ClipRectangle);
Graphics g = myBuffer.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighSpeed;
g.Clear(this.BackColor);
if (theImage != null)
{
//g.Clear(Color.White);
g.DrawImage(theImage, this.ClientRectangle);
}
myBuffer.Render(e.Graphics);
g.Dispose();
myBuffer.Dispose();//释放资源
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//如果开始绘制,则开始记录鼠标位置
if ((isDrawing = !isDrawing) == true)
{
startPoint = new Point(e.X, e.Y);
oldPoint = new Point(e.X, e.Y);
}
}
}
Rectangle rect;
Rectangle elli;
int juj;//判断是矩形还是椭圆
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Graphics g;
g = this.CreateGraphics();
if (isDrawing)
{
switch (drawTool)
{
case drawTools.None:
break;
case drawTools.Pen:
//从上一个点到当前点绘制线段
g.DrawLine(new Pen(foreColor, line_width), oldPoint, new Point(e.X, e.Y));
ig.DrawLine(new Pen(foreColor, line_width), oldPoint, new Point(e.X, e.Y));
oldPoint.X = e.X;
oldPoint.Y = e.Y;
break;
case drawTools.Line:
//首先恢复此次操作之前的图像,然后再添加Line
this.Form1_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));//可能是这一步使得之前的填充步骤失效了。
g.DrawLine(new Pen(foreColor, line_width), startPoint, new Point(e.X, e.Y));
break;
case drawTools.Ellipse:
//首先恢复此次操作之前的图像,然后再添加Ellipse
this.Form1_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
g.DrawEllipse(new Pen(foreColor, line_width), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
break;
case drawTools.Rectangle:
//首先恢复此次操作之前的图像,然后再添加Rectangle
this.Form1_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
g.DrawRectangle(new Pen(foreColor, line_width), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
//Rectangle rect;
break;
case drawTools.String:
break;
case drawTools.Rubber:
//用背景色绘制宽线段
g.DrawLine(new Pen(backColor, 20), oldPoint, new Point(e.X, e.Y));
ig.DrawLine(new Pen(backColor, 20), oldPoint, new Point(e.X, e.Y));
oldPoint.X = e.X;
oldPoint.Y = e.Y;
break;
}
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
switch (drawTool)
{
case drawTools.Line:
ig.DrawLine(new Pen(foreColor, line_width), startPoint, new Point(e.X, e.Y));
break;
case drawTools.Ellipse:
ig.DrawEllipse(new Pen(foreColor, line_width), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
elli = new Rectangle(startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
juj = 2;
break;
case drawTools.Rectangle:
ig.DrawRectangle(new Pen(foreColor, line_width), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
rect = new Rectangle(startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);//试验
juj = 1;
break;
}
}
private void toolStripButton6_Click_1(object sender, EventArgs e)填充操作
{
switch(juj)
{
case 1:
{
Bitmap bit = new Bitmap(1000, 1000);//绘图一般画在位图上
//Graphics g = Graphics.FromImage(bit);
Graphics g = this.CreateGraphics();
SolidBrush b1 = new SolidBrush(Color.Red);//
g.FillRectangle(b1, rect);//这句实现填充矩形的功能
g.Dispose();
break;
}
case 2:
{
Bitmap bit = new Bitmap(1000, 1000);//绘图一般画在位图上
//Graphics g = Graphics.FromImage(bit);
Graphics g = this.CreateGraphics();
SolidBrush b1 = new SolidBrush(Color.Red);//
g.FillEllipse(b1, elli);//这句实现填充矩形的功能
g.Dispose();
break;
}
}
}
------解决思路----------------------
强制执行下Paint事件而已,最好不要这样调用
Control都有Invalid 和Refresh方法,可以刷新界面
------解决思路----------------------
可能是你图片与PictureBox大小不一致,并且显示时自动缩放造成的.
------解决思路----------------------
杭电的