110,536
社区成员
发帖
与我相关
我的任务
分享
Bitmap bitmap;//申明为类成员
Graphics graph;
private void createImage()
{//调用的时候先调用createImage方法
bitmap=new Bitmap(this.Width,this.Height);
graph=new Graphics.FromImage(bitmap);
}
private void updateImage()
{
bitmap=new Bitmap(this.Width,this.Height);//问题在于为什么一定要在此处对bitmap重新new,
//如果不重新new就显示不出线条
graph=new Graphics.FromImage(bitmap);
graph.DrawLine(pen,0,2,20,50);
this.BackgroundImage=bitmap;
}
//如果这样写就不会有图形
private void updateImage()
{
graph.DrawLine(pen,0,2,20,50);
this.BackgroundImage=bitmap;
}
private Bitmap bitmap;
private Graphics graph;
private Pen penWave = new Pen(Color.Lime);
private Pen penGrid = new Pen(Color.Gray);
protected override void OnLoad(EventArgs e)
{
//打开双缓冲,防止闪烁
DoubleBuffered = true;
canvas_height = base.ClientSize.Height;
canvas_width = base.ClientSize.Width;
bitmap = new Bitmap(this.Width, this.Height);
graph = Graphics.FromImage(bitmap);
DrawGrids(ref graph);
this.BackgroundImage = bitmap;
}
protected override void OnResize(EventArgs e)
{
canvas_height = base.ClientSize.Height;
canvas_width = base.ClientSize.Width;
bitmap = new Bitmap(this.Width, this.Height);
graph = Graphics.FromImage(bitmap);
DrawGrids(ref graph);
DrawDot(ref graph);
this.BackgroundImage = bitmap;
}
private void DrawGrids(ref Graphics g)
{//画网格
//pos,canvas_height都是变量,不重复贴了,不影响整体
g.DrawLine(penGrid, pos, 0, pos, canvas_height);//基本上都是这种代码,不重复贴,没做别的处理
}
private void DrawWave(ref Graphics g)
{
graph.DrawLine(penWave,0,2,20,50);//基本上都是这种,两点之间连线的,不重复贴代码了
}
//调用的时候,会先调用这个CreateImage()方法,再调用UpdateImage
//CreateImage只调用一次,UpdateImage会调用多次
public void CreateImage()
{
bitmap=new Bitmap(this.Width,this.Height);
graph=new Graphics.FromImage(bitmap);
}
public void UpdateImage()
{//经过反复试验,在这个方法里必须要对bitmap重新new,否则看不到两点间的连线
bitmap = new Bitmap(this.Width, this.Height);
graph = Graphics.FromImage(bitmap);
DrawDot(ref graph);
this.BackgroundImage = bitmap;
}
本来是想在bitmap上先画好格子,然后再调用UpdateImage只画点,不用重复画格子,但是现在的问题是每次都要对bitmap重新new,于是只能每次都要重新画格子,这样影响效率,所以才问这么个问题。为什么之前对bitmap创建过实例了,然后进行画图看不到图形?private void updateImage()
{
graph.DrawLine(pen,0,2,20,50);
this.BackgroundImage=bitmap;
}
改为:
private void updateImage()
{
graph.DrawLine(pen,0,2,20,50);
this.BackgroundImage=bitmap;
OnBackgroundImageChanged(new EventArgs());
}
控件的BackgroundImage属性在设置值的时候,有一段判断语句:
if(this.BackgroundImage != value)
因此对于第二次的this.BackgroundImage=bitmap;操作是没有任何动作的。不过你可以人为地引发那个事件。
private Bitmap bitmap;
private Graphics graph;
private Pen penWave = new Pen(Color.Lime);
private Pen penGrid = new Pen(Color.Gray);
protected override void OnLoad(EventArgs e)
{
//打开双缓冲,防止闪烁
DoubleBuffered = true;
canvas_height = base.ClientSize.Height;
canvas_width = base.ClientSize.Width;
CreateImage();
DrawGrids(ref graph);
this.BackgroundImage = bitmap;
}
protected override void OnResize(EventArgs e)
{
canvas_height = base.ClientSize.Height;
canvas_width = base.ClientSize.Width;
this.Refresh();
}
private void DrawGrids()
{//画网格
//pos,canvas_height都是变量,不重复贴了,不影响整体
graph.DrawLine(penGrid, pos, 0, pos, canvas_height);//基本上都是这种代码,不重复贴,没做别的处理
}
private void DrawWave()
{
graph.DrawLine(penWave,0,2,20,50);//基本上都是这种,两点之间连线的,不重复贴代码了
}
//调用的时候,会先调用这个CreateImage()方法,再调用UpdateImage
//CreateImage只调用一次,UpdateImage会调用多次
public void CreateImage()
{
bitmap=new Bitmap(this.Width,this.Height);
graph=new Graphics.FromImage(bitmap);
}
public void UpdateImage()
{//经过反复试验,在这个方法里必须要对bitmap重新new,否则看不到两点间的连线
DrawDot();
this.Refresh();
}
改成酱紫应该没问题了。