Graphics.FromImage(bitmap)的问题,为什么每次都要重新new bitmap

leafmao leafmao 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
使用Bitmap对控件进行绘图,并且令控件的Background为Bitmap,代码如下


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;
}


调用的时候是先调用的createImage(),先创建了bitmap和graph,然后再调用updateImage()方法,但是在updateImage里一定要对bitmap重新赋值才能画出图形,请教这是为什么?
...全文
评论 25
收藏 已收藏
分享
返回顶部
leafmao
C# 111,128 成员 642,537 内容