C#中的on_paint事件有什么作用?
发布网友
发布时间:2022-05-05 15:36
我来回答
共1个回答
热心网友
时间:2022-06-27 16:59
不知道你用folderbrowserdialog这个东西是干什么用,要是想保存文件savefiledialog完全可以解决,即使是用folderbrowserdialog最后在foldername(这只是个文件夹名)后面也应该加个文件名如"aa.bmp",而且你在保存的时候写的是bit.save(savefiledialog.filename);savefiledialog没有显示过所以savefiledialog.filename为空,即路径的形式不合法
如果你想在窗体上画最好用picturebox,它的刷新效果比较好,用下面的方法可以实现简单的绘制,保存
首先托一个picturebox到窗体,然后就加上下面的代码就行了
bitmap
bit;
bool
draw
=
false;
graphics
g;
point
startpoint;
pen
pen;
savefiledialog
savefiledialog;
private
void
form1_load(object
sender,
eventargs
e)
{
picturebox1.dock
=
dockstyle.fill;
bit
=
new
bitmap(picturebox1.width,
picturebox1.height);
g
=
graphics.fromimage(bit);
g.clear(picturebox1.backcolor);//设置bit的背景色
pen
=
new
pen(color.red);
savefiledialog
=
new
savefiledialog();
savefiledialog.filter
=
"bmp|*.bmp|jpg|*.jpg|gif|*.gif";
}
private
void
picturebox1_mouseup(object
sender,
mouseeventargs
e)
{
draw
=
false;
}
private
void
picturebox1_mousemove(object
sender,
mouseeventargs
e)
{
if
(draw
&&
e.button
==
mousebuttons.left)
{
g.drawline(pen,
startpoint,
e.location);
startpoint
=
e.location;
picturebox1.backgroundimage
=
bit;
picturebox1.refresh();
}
}
private
void
picturebox1_mousedown(object
sender,
mouseeventargs
e)
{
if
(e.button
==
mousebuttons.left)
{
draw
=
true;
startpoint
=
e.location;
}
if
(e.button
==
mousebuttons.right)//右键保存
{
draw
=
false;
if
(savefiledialog.showdialog()
!=
dialogresult.cancel)
{
bit.save(savefiledialog.filename);
}
}
}