C# 如何使用对话框保存文件?
我正在尝试用下面的代码保存打印屏幕,但它不起作用C# 如何使用对话框保存文件?,c#,C#,我正在尝试用下面的代码保存打印屏幕,但它不起作用 private void button3_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); if (saveFileDialog1.ShowDialog() == DialogResult.OK) { button1.Visible
private void button3_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
button1.Visible = false;
button2.Visible = false;
Bitmap bitmap = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(bitmap, this.ClientRectangle);
bitmap.Save("myPrintScreen.bmp");
button1.Visible = true;
button2.Visible = true;
}
}
使用
您可以使用文件名
设置文件名。例如:
saveFileDialog1.FileName = "myPrintScreen.bmp";
[问题编辑后再编辑]
改变
致:
你需要这个。
请看这里提供的示例。。看看这个例子included@illDev因为没有研究工作。试着用谷歌搜索你自己的标题。我把文件放在哪里了?`SaveFileDialog saveFileDialog1=新建SaveFileDialog();如果(saveFileDialog1.ShowDialog()==DialogResult.OK){button1.Visible=false;button2.Visible=false;位图=新位图(this.Width,this.Height);this.DrawToBitmap(Bitmap,this.clientrangle);Bitmap.Save(“myPrintScreen.bmp”);button1.Visible=true;button2.Visible=true;}`?您可以在调用
.OpenDialog()
私有无效按钮3\u单击(对象发送者,事件参数e){SaveFileDialog saveFileDialog1=new SaveFileDialog();saveFileDialog1.filename=“myPrintScreen.bmp”;如果(saveFileDialog1.ShowDialog()==DialogResult.OK){button1.Visible=false;button2.Visible=false;位图位图=new位图(this.Width,this.Height);使用(var Stream=saveFileDialog1.OpenFile()){Bitmap.Save(Stream,Bitmap.bmp);}button1.Visible=true;button2.Visible=true;}
bitmap.Save(Stream,bitmap.bmp);
应该是bitmap.Save(Stream,ImageFormat.bmp);
因为您删除了行this.DrawToBitmap(bitmap,this.ClientRectangle);
saveFileDialog1.FileName = "myPrintScreen.bmp";
this.DrawToBitmap(bitmap, this.ClientRectangle);
using(var Stream = saveFileDialog1.OpenFile())
{
bitmap.Save(Stream , ImageFormat.Bmp);
}